diff --git a/bootstrap/conf.go b/bootstrap/conf.go index f4d1c904..06433990 100644 --- a/bootstrap/conf.go +++ b/bootstrap/conf.go @@ -3,6 +3,7 @@ package bootstrap import ( "github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/utils" + "github.com/caarlos0/env/v6" log "github.com/sirupsen/logrus" "io/ioutil" "os" @@ -42,8 +43,18 @@ func InitConf() { log.Fatalf("update config struct error: %s", err.Error()) } } + if !conf.Conf.Force { + confFromEnv() + } 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() { + if err := env.Parse(conf.Conf); err != nil { + log.Fatalf("load config from env error: %s", err.Error()) + } } diff --git a/conf/config.go b/conf/config.go index 75eace53..991d1f40 100644 --- a/conf/config.go +++ b/conf/config.go @@ -1,36 +1,37 @@ package conf type Database struct { - Type string `json:"type"` - User string `json:"user"` - Password string `json:"password"` - Host string `json:"host"` - Port int `json:"port"` - Name string `json:"name"` - TablePrefix string `json:"table_prefix"` - DBFile string `json:"db_file"` - SslMode string `json:"ssl_mode"` + Type string `json:"type" env:"A_LIST_DB_TYPE"` + User string `json:"user" env:"A_LIST_DB_USER"` + Password string `json:"password" env:"A_LIST_DB_PASS"` + Host string `json:"host" env:"A_LIST_DB_HOST"` + Port int `json:"port" env:"A_LIST_DB_PORT"` + Name string `json:"name" env:"A_LIST_DB_NAME"` + TablePrefix string `json:"table_prefix" env:"A_LIST_DB_TABLE_PREFIX"` + DBFile string `json:"db_file" env:"A_LIST_DB_FILE"` + SslMode string `json:"ssl_mode" env:"A_LIST_SLL_MODE"` } type Scheme struct { - Https bool `json:"https"` - CertFile string `json:"cert_file"` - KeyFile string `json:"key_file"` + Https bool `json:"https" env:"A_LIST_HTTPS"` + CertFile string `json:"cert_file" env:"A_LIST_CERT"` + KeyFile string `json:"key_file" env:"A_LIST_KEY"` } type CacheConfig struct { - Expiration int64 `json:"expiration"` - CleanupInterval int64 `json:"cleanup_interval"` + Expiration int64 `json:"expiration" env:"A_LIST_DB_EXPIRATION"` + CleanupInterval int64 `json:"cleanup_interval" env:"A_LIST_CLEANUP_INTERVAL"` } type Config struct { - Address string `json:"address"` - Port int `json:"port"` - Assets string `json:"assets"` + Force bool `json:"force"` + Address string `json:"address" env:"A_LIST_ADDR"` + Port int `json:"port" env:"A_LIST_PORT"` + Assets string `json:"assets" env:"A_LIST_ASSETS"` Database Database `json:"database"` Scheme Scheme `json:"scheme"` Cache CacheConfig `json:"cache"` - TempDir string `json:"temp_dir"` + TempDir string `json:"temp_dir" env:"A_LIST_TEMP_DIR"` } func DefaultConfig() *Config { diff --git a/go.mod b/go.mod index be3e981d..3bb04aa6 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/aws/aws-sdk-go v1.27.0 + github.com/caarlos0/env/v6 v6.9.1 github.com/eko/gocache/v2 v2.1.0 github.com/gin-contrib/cors v1.3.1 github.com/gin-gonic/gin v1.7.4 @@ -25,7 +26,7 @@ require ( require ( github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect - github.com/fatih/color v1.13.0 // indirect + github.com/fatih/color v1.13.0 github.com/mattn/go-colorable v0.1.9 // indirect golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect ) diff --git a/go.sum b/go.sum index 8cde4460..7e63ced7 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0= github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= +github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k= +github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=