-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
celestix
committed
Aug 12, 2023
1 parent
ca22638
commit 0664052
Showing
5 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package shortener | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/AnimeKaizoku/cacher" | ||
"github.com/srinkco/srink/utils/randomiser" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
const DEF_SQLITE_FILE_NAME = "srink.sql" | ||
|
||
type InSQLEngine struct { | ||
session *gorm.DB | ||
cache *cacher.Cacher[string, string] | ||
} | ||
|
||
type Surl struct { | ||
Hash string `gorm:"primary_key"` | ||
Dest string | ||
} | ||
|
||
func newInSQLEngine(dbName string) *InSQLEngine { | ||
db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{ | ||
SkipDefaultTransaction: true, | ||
Logger: logger.Default.LogMode(logger.Error), | ||
}) | ||
if err != nil { | ||
panic("failed to connect database") | ||
} | ||
|
||
// Migrate the schema | ||
db.AutoMigrate(&Surl{}) | ||
|
||
return &InSQLEngine{ | ||
session: db, | ||
cache: cacher.NewCacher[string, string](&cacher.NewCacherOpts{ | ||
TimeToLive: time.Hour * 24 * 30, | ||
CleanInterval: time.Hour * 24 * 31, | ||
}), | ||
} | ||
} | ||
|
||
func (e *InSQLEngine) Shorten(url, hash string) string { | ||
if hash == "" { | ||
if hash, ok := e.getHash(url); ok { | ||
return hash | ||
} | ||
hash = randomiser.GetString(HASH_NUM) | ||
} | ||
e.saveUrl(hash, url) | ||
return hash | ||
} | ||
|
||
func (e *InSQLEngine) saveUrl(hash, dest string) { | ||
tx := e.session.Begin() | ||
v := &Surl{Hash: hash} | ||
tx.FirstOrCreate(v) | ||
v.Dest = dest | ||
tx.Save(v) | ||
tx.Commit() | ||
e.cache.Set(hash, dest) | ||
} | ||
|
||
func (e *InSQLEngine) getHash(url string) (string, bool) { | ||
var s Surl | ||
e.session.Where("dest = ?", url).Find(&s) | ||
return s.Hash, s.Hash != "" | ||
} | ||
|
||
func (e *InSQLEngine) GetUrl(hash string) string { | ||
url, ok := e.cache.Get(hash) | ||
if ok { | ||
return url | ||
} | ||
var s Surl | ||
e.session.Where("hash = ?", hash).Find(&s) | ||
if s.Dest != "" { | ||
e.cache.Set(hash, s.Dest) | ||
} | ||
return s.Dest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters