@@ -0,0 +1,69 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"honoka-chan/config"
|
||||
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
"github.com/syndtr/goleveldb/leveldb/util"
|
||||
)
|
||||
|
||||
type LevelDbImpl struct {
|
||||
ldb *leveldb.DB
|
||||
}
|
||||
|
||||
var (
|
||||
LevelDb LevelDbImpl
|
||||
err error
|
||||
)
|
||||
|
||||
func init() {
|
||||
LevelDb.InitDb()
|
||||
}
|
||||
|
||||
func (ldb *LevelDbImpl) InitDb() {
|
||||
ldb.ldb, err = leveldb.OpenFile(config.Conf.LevelDb.DataPath, nil)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (ldb *LevelDbImpl) Get(key []byte) (res []byte, err error) {
|
||||
if len(key) == 0 {
|
||||
return nil, errors.New("key is null")
|
||||
}
|
||||
res, err = ldb.ldb.Get(key, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (ldb *LevelDbImpl) Put(key, value []byte) (err error) {
|
||||
if len(key) == 0 {
|
||||
return errors.New("key is null")
|
||||
}
|
||||
err = ldb.ldb.Put(key, value, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ldb *LevelDbImpl) List() (res map[string]string) {
|
||||
res = make(map[string]string)
|
||||
iter := ldb.ldb.NewIterator(nil, nil)
|
||||
for iter.Next() {
|
||||
res[string(iter.Key())] = string(iter.Value())
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (ldb *LevelDbImpl) ListPrefix(prefix []byte) (res map[string]string) {
|
||||
res = make(map[string]string)
|
||||
iter := ldb.ldb.NewIterator(util.BytesPrefix(prefix), nil)
|
||||
for iter.Next() {
|
||||
res[string(iter.Key())] = string(iter.Value())
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"honoka-chan/config"
|
||||
"strconv"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisCli *redis.Client
|
||||
RedisCtx = context.Background()
|
||||
)
|
||||
|
||||
func init() {
|
||||
RedisCli = redis.NewClient(&redis.Options{
|
||||
Addr: config.Conf.Redis.Host + ":" + config.Conf.Redis.Port,
|
||||
Password: config.Conf.Redis.Pass,
|
||||
DB: config.Conf.Redis.Db,
|
||||
})
|
||||
|
||||
_, err := RedisCli.Ping(RedisCtx).Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetUid(key string) (int, error) {
|
||||
uid, err := RedisCli.HGet(RedisCtx, "login_key_uid", key).Result()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
userId, err := strconv.Atoi(uid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if userId == 0 {
|
||||
return 0, errors.New("userId is 0")
|
||||
}
|
||||
|
||||
return userId, nil
|
||||
}
|
||||
|
||||
func MatchTokenUid(token, uid string) bool {
|
||||
res, err := RedisCli.HGet(RedisCtx, "token_uid", token).Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return res == uid
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package database
|
||||
|
||||
import "fmt"
|
||||
|
||||
func MatchTokenUid(token, uid string) bool {
|
||||
// ret := LevelDb.List()
|
||||
// for k, v := range ret {
|
||||
// fmt.Println(k, v)
|
||||
// fmt.Println()
|
||||
// }
|
||||
res, err := LevelDb.Get([]byte(token))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
|
||||
return string(res) == uid
|
||||
}
|
||||
Reference in New Issue
Block a user