Reorganize directory structures

Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
2025-08-05 20:06:35 +08:00
parent d666470272
commit adf2a72630
2087 changed files with 637 additions and 603 deletions
+63
View File
@@ -0,0 +1,63 @@
package db
import (
"fmt"
"honoka-chan/pkg/utils"
"os"
_ "modernc.org/sqlite"
"xorm.io/xorm"
)
var (
DB *Instance
ExampleDb = "assets/data.example.db"
MainDb = "assets/main.db"
UserDb = "assets/data.db"
MainEng *xorm.Engine
UserEng *xorm.Engine
)
func init() {
DB = GetInstance()
_, err := os.Stat(UserDb)
if err != nil {
utils.WriteAllText(UserDb, utils.ReadAllText(ExampleDb))
}
eng, err := xorm.NewEngine("sqlite", MainDb)
if err != nil {
panic(err)
}
err = eng.Ping()
if err != nil {
panic(err)
}
eng.SetMaxOpenConns(10)
eng.SetMaxIdleConns(5)
MainEng = eng
eng, err = xorm.NewEngine("sqlite", UserDb)
if err != nil {
panic(err)
}
err = eng.Ping()
if err != nil {
panic(err)
}
eng.SetMaxOpenConns(10)
eng.SetMaxIdleConns(5)
UserEng = eng
}
func MatchTokenUid(token, uid string) bool {
res, err := DB.Get([]byte(uid))
if err != nil {
fmt.Println(err)
return false
}
return string(res) == token
}
+58
View File
@@ -0,0 +1,58 @@
package db
import (
"errors"
"sync"
"github.com/syndtr/goleveldb/leveldb"
)
var (
once sync.Once
)
type Instance struct {
db *leveldb.DB
mu sync.Mutex
}
func GetInstance() *Instance {
in := Instance{}
once.Do(func() {
var err error
in.db, err = leveldb.OpenFile("./data/honoka-chan.db", nil)
if err != nil {
panic(err)
}
})
return &in
}
func (in *Instance) Close() error {
in.mu.Lock()
defer in.mu.Unlock()
if in.db != nil {
return in.db.Close()
}
return nil
}
func (in *Instance) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errors.New("empty key")
}
res, err := in.db.Get(key, nil)
if err == leveldb.ErrNotFound {
return nil, nil
} else if err != nil {
return nil, err
}
return res, nil
}
func (in *Instance) Set(key, value []byte) error {
if len(key) == 0 {
return errors.New("empty key")
}
return in.db.Put(key, value, nil)
}