feat: initial setting items
This commit is contained in:
83
internal/bootstrap/config.go
Normal file
83
internal/bootstrap/config.go
Normal file
@ -0,0 +1,83 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
conf2 "github.com/alist-org/alist/v3/internal/conf"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/alist-org/alist/v3/cmd/args"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/caarlos0/env/v6"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
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.CreateNestedFile(args.Config)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create config file: %+v", err)
|
||||
}
|
||||
conf2.Conf = conf2.DefaultConfig()
|
||||
if !utils.WriteToJson(args.Config, conf2.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())
|
||||
}
|
||||
conf2.Conf = conf2.DefaultConfig()
|
||||
err = utils.Json.Unmarshal(configBytes, conf2.Conf)
|
||||
if err != nil {
|
||||
log.Fatalf("load config error: %s", err.Error())
|
||||
}
|
||||
log.Debugf("config:%+v", conf2.Conf)
|
||||
// update config.json struct
|
||||
confBody, err := utils.Json.MarshalIndent(conf2.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 !conf2.Conf.Force {
|
||||
confFromEnv()
|
||||
}
|
||||
// convert abs path
|
||||
var absPath string
|
||||
var err error
|
||||
if !filepath.IsAbs(conf2.Conf.TempDir) {
|
||||
absPath, err = filepath.Abs(conf2.Conf.TempDir)
|
||||
if err != nil {
|
||||
log.Fatalf("get abs path error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
conf2.Conf.TempDir = absPath
|
||||
err = os.RemoveAll(filepath.Join(conf2.Conf.TempDir))
|
||||
if err != nil {
|
||||
log.Errorln("failed delete temp file:", err)
|
||||
}
|
||||
err = os.MkdirAll(conf2.Conf.TempDir, 0700)
|
||||
if err != nil {
|
||||
log.Fatalf("create temp dir error: %s", err.Error())
|
||||
}
|
||||
log.Debugf("config: %+v", conf2.Conf)
|
||||
}
|
||||
|
||||
func confFromEnv() {
|
||||
prefix := "ALIST_"
|
||||
if args.NoPrefix {
|
||||
prefix = ""
|
||||
}
|
||||
log.Infof("load config from env with prefix: %s", prefix)
|
||||
if err := env.Parse(conf2.Conf, env.Options{
|
||||
Prefix: prefix,
|
||||
}); err != nil {
|
||||
log.Fatalf("load config from env error: %s", err.Error())
|
||||
}
|
||||
}
|
6
internal/bootstrap/data/data.go
Normal file
6
internal/bootstrap/data/data.go
Normal file
@ -0,0 +1,6 @@
|
||||
package data
|
||||
|
||||
func InitData() {
|
||||
initUser()
|
||||
initSettings()
|
||||
}
|
78
internal/bootstrap/data/setting.go
Normal file
78
internal/bootstrap/data/setting.go
Normal file
@ -0,0 +1,78 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/db"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var initialSettingItems = []model.SettingItem{
|
||||
// site settings
|
||||
{Key: "version", Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY},
|
||||
{Key: "site_title", Value: "AList", Type: conf.TypeString, Group: model.SITE},
|
||||
{Key: "site_logo", Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.SITE},
|
||||
{Key: "favicon", Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.SITE},
|
||||
{Key: "announcement", Value: "https://github.com/alist-org/alist", Type: conf.TypeString, Group: model.SITE},
|
||||
// style settings
|
||||
{Key: "icon_color", Value: "#1890ff", Type: conf.TypeString, Group: model.STYLE},
|
||||
// preview settings
|
||||
{Key: "text_types", Value: "txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp,tsx,vtt,srt,ass,rs,lrc", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||
{Key: "audio_types", Value: "mp3,flac,ogg,m4a,wav,opus", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||
{Key: "video_types", Value: "mp4,mkv,avi,mov,rmvb,webm,flv", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||
{Key: "proxy_types", Value: "m3u8", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||
{Key: "pdf_viewer_url", Value: "https://alist-org.github.io/pdf.js/web/viewer.html?file=$url", Type: conf.TypeString, Group: model.PREVIEW},
|
||||
{Key: "audio_autoplay", Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
|
||||
{Key: "video_autoplay", Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
|
||||
// global settings
|
||||
{Key: "hide_files", Value: "/\\/README.md/i", Type: conf.TypeText, Group: model.GLOBAL},
|
||||
{Key: "global_readme", Value: "This is global readme", Type: conf.TypeText, Group: model.GLOBAL},
|
||||
{Key: "customize_head", Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||
{Key: "customize_body", Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||
}
|
||||
|
||||
func initSettings() {
|
||||
// check deprecated
|
||||
settings, err := db.GetSettings()
|
||||
if err != nil {
|
||||
log.Fatalf("failed get settings: %+v", err)
|
||||
}
|
||||
for i := range settings {
|
||||
if !isActive(settings[i].Key) {
|
||||
settings[i].Flag = model.DEPRECATED
|
||||
}
|
||||
}
|
||||
if settings != nil && len(settings) > 0 {
|
||||
err = db.SaveSettings(settings)
|
||||
if err != nil {
|
||||
log.Fatalf("failed save settings: %+v", err)
|
||||
}
|
||||
}
|
||||
// insert new items
|
||||
for i, _ := range initialSettingItems {
|
||||
v := initialSettingItems[i]
|
||||
_, err := db.GetSettingByKey(v.Key)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = db.SaveSetting(v)
|
||||
if err != nil {
|
||||
log.Fatalf("failed create setting: %+v", err)
|
||||
}
|
||||
} else {
|
||||
log.Fatalf("failed get setting: %+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isActive(key string) bool {
|
||||
for _, item := range initialSettingItems {
|
||||
if item.Key == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
54
internal/bootstrap/data/user.go
Normal file
54
internal/bootstrap/data/user.go
Normal file
@ -0,0 +1,54 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/cmd/args"
|
||||
"github.com/alist-org/alist/v3/internal/db"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/pkg/utils/random"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func initUser() {
|
||||
admin, err := db.GetAdmin()
|
||||
adminPassword := random.String(8)
|
||||
if args.Dev {
|
||||
adminPassword = "admin"
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
admin = &model.User{
|
||||
Username: "admin",
|
||||
Password: adminPassword,
|
||||
Role: model.ADMIN,
|
||||
BasePath: "/",
|
||||
Webdav: true,
|
||||
}
|
||||
if err := db.CreateUser(admin); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
guest, err := db.GetGuest()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
guest = &model.User{
|
||||
Username: "guest",
|
||||
Password: "guest",
|
||||
ReadOnly: true,
|
||||
Webdav: true,
|
||||
Role: model.GUEST,
|
||||
BasePath: "/",
|
||||
}
|
||||
if err := db.CreateUser(guest); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
log.Infof("admin password: %+v", admin.Password)
|
||||
}
|
39
internal/bootstrap/db.go
Normal file
39
internal/bootstrap/db.go
Normal file
@ -0,0 +1,39 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/cmd/args"
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/db"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
stdlog "log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitDB() {
|
||||
newLogger := logger.New(
|
||||
stdlog.New(log.StandardLogger().Out, "\r\n", stdlog.LstdFlags),
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second,
|
||||
LogLevel: logger.Silent,
|
||||
IgnoreRecordNotFoundError: true,
|
||||
Colorful: true,
|
||||
},
|
||||
)
|
||||
gormConfig := &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: conf.Conf.Database.TablePrefix,
|
||||
},
|
||||
Logger: newLogger,
|
||||
}
|
||||
if args.Dev {
|
||||
dB, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), gormConfig)
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
db.Init(dB)
|
||||
}
|
||||
}
|
58
internal/bootstrap/log.go
Normal file
58
internal/bootstrap/log.go
Normal file
@ -0,0 +1,58 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/alist-org/alist/v3/cmd/args"
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
ForceColors: true,
|
||||
EnvironmentOverrideColors: true,
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
FullTimestamp: true,
|
||||
})
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
|
||||
func Log() {
|
||||
log.SetOutput(logrus.StandardLogger().Out)
|
||||
if args.Debug || args.Dev {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
logrus.SetReportCaller(true)
|
||||
} else {
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
logrus.SetReportCaller(false)
|
||||
}
|
||||
logConfig := conf.Conf.Log
|
||||
if logConfig.Enable {
|
||||
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 {
|
||||
logrus.Fatalf("failed to create rotate logrus: %s", err)
|
||||
}
|
||||
logrus.SetOutput(writer)
|
||||
}
|
||||
logrus.Infof("init logrus...")
|
||||
}
|
Reference in New Issue
Block a user