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 InitUserTable(ctx context.Context, rootName, rootPwd string) error
InitFileTables(ctx context.Context) error InitFileTables(ctx context.Context) error
InitConfigTable(ctx context.Context, cfg *SiteConfig) error InitConfigTable(ctx context.Context, cfg *SiteConfig) error
Close() error
IDBLockable IDBLockable
IUserDB IUserDB
IFileDB IFileDB

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"crypto/sha1" "crypto/sha1"
// "encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@ -25,11 +24,7 @@ import (
"github.com/ihexxa/quickshare/src/cryptoutil/jwt" "github.com/ihexxa/quickshare/src/cryptoutil/jwt"
"github.com/ihexxa/quickshare/src/db" "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/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/depidx"
"github.com/ihexxa/quickshare/src/fs" "github.com/ihexxa/quickshare/src/fs"
"github.com/ihexxa/quickshare/src/fs/local" "github.com/ihexxa/quickshare/src/fs/local"
@ -39,7 +34,6 @@ import (
"github.com/ihexxa/quickshare/src/idgen/simpleidgen" "github.com/ihexxa/quickshare/src/idgen/simpleidgen"
"github.com/ihexxa/quickshare/src/iolimiter" "github.com/ihexxa/quickshare/src/iolimiter"
"github.com/ihexxa/quickshare/src/kvstore" "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/search/fileindex"
"github.com/ihexxa/quickshare/src/worker/localworker" "github.com/ihexxa/quickshare/src/worker/localworker"
qsstatic "github.com/ihexxa/quickshare/static" 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) filesystem := local.NewLocalFS(rootPath, 0660, opensLimit, openTTL, readerTTL, ider)
jwtEncDec := jwt.NewJWTEncDec(secret) 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) quickshareDb, adminName, err := initDB(cfg, filesystem)
if err != nil { if err != nil {
logger.Errorf("failed to init DB: %s", err) logger.Errorf("failed to init DB: %s", err)
@ -156,11 +132,6 @@ func initDeps(cfg gocfg.ICfg) (*depidx.Deps, string) {
deps.SetDB(quickshareDb) deps.SetDB(quickshareDb)
deps.SetFS(filesystem) deps.SetFS(filesystem)
deps.SetToken(jwtEncDec) deps.SetToken(jwtEncDec)
// deps.SetKV(kv)
// deps.SetUsers(users)
// deps.SetFileInfos(fileInfos)
// deps.SetSiteStore(siteStore)
// deps.SetBoltStore(boltDB)
deps.SetID(ider) deps.SetID(ider)
deps.SetLog(logger) deps.SetLog(logger)
deps.SetLimiter(limiter) deps.SetLimiter(limiter)
@ -174,7 +145,6 @@ func initDeps(cfg gocfg.ICfg) (*depidx.Deps, string) {
deps.SetWorkers(workers) deps.SetWorkers(workers)
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit") searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
// initFileIndex := cfg.GrabBool("Server.InitFileIndex")
fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit) fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit)
indexInfo, err := filesystem.Stat(fileIndexPath) indexInfo, err := filesystem.Stat(fileIndexPath)
indexInited := false 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) return nil, fmt.Errorf("new setting service error: %w", err)
} }
// middleware // middlewares
router.Use(userHdrs.AuthN()) router.Use(userHdrs.AuthN())
router.Use(userHdrs.APIAccessControl()) 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)) router.Use(static.Serve("/", esFS))
} }
// handler // handlers
v1 := router.Group("/v1") v1 := router.Group("/v1")
usersAPI := v1.Group("/users") 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.PATCH("/preferences", userHdrs.SetPreferences)
usersAPI.PUT("/used-space", userHdrs.ResetUsedSpace) usersAPI.PUT("/used-space", userHdrs.ResetUsedSpace)
// rolesAPI := v1.Group("/roles") rolesAPI := v1.Group("/roles")
// rolesAPI.POST("/", userHdrs.AddRole) // rolesAPI.POST("/", userHdrs.AddRole)
// rolesAPI.DELETE("/", userHdrs.DelRole) // rolesAPI.DELETE("/", userHdrs.DelRole)
// rolesAPI.GET("/list", userHdrs.ListRoles) rolesAPI.GET("/list", userHdrs.ListRoles)
captchaAPI := v1.Group("/captchas") captchaAPI := v1.Group("/captchas")
captchaAPI.GET("/", userHdrs.GetCaptchaID) 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.Log().Errorf("failed to persist file index: %s", err)
} }
s.deps.Workers().Stop() 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() s.deps.Log().Sync()
return s.server.Shutdown(context.Background()) return nil
} }
func (s *Server) depsFS() fs.ISimpleFS { func (s *Server) depsFS() fs.ISimpleFS {

View file

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

Binary file not shown.