9 changed files with 188 additions and 60 deletions
-
1.gitignore
-
6go.mod
-
11go.sum
-
107lib/cache/cache.go
-
54lib/common/common.go
-
51lib/common/guid.go
-
1main.go
-
5models/token.go
-
12repositories/repsitory.go
@ -0,0 +1,107 @@ |
|||
package cache |
|||
|
|||
import ( |
|||
"fmt" |
|||
"time" |
|||
|
|||
badger "github.com/dgraph-io/badger/v2" |
|||
// log "github.com/sirupsen/logrus"
|
|||
) |
|||
|
|||
type Reader interface { |
|||
Get(key string) (*string, error) |
|||
} |
|||
|
|||
type Writer interface { |
|||
Set(key string, value string) error |
|||
SetWithTTL(key string, value string, ttl int) error |
|||
} |
|||
|
|||
type Cache interface { |
|||
Reader |
|||
Writer |
|||
Close() |
|||
} |
|||
|
|||
type bCache struct { |
|||
db *badger.DB |
|||
// logger *log.Entry
|
|||
} |
|||
|
|||
func NewCache(db *badger.DB) Cache { |
|||
return &bCache{ |
|||
db: db, |
|||
} |
|||
} |
|||
|
|||
func (cache bCache) close() { |
|||
cache.db.Close() |
|||
} |
|||
|
|||
// Get gets a cache entry by key
|
|||
func (cache bCache) Get(key string) (*string, error) { |
|||
var result []byte |
|||
if err := cache.db.View(func(txn *badger.Txn) error { |
|||
item, err := txn.Get([]byte(key)) |
|||
if err != nil { |
|||
if err.Error() != "Key not found" { |
|||
return nil |
|||
} |
|||
} |
|||
if err == nil || (item != nil && !item.IsDeletedOrExpired()) { |
|||
if err := item.Value(func(val []byte) error { |
|||
if err != nil { |
|||
return err |
|||
} |
|||
result = append([]byte{}, val...) |
|||
return nil |
|||
}); err != nil { |
|||
return err |
|||
} |
|||
} |
|||
return nil |
|||
}); err != nil { |
|||
return nil, err |
|||
} |
|||
r := string(result) |
|||
return &r, nil |
|||
} |
|||
|
|||
// SetWithTTL sets a cache entry
|
|||
func (cache bCache) Set(key string, value string) error { |
|||
if err := cache.db.Update(func(txn *badger.Txn) error { |
|||
err := txn.Set([]byte(key), []byte(value)) |
|||
return err |
|||
}); err != nil { |
|||
// cache.logger.WithFields(log.Fields{"module": "cache"}).Errorln(err.Error())
|
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
// SetWithTTL sets a cache entry with defined ttl
|
|||
func (cache bCache) SetWithTTL(key string, value string, ttl int) error { |
|||
if err := cache.db.Update(func(txn *badger.Txn) error { |
|||
e := badger.NewEntry([]byte(key), []byte(value)).WithTTL(time.Duration(ttl) * time.Second) |
|||
err := txn.SetEntry(e) |
|||
return err |
|||
}); err != nil { |
|||
// cache.logger.WithFields(log.Fields{"module": "cache"}).Errorln(err.Error())
|
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func (cache bCache) Close() { |
|||
cache.close() |
|||
} |
|||
|
|||
func Bootstrap() (Cache, error) { |
|||
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true)) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("Unable to open badger db: %s", err.Error()) |
|||
} |
|||
|
|||
cache := NewCache(db) |
|||
return cache, err |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
package common |
|||
|
|||
import ( |
|||
"database/sql/driver" |
|||
|
|||
"github.com/oklog/ulid/v2" |
|||
) |
|||
|
|||
// GUID our new ULID based datatype
|
|||
type GUID ulid.ULID |
|||
|
|||
// StringToGUID -> parse string to ULID GUID
|
|||
func StringToGUID(s string) (GUID, error) { |
|||
id, err := ulid.Parse(s) // maybe ParseStrict
|
|||
return GUID(id), err |
|||
} |
|||
|
|||
// String Representation of Binary16
|
|||
func (guid GUID) String() string { |
|||
return ulid.ULID(guid).String() |
|||
} |
|||
|
|||
// GormDataType sets type to binary(16)
|
|||
func (guid GUID) GormDataType() string { |
|||
return "binary(16)" |
|||
} |
|||
|
|||
func (guid GUID) MarshalJSON() ([]byte, error) { |
|||
str := "\"" + ulid.ULID(guid).String() + "\"" |
|||
return []byte(str), nil |
|||
} |
|||
|
|||
func (guid *GUID) UnmarshalJSON(data []byte) error { |
|||
id, err := ulid.Parse(string(data)) // maybe ParseStrict
|
|||
*guid = GUID(id) |
|||
return err |
|||
} |
|||
|
|||
// Scan tells GORM how to receive from the database
|
|||
func (guid *GUID) Scan(value interface{}) error { |
|||
converted := ulid.ULID(*guid) |
|||
c := &converted |
|||
err := c.Scan(value) |
|||
*guid = GUID(*c) |
|||
return err |
|||
} |
|||
|
|||
// Value tells GORM how to save into the database
|
|||
func (guid GUID) Value() (driver.Value, error) { |
|||
return ulid.ULID(guid).Value() |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
package repositories |
|||
|
|||
import ( |
|||
"git.devices.local/mawas/golang-api-skeleton/lib/common" |
|||
"gorm.io/gorm" |
|||
) |
|||
|
|||
type repo struct { |
|||
db *gorm.DB |
|||
userID common.GUID |
|||
username string |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue