Skip to content

Commit fced60c

Browse files
committed
feat: basic structure
1 parent b760605 commit fced60c

File tree

21 files changed

+519
-3
lines changed

21 files changed

+519
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ bin/*
2727
public/*.html
2828
public/assets/
2929
public/public/
30-
data/
30+
data/
31+
log/

bootstrap/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
What to do at startup, such as:
2+
- parse config
3+
- init store

bootstrap/config.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package bootstrap
2+
3+
import (
4+
"github.com/alist-org/alist/v3/cmd/args"
5+
"github.com/alist-org/alist/v3/conf"
6+
"github.com/alist-org/alist/v3/pkg/utils"
7+
"github.com/caarlos0/env/v6"
8+
log "github.com/sirupsen/logrus"
9+
"io/ioutil"
10+
"os"
11+
"path/filepath"
12+
)
13+
14+
func InitConfig() {
15+
log.Infof("reading config file: %s", args.Config)
16+
if !utils.Exists(args.Config) {
17+
log.Infof("config file not exists, creating default config file")
18+
_, err := utils.CreatNestedFile(args.Config)
19+
if err != nil {
20+
log.Fatalf("failed to create config file")
21+
}
22+
conf.Conf = conf.DefaultConfig()
23+
if !utils.WriteToJson(args.Config, conf.Conf) {
24+
log.Fatalf("failed to create default config file")
25+
}
26+
} else {
27+
configBytes, err := ioutil.ReadFile(args.Config)
28+
if err != nil {
29+
log.Fatalf("reading config file error:%s", err.Error())
30+
}
31+
conf.Conf = conf.DefaultConfig()
32+
err = utils.Json.Unmarshal(configBytes, conf.Conf)
33+
if err != nil {
34+
log.Fatalf("load config error: %s", err.Error())
35+
}
36+
log.Debugf("config:%+v", conf.Conf)
37+
// update config.json struct
38+
confBody, err := utils.Json.MarshalIndent(conf.Conf, "", " ")
39+
if err != nil {
40+
log.Fatalf("marshal config error:%s", err.Error())
41+
}
42+
err = ioutil.WriteFile(args.Config, confBody, 0777)
43+
if err != nil {
44+
log.Fatalf("update config struct error: %s", err.Error())
45+
}
46+
}
47+
if !conf.Conf.Force {
48+
confFromEnv()
49+
}
50+
err := os.RemoveAll(filepath.Join(conf.Conf.TempDir))
51+
if err != nil {
52+
log.Errorln("failed delete temp file:", err)
53+
}
54+
err = os.MkdirAll(conf.Conf.TempDir, 0700)
55+
if err != nil {
56+
log.Fatalf("create temp dir error: %s", err.Error())
57+
}
58+
log.Debugf("config: %+v", conf.Conf)
59+
}
60+
61+
func confFromEnv() {
62+
prefix := "ALIST_"
63+
if args.NoPrefix {
64+
prefix = ""
65+
}
66+
log.Infof("load config from env with prefix: %s", prefix)
67+
if err := env.Parse(conf.Conf, env.Options{
68+
Prefix: prefix,
69+
}); err != nil {
70+
log.Fatalf("load config from env error: %s", err.Error())
71+
}
72+
}

bootstrap/log.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package bootstrap
2+
3+
import (
4+
"github.com/alist-org/alist/v3/cmd/args"
5+
"github.com/alist-org/alist/v3/conf"
6+
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
7+
log "github.com/sirupsen/logrus"
8+
"time"
9+
)
10+
11+
func Log() {
12+
if args.Debug {
13+
log.SetLevel(log.DebugLevel)
14+
log.SetReportCaller(true)
15+
}
16+
log.SetFormatter(&log.TextFormatter{
17+
ForceColors: true,
18+
EnvironmentOverrideColors: true,
19+
TimestampFormat: "2006-01-02 15:04:05",
20+
FullTimestamp: true,
21+
})
22+
logConfig := conf.Conf.Log
23+
if !args.Debug && logConfig.Path != "" {
24+
var (
25+
writer *rotatelogs.RotateLogs
26+
err error
27+
)
28+
if logConfig.Name != "" {
29+
writer, err = rotatelogs.New(
30+
logConfig.Path,
31+
rotatelogs.WithLinkName(logConfig.Name),
32+
rotatelogs.WithRotationCount(logConfig.RotationCount),
33+
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
34+
)
35+
} else {
36+
writer, err = rotatelogs.New(
37+
logConfig.Path,
38+
rotatelogs.WithRotationCount(logConfig.RotationCount),
39+
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
40+
)
41+
}
42+
if err != nil {
43+
log.Fatalf("failed to create rotate log: %s", err)
44+
}
45+
log.SetOutput(writer)
46+
}
47+
log.Infof("init log...")
48+
}

cmd/alist.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"flag"
5+
"fmt"
6+
"github.com/alist-org/alist/v3/bootstrap"
7+
"github.com/alist-org/alist/v3/cmd/args"
8+
"github.com/alist-org/alist/v3/conf"
9+
"os"
10+
)
411

12+
func init() {
13+
flag.StringVar(&args.Config, "conf", "data/config.json", "config file")
14+
flag.BoolVar(&args.Debug, "debug", false, "start with debug mode")
15+
flag.BoolVar(&args.Version, "version", false, "print version info")
16+
flag.BoolVar(&args.Password, "password", false, "print current password")
17+
flag.BoolVar(&args.NoPrefix, "no-prefix", false, "disable env prefix")
18+
flag.Parse()
19+
}
20+
21+
func Init() {
22+
if args.Version {
23+
fmt.Printf("Built At: %s\nGo Version: %s\nAuthor: %s\nCommit ID: %s\nVersion: %s\nWebVersion: %s\n",
24+
conf.BuiltAt, conf.GoVersion, conf.GitAuthor, conf.GitCommit, conf.Version, conf.WebVersion)
25+
os.Exit(0)
26+
}
27+
bootstrap.InitConfig()
28+
bootstrap.Log()
29+
}
530
func main() {
6-
fmt.Println("Hello, world.")
31+
Init()
732
}

cmd/args/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package args
2+
3+
var (
4+
Config string // config file
5+
Debug bool
6+
Version bool
7+
Password bool
8+
NoPrefix bool
9+
)

conf/config.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package conf
2+
3+
type Database struct {
4+
Type string `json:"type" env:"DB_TYPE"`
5+
Host string `json:"host" env:"DB_HOST"`
6+
Port int `json:"port" env:"DB_PORT"`
7+
User string `json:"user" env:"DB_USER"`
8+
Password string `json:"password" env:"DB_PASS"`
9+
Name string `json:"name" env:"DB_NAME"`
10+
DBFile string `json:"db_file" env:"DB_FILE"`
11+
TablePrefix string `json:"table_prefix" env:"DB_TABLE_PREFIX"`
12+
SslMode string `json:"ssl_mode" env:"DB_SLL_MODE"`
13+
}
14+
15+
type Scheme struct {
16+
Https bool `json:"https" env:"HTTPS"`
17+
CertFile string `json:"cert_file" env:"CERT_FILE"`
18+
KeyFile string `json:"key_file" env:"KEY_FILE"`
19+
}
20+
21+
type CacheConfig struct {
22+
Expiration int64 `json:"expiration" env:"CACHE_EXPIRATION"`
23+
CleanupInterval int64 `json:"cleanup_interval" env:"CLEANUP_INTERVAL"`
24+
}
25+
26+
type LogConfig struct {
27+
Path string `json:"path" env:"LOG_PATH"`
28+
Name string `json:"name" env:"LOG_NAME"`
29+
RotationTime uint `json:"rotation_time" env:"LOG_TIME"`
30+
RotationCount uint `json:"rotation_count" env:"LOG_COUNT"`
31+
}
32+
33+
type Config struct {
34+
Force bool `json:"force"`
35+
Address string `json:"address" env:"ADDR"`
36+
Port int `json:"port" env:"PORT"`
37+
Assets string `json:"assets" env:"ASSETS"`
38+
Database Database `json:"database"`
39+
Scheme Scheme `json:"scheme"`
40+
Cache CacheConfig `json:"cache"`
41+
TempDir string `json:"temp_dir" env:"TEMP_DIR"`
42+
Log LogConfig `json:"log"`
43+
}
44+
45+
func DefaultConfig() *Config {
46+
return &Config{
47+
Address: "0.0.0.0",
48+
Port: 5244,
49+
Assets: "https://npm.elemecdn.com/alist-web@$version/dist",
50+
TempDir: "data/temp",
51+
Database: Database{
52+
Type: "sqlite3",
53+
Port: 0,
54+
TablePrefix: "x_",
55+
DBFile: "data/data.db",
56+
},
57+
Cache: CacheConfig{
58+
Expiration: 60,
59+
CleanupInterval: 120,
60+
},
61+
Log: LogConfig{
62+
Path: "log/%Y-%m-%d-%H:%M.log",
63+
Name: "log/log.log",
64+
RotationTime: 24,
65+
RotationCount: 5,
66+
},
67+
}
68+
}

conf/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package conf

conf/var.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package conf
2+
3+
var (
4+
BuiltAt string
5+
GoVersion string
6+
GitAuthor string
7+
GitCommit string
8+
Version string = "dev"
9+
WebVersion string
10+
)
11+
12+
var (
13+
Conf *Config
14+
)

drivers/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
storage driver

0 commit comments

Comments
 (0)