feat: basic structure
This commit is contained in:
3
bootstrap/README.md
Normal file
3
bootstrap/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
What to do at startup, such as:
|
||||
- parse config
|
||||
- init store
|
72
bootstrap/config.go
Normal file
72
bootstrap/config.go
Normal file
@ -0,0 +1,72 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/cmd/args"
|
||||
"github.com/alist-org/alist/v3/conf"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/caarlos0/env/v6"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func InitConfig() {
|
||||
log.Infof("reading config file: %s", args.Config)
|
||||
if !utils.Exists(args.Config) {
|
||||
log.Infof("config file not exists, creating default config file")
|
||||
_, err := utils.CreatNestedFile(args.Config)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create config file")
|
||||
}
|
||||
conf.Conf = conf.DefaultConfig()
|
||||
if !utils.WriteToJson(args.Config, conf.Conf) {
|
||||
log.Fatalf("failed to create default config file")
|
||||
}
|
||||
} else {
|
||||
configBytes, err := ioutil.ReadFile(args.Config)
|
||||
if err != nil {
|
||||
log.Fatalf("reading config file error:%s", err.Error())
|
||||
}
|
||||
conf.Conf = conf.DefaultConfig()
|
||||
err = utils.Json.Unmarshal(configBytes, conf.Conf)
|
||||
if err != nil {
|
||||
log.Fatalf("load config error: %s", err.Error())
|
||||
}
|
||||
log.Debugf("config:%+v", conf.Conf)
|
||||
// update config.json struct
|
||||
confBody, err := utils.Json.MarshalIndent(conf.Conf, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("marshal config error:%s", err.Error())
|
||||
}
|
||||
err = ioutil.WriteFile(args.Config, confBody, 0777)
|
||||
if err != nil {
|
||||
log.Fatalf("update config struct error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
if !conf.Conf.Force {
|
||||
confFromEnv()
|
||||
}
|
||||
err := os.RemoveAll(filepath.Join(conf.Conf.TempDir))
|
||||
if err != nil {
|
||||
log.Errorln("failed delete temp file:", err)
|
||||
}
|
||||
err = os.MkdirAll(conf.Conf.TempDir, 0700)
|
||||
if err != nil {
|
||||
log.Fatalf("create temp dir error: %s", err.Error())
|
||||
}
|
||||
log.Debugf("config: %+v", conf.Conf)
|
||||
}
|
||||
|
||||
func confFromEnv() {
|
||||
prefix := "ALIST_"
|
||||
if args.NoPrefix {
|
||||
prefix = ""
|
||||
}
|
||||
log.Infof("load config from env with prefix: %s", prefix)
|
||||
if err := env.Parse(conf.Conf, env.Options{
|
||||
Prefix: prefix,
|
||||
}); err != nil {
|
||||
log.Fatalf("load config from env error: %s", err.Error())
|
||||
}
|
||||
}
|
48
bootstrap/log.go
Normal file
48
bootstrap/log.go
Normal file
@ -0,0 +1,48 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/cmd/args"
|
||||
"github.com/alist-org/alist/v3/conf"
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Log() {
|
||||
if args.Debug {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.SetReportCaller(true)
|
||||
}
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
ForceColors: true,
|
||||
EnvironmentOverrideColors: true,
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
FullTimestamp: true,
|
||||
})
|
||||
logConfig := conf.Conf.Log
|
||||
if !args.Debug && logConfig.Path != "" {
|
||||
var (
|
||||
writer *rotatelogs.RotateLogs
|
||||
err error
|
||||
)
|
||||
if logConfig.Name != "" {
|
||||
writer, err = rotatelogs.New(
|
||||
logConfig.Path,
|
||||
rotatelogs.WithLinkName(logConfig.Name),
|
||||
rotatelogs.WithRotationCount(logConfig.RotationCount),
|
||||
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
|
||||
)
|
||||
} else {
|
||||
writer, err = rotatelogs.New(
|
||||
logConfig.Path,
|
||||
rotatelogs.WithRotationCount(logConfig.RotationCount),
|
||||
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
|
||||
)
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create rotate log: %s", err)
|
||||
}
|
||||
log.SetOutput(writer)
|
||||
}
|
||||
log.Infof("init log...")
|
||||
}
|
Reference in New Issue
Block a user