fix(server): 1.replace bolt with sqlite in config 2.add hardcoded listing roles

This commit is contained in:
hexxa 2022-09-16 11:28:51 +08:00 committed by Hexxa
parent d97451653e
commit 4f6683de03
9 changed files with 61 additions and 85 deletions

View file

@ -30,6 +30,7 @@ type IDBQuickshare interface {
InitUserTable(ctx context.Context, rootName, rootPwd string) error
InitFileTables(ctx context.Context) error
InitConfigTable(ctx context.Context, cfg *SiteConfig) error
Close() error
IDBLockable
IUserDB
IFileDB

View file

@ -37,6 +37,10 @@ func NewSQLiteStore(db db.IDB) (*SQLiteStore, error) {
}, nil
}
func (st *SQLiteStore) Close() error {
return st.db.Close()
}
func (st *SQLiteStore) Lock() {
st.mtx.Lock()
}

View file

@ -6,10 +6,7 @@ import (
"github.com/ihexxa/quickshare/src/cron"
"github.com/ihexxa/quickshare/src/cryptoutil"
// "github.com/ihexxa/quickshare/src/db/boltstore"
// "github.com/ihexxa/quickshare/src/db/fileinfostore"
"github.com/ihexxa/quickshare/src/db"
// "github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/fs"
"github.com/ihexxa/quickshare/src/idgen"
"github.com/ihexxa/quickshare/src/iolimiter"
@ -27,13 +24,9 @@ type IUploader interface {
}
type Deps struct {
fs fs.ISimpleFS
token cryptoutil.ITokenEncDec
kv kvstore.IKVStore
// users db.IUserDB
// fileInfos db.IFileDB
// siteStore db.IConfigDB
// boltStore *boltstore.BoltStore
fs fs.ISimpleFS
token cryptoutil.ITokenEncDec
kv kvstore.IKVStore
id idgen.IIDGen
logger *zap.SugaredLogger
limiter iolimiter.ILimiter
@ -115,14 +108,6 @@ func (deps *Deps) SetWorkers(workers worker.IWorkerPool) {
deps.workers = workers
}
// func (deps *Deps) BoltStore() *boltstore.BoltStore {
// return deps.boltStore
// }
// func (deps *Deps) SetBoltStore(boltStore *boltstore.BoltStore) {
// deps.boltStore = boltStore
// }
func (deps *Deps) Cron() cron.ICron {
return deps.cron
}

View file

@ -571,13 +571,13 @@ type ListRolesResp struct {
}
func (h *MultiUsersSvc) ListRoles(c *gin.Context) {
roles, err := h.deps.Users().ListRoles()
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(200, &ListRolesResp{Roles: roles})
// TODO: currently roles are hardcoded
c.JSON(200, &ListRolesResp{
Roles: map[string]bool{
db.AdminRole: true,
db.UserRole: true,
db.VisitorRole: true,
}})
}
func (h *MultiUsersSvc) getUserInfo(c *gin.Context) (map[string]string, error) {

View file

@ -2,6 +2,7 @@ package server
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
@ -9,9 +10,7 @@ import (
"strings"
"github.com/ihexxa/gocfg"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
"github.com/ihexxa/quickshare/src/db/rdb/sqlite"
)
type Args struct {
@ -62,20 +61,25 @@ func LoadCfg(ctx context.Context, args *Args) (*gocfg.Cfg, error) {
}
func mergeDbConfig(ctx context.Context, cfg *gocfg.Cfg, dbPath string) (*gocfg.Cfg, error) {
kv := boltdbpvd.New(dbPath, 1024)
defer kv.Close()
siteStore, err := sitestore.NewSiteStore(kv)
if err != nil {
return nil, fmt.Errorf("fail to new site config store: %s", err)
if dbPath == "" {
return cfg, nil
}
clientCfg, err := siteStore.GetCfg(ctx)
sqliteDB, err := sqlite.NewSQLite(dbPath)
if err != nil {
if errors.Is(err, sitestore.ErrNotFound) {
return nil, fmt.Errorf("failed to init sqlite db(%s): %w", dbPath, err)
}
dbQuickshare, err := sqlite.NewSQLiteStore(sqliteDB)
if err != nil {
return nil, fmt.Errorf("failed to create quickshare db(%s): %w", dbPath, err)
}
clientCfg, err := dbQuickshare.GetCfg(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return cfg, nil
}
return nil, err
return nil, fmt.Errorf("get db config error(%s): %w", dbPath, err)
}
clientCfgBytes, err := json.Marshal(clientCfg)

View file

@ -25,7 +25,7 @@ func TestLoadCfg(t *testing.T) {
&Args{
Host: "",
Port: 0,
DbPath: "testdata/test_quickshare.db",
DbPath: "testdata/quickshare.sqlite",
Configs: []string{},
},
// default config + db + config_1 (dbPath is from config_1)
@ -39,7 +39,7 @@ func TestLoadCfg(t *testing.T) {
&Args{
Host: "",
Port: 0,
DbPath: "testdata/test_quickshare.db",
DbPath: "testdata/quickshare.sqlite",
Configs: []string{"testdata/config_1.yml"},
},
// default config + db + config_1 + config_4
@ -61,11 +61,11 @@ func TestLoadCfg(t *testing.T) {
"testdata/config_partial_db.yml",
},
},
// default config + db + config_1 + config_4 + config_partial_users.yml + config_partial_db.yml + args(db)
// // default config + db + config_1 + config_4 + config_partial_users.yml + config_partial_db.yml + args(db)
&Args{
Host: "",
Port: 0,
DbPath: "testdata/test_quickshare.db",
DbPath: "testdata/quickshare.sqlite",
Configs: []string{
"testdata/config_1.yml",
"testdata/config_4.yml",
@ -81,13 +81,13 @@ func TestLoadCfg(t *testing.T) {
cfgDBOnly.Site = &db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: "Quickshare",
SiteDesc: "quick and simple file sharing",
SiteDesc: "Quick and simple file sharing",
Bg: &db.BgConfig{
Url: "/static/img/textured_paper.png",
Url: "",
Repeat: "repeat",
Position: "center",
Align: "fixed",
BgColor: "#ccc",
BgColor: "",
},
},
}
@ -145,18 +145,18 @@ func TestLoadCfg(t *testing.T) {
Site: &db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: "Quickshare",
SiteDesc: "quick and simple file sharing",
SiteDesc: "Quick and simple file sharing",
Bg: &db.BgConfig{
Url: "/static/img/textured_paper.png",
Url: "",
Repeat: "repeat",
Position: "center",
Align: "fixed",
BgColor: "#ccc",
BgColor: "",
},
},
},
Db: &DbConfig{
DbPath: "testdata/test_quickshare.db",
DbPath: "testdata/quickshare.sqlite",
},
}

View file

@ -4,7 +4,6 @@ import (
"context"
"crypto/rand"
"crypto/sha1"
// "encoding/json"
"errors"
"fmt"
"net/http"
@ -25,11 +24,7 @@ import (
"github.com/ihexxa/quickshare/src/cryptoutil/jwt"
"github.com/ihexxa/quickshare/src/db"
// "github.com/ihexxa/quickshare/src/db/boltstore"
// "github.com/ihexxa/quickshare/src/db/fileinfostore"
"github.com/ihexxa/quickshare/src/db/rdb/sqlite"
// "github.com/ihexxa/quickshare/src/db/sitestore"
// "github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/depidx"
"github.com/ihexxa/quickshare/src/fs"
"github.com/ihexxa/quickshare/src/fs/local"
@ -39,7 +34,6 @@ import (
"github.com/ihexxa/quickshare/src/idgen/simpleidgen"
"github.com/ihexxa/quickshare/src/iolimiter"
"github.com/ihexxa/quickshare/src/kvstore"
// "github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
"github.com/ihexxa/quickshare/src/search/fileindex"
"github.com/ihexxa/quickshare/src/worker/localworker"
qsstatic "github.com/ihexxa/quickshare/static"
@ -124,24 +118,6 @@ func initDeps(cfg gocfg.ICfg) (*depidx.Deps, string) {
filesystem := local.NewLocalFS(rootPath, 0660, opensLimit, openTTL, readerTTL, ider)
jwtEncDec := jwt.NewJWTEncDec(secret)
// kv := boltdbpvd.New(dbPath, 1024)
// users, err := userstore.NewKVUserStore(kv)
// if err != nil {
// panic(fmt.Sprintf("failed to init user store: %s", err))
// }
// fileInfos, err := fileinfostore.NewFileInfoStore(kv)
// if err != nil {
// panic(fmt.Sprintf("failed to init file info store: %s", err))
// }
// siteStore, err := sitestore.NewSiteStore(kv)
// if err != nil {
// panic(fmt.Sprintf("failed to init site config store: %s", err))
// }
// boltDB, err := boltstore.NewBoltStore(kv.Bolt())
// if err != nil {
// panic(fmt.Sprintf("failed to init bolt store: %s", err))
// }
quickshareDb, adminName, err := initDB(cfg, filesystem)
if err != nil {
logger.Errorf("failed to init DB: %s", err)
@ -156,11 +132,6 @@ func initDeps(cfg gocfg.ICfg) (*depidx.Deps, string) {
deps.SetDB(quickshareDb)
deps.SetFS(filesystem)
deps.SetToken(jwtEncDec)
// deps.SetKV(kv)
// deps.SetUsers(users)
// deps.SetFileInfos(fileInfos)
// deps.SetSiteStore(siteStore)
// deps.SetBoltStore(boltDB)
deps.SetID(ider)
deps.SetLog(logger)
deps.SetLimiter(limiter)
@ -174,7 +145,6 @@ func initDeps(cfg gocfg.ICfg) (*depidx.Deps, string) {
deps.SetWorkers(workers)
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
// initFileIndex := cfg.GrabBool("Server.InitFileIndex")
fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit)
indexInfo, err := filesystem.Stat(fileIndexPath)
indexInited := false
@ -293,7 +263,7 @@ func initHandlers(router *gin.Engine, adminName string, cfg gocfg.ICfg, deps *de
return nil, fmt.Errorf("new setting service error: %w", err)
}
// middleware
// middlewares
router.Use(userHdrs.AuthN())
router.Use(userHdrs.APIAccessControl())
@ -311,7 +281,7 @@ func initHandlers(router *gin.Engine, adminName string, cfg gocfg.ICfg, deps *de
router.Use(static.Serve("/", esFS))
}
// handler
// handlers
v1 := router.Group("/v1")
usersAPI := v1.Group("/users")
@ -328,10 +298,10 @@ func initHandlers(router *gin.Engine, adminName string, cfg gocfg.ICfg, deps *de
usersAPI.PATCH("/preferences", userHdrs.SetPreferences)
usersAPI.PUT("/used-space", userHdrs.ResetUsedSpace)
// rolesAPI := v1.Group("/roles")
rolesAPI := v1.Group("/roles")
// rolesAPI.POST("/", userHdrs.AddRole)
// rolesAPI.DELETE("/", userHdrs.DelRole)
// rolesAPI.GET("/list", userHdrs.ListRoles)
rolesAPI.GET("/list", userHdrs.ListRoles)
captchaAPI := v1.Group("/captchas")
captchaAPI.GET("/", userHdrs.GetCaptchaID)
@ -433,9 +403,21 @@ func (s *Server) Shutdown() error {
s.deps.Log().Errorf("failed to persist file index: %s", err)
}
s.deps.Workers().Stop()
s.deps.FS().Close()
err = s.deps.FS().Close()
if err != nil {
s.deps.Log().Errorf("failed to close file system: %s", err)
}
err = s.deps.DB().Close()
if err != nil {
s.deps.Log().Errorf("failed to close database: %s", err)
}
err = s.server.Shutdown(context.Background())
if err != nil {
s.deps.Log().Errorf("failed to shutdown server: %s", err)
}
s.deps.Log().Sync()
return s.server.Shutdown(context.Background())
return nil
}
func (s *Server) depsFS() fs.ISimpleFS {

View file

@ -48,5 +48,5 @@ workers:
# align: "1"
# bgColor: "1"
db:
dbPath: "testdata/test_quickshare.db"
dbPath: "testdata/quickshare.sqlite"

Binary file not shown.