feat(server): add flag for skipping indexing in launching
This commit is contained in:
parent
0cce1bd090
commit
aeb9d26bbd
4 changed files with 46 additions and 21 deletions
|
@ -51,6 +51,7 @@ type ServerCfg struct {
|
||||||
MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
|
MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
|
||||||
PublicPath string `json:"publicPath" yaml:"publicPath"`
|
PublicPath string `json:"publicPath" yaml:"publicPath"`
|
||||||
SearchResultLimit int `json:"searchResultLimit" yaml:"searchResultLimit"`
|
SearchResultLimit int `json:"searchResultLimit" yaml:"searchResultLimit"`
|
||||||
|
InitFileIndex bool `json:"initFileIndex" yaml:"initFileIndex"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkerPoolCfg struct {
|
type WorkerPoolCfg struct {
|
||||||
|
@ -116,6 +117,7 @@ func DefaultConfigStruct() *Config {
|
||||||
MaxHeaderBytes: 512,
|
MaxHeaderBytes: 512,
|
||||||
PublicPath: "static/public",
|
PublicPath: "static/public",
|
||||||
SearchResultLimit: 16,
|
SearchResultLimit: 16,
|
||||||
|
InitFileIndex: true,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 1024,
|
QueueSize: 1024,
|
||||||
|
|
|
@ -134,6 +134,7 @@ func TestLoadCfg(t *testing.T) {
|
||||||
MaxHeaderBytes: 1,
|
MaxHeaderBytes: 1,
|
||||||
PublicPath: "1",
|
PublicPath: "1",
|
||||||
SearchResultLimit: 16,
|
SearchResultLimit: 16,
|
||||||
|
InitFileIndex: true,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 1,
|
QueueSize: 1,
|
||||||
|
@ -201,6 +202,7 @@ func TestLoadCfg(t *testing.T) {
|
||||||
MaxHeaderBytes: 4,
|
MaxHeaderBytes: 4,
|
||||||
PublicPath: "4",
|
PublicPath: "4",
|
||||||
SearchResultLimit: 16,
|
SearchResultLimit: 16,
|
||||||
|
InitFileIndex: true,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 4,
|
QueueSize: 4,
|
||||||
|
@ -270,6 +272,7 @@ func TestLoadCfg(t *testing.T) {
|
||||||
MaxHeaderBytes: 4,
|
MaxHeaderBytes: 4,
|
||||||
PublicPath: "4",
|
PublicPath: "4",
|
||||||
SearchResultLimit: 16,
|
SearchResultLimit: 16,
|
||||||
|
InitFileIndex: true,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 4,
|
QueueSize: 4,
|
||||||
|
@ -339,6 +342,7 @@ func TestLoadCfg(t *testing.T) {
|
||||||
MaxHeaderBytes: 4,
|
MaxHeaderBytes: 4,
|
||||||
PublicPath: "4",
|
PublicPath: "4",
|
||||||
SearchResultLimit: 16,
|
SearchResultLimit: 16,
|
||||||
|
InitFileIndex: true,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 4,
|
QueueSize: 4,
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -123,6 +124,7 @@ func mkRoot(rootPath string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDeps(cfg gocfg.ICfg) *depidx.Deps {
|
func initDeps(cfg gocfg.ICfg) *depidx.Deps {
|
||||||
|
var err error
|
||||||
logger := initLogger(cfg)
|
logger := initLogger(cfg)
|
||||||
|
|
||||||
secret, ok := cfg.String("ENV.TOKENSECRET")
|
secret, ok := cfg.String("ENV.TOKENSECRET")
|
||||||
|
@ -141,25 +143,6 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
|
||||||
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)
|
||||||
|
|
||||||
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
|
|
||||||
fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit)
|
|
||||||
|
|
||||||
indexInfo, err := filesystem.Stat(fileIndexPath)
|
|
||||||
if err != nil {
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
panic(fmt.Sprintf("failed to detect file index: %s", err))
|
|
||||||
} else {
|
|
||||||
logger.Info("warning: no file index found")
|
|
||||||
}
|
|
||||||
} else if indexInfo.IsDir() {
|
|
||||||
panic(fmt.Sprintf("file index is folder, not file: %s", fileIndexPath))
|
|
||||||
} else {
|
|
||||||
err = fileIndex.ReadFrom(fileIndexPath)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("failed to load file index: %s", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dbPath := cfg.GrabString("Db.DbPath")
|
dbPath := cfg.GrabString("Db.DbPath")
|
||||||
dbDir := filepath.Dir(dbPath)
|
dbDir := filepath.Dir(dbPath)
|
||||||
if err = filesystem.MkdirAll(dbDir); err != nil {
|
if err = filesystem.MkdirAll(dbDir); err != nil {
|
||||||
|
@ -216,7 +199,6 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
|
||||||
deps.SetID(ider)
|
deps.SetID(ider)
|
||||||
deps.SetLog(logger)
|
deps.SetLog(logger)
|
||||||
deps.SetLimiter(limiter)
|
deps.SetLimiter(limiter)
|
||||||
deps.SetIFileIndex(fileIndex)
|
|
||||||
|
|
||||||
queueSize := cfg.GrabInt("Workers.QueueSize")
|
queueSize := cfg.GrabInt("Workers.QueueSize")
|
||||||
sleepCyc := cfg.GrabInt("Workers.SleepCyc")
|
sleepCyc := cfg.GrabInt("Workers.SleepCyc")
|
||||||
|
@ -226,6 +208,42 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
|
||||||
workers.Start()
|
workers.Start()
|
||||||
deps.SetWorkers(workers)
|
deps.SetWorkers(workers)
|
||||||
|
|
||||||
|
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
|
||||||
|
initFileIndex := cfg.GrabBool("Server.InitFileIndex")
|
||||||
|
fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit)
|
||||||
|
indexInfo, err := filesystem.Stat(fileIndexPath)
|
||||||
|
inited := false
|
||||||
|
if err != nil {
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
logger.Infof("failed to detect file index: %s", err)
|
||||||
|
} else {
|
||||||
|
logger.Info("warning: no file index found")
|
||||||
|
}
|
||||||
|
} else if indexInfo.IsDir() {
|
||||||
|
logger.Infof("file index is folder, not file: %s", fileIndexPath)
|
||||||
|
} else {
|
||||||
|
err = fileIndex.ReadFrom(fileIndexPath)
|
||||||
|
if err != nil {
|
||||||
|
logger.Infof("failed to load file index: %s", err)
|
||||||
|
} else {
|
||||||
|
inited = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !inited && initFileIndex {
|
||||||
|
msg, _ := json.Marshal(fileshdr.IndexingParams{})
|
||||||
|
err = deps.Workers().TryPut(
|
||||||
|
localworker.NewMsg(
|
||||||
|
deps.ID().Gen(),
|
||||||
|
map[string]string{localworker.MsgTypeKey: fileshdr.MsgTypeIndexing},
|
||||||
|
string(msg),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
logger.Infof("failed to reindex file index: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deps.SetIFileIndex(fileIndex)
|
||||||
|
|
||||||
return deps
|
return deps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,8 @@ func TestFileHandlers(t *testing.T) {
|
||||||
},
|
},
|
||||||
"server": {
|
"server": {
|
||||||
"debug": true,
|
"debug": true,
|
||||||
"host": "127.0.0.1"
|
"host": "127.0.0.1",
|
||||||
|
"initFileIndex": true
|
||||||
},
|
},
|
||||||
"fs": {
|
"fs": {
|
||||||
"root": "tmpTestData"
|
"root": "tmpTestData"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue