From bb017c5f6d9b878ee32c517e68d93e73737e4228 Mon Sep 17 00:00:00 2001 From: Xhofe Date: Sun, 13 Mar 2022 17:01:45 +0800 Subject: [PATCH] feat: remove env prefix for docker --- Dockerfile | 2 +- bootstrap/conf.go | 8 +++++++- bootstrap/log.go | 1 + conf/config.go | 36 ++++++++++++++++++------------------ conf/var.go | 1 + 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index afa91a84..4392cceb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,4 +11,4 @@ VOLUME /opt/alist/data/ WORKDIR /opt/alist/ COPY --from=builder /app/bin/alist ./ EXPOSE 5244 -CMD [ "./alist" ] \ No newline at end of file +CMD [ "./alist -docker" ] \ No newline at end of file diff --git a/bootstrap/conf.go b/bootstrap/conf.go index 06433990..f4d87d4e 100644 --- a/bootstrap/conf.go +++ b/bootstrap/conf.go @@ -54,7 +54,13 @@ func InitConf() { } func confFromEnv() { - if err := env.Parse(conf.Conf); err != nil { + prefix := "ALIST_" + if conf.Docker { + prefix = "" + } + if err := env.Parse(conf.Conf, env.Options{ + Prefix: prefix, + }); err != nil { log.Fatalf("load config from env error: %s", err.Error()) } } diff --git a/bootstrap/log.go b/bootstrap/log.go index 30bfb172..45bb418b 100644 --- a/bootstrap/log.go +++ b/bootstrap/log.go @@ -30,6 +30,7 @@ func init() { flag.BoolVar(&conf.Debug, "debug", false, "start with debug mode") flag.BoolVar(&conf.Version, "version", false, "print version info") flag.BoolVar(&conf.Password, "password", false, "print current password") + flag.BoolVar(&conf.Docker, "docker", false, "is using docker") flag.Parse() InitLog() } diff --git a/conf/config.go b/conf/config.go index aeebe56c..280cd5df 100644 --- a/conf/config.go +++ b/conf/config.go @@ -1,37 +1,37 @@ package conf type Database struct { - 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 string `json:"type" env:"DB_TYPE"` + Host string `json:"host" env:"DB_HOST"` + Port int `json:"port" env:"DB_PORT"` + User string `json:"user" env:"DB_USER"` + Password string `json:"password" env:"DB_PASS"` + Name string `json:"name" env:"DB_NAME"` + DBFile string `json:"db_file" env:"DB_FILE"` + TablePrefix string `json:"table_prefix" env:"DB_TABLE_PREFIX"` + SslMode string `json:"ssl_mode" env:"DB_SLL_MODE"` } type Scheme struct { - 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"` + Https bool `json:"https" env:"HTTPS"` + CertFile string `json:"cert_file" env:"CERT_FILE"` + KeyFile string `json:"key_file" env:"KEY_FILE"` } type CacheConfig struct { - Expiration int64 `json:"expiration" env:"A_LIST_EXPIRATION"` - CleanupInterval int64 `json:"cleanup_interval" env:"A_LIST_CLEANUP_INTERVAL"` + Expiration int64 `json:"expiration" env:"CACHE_EXPIRATION"` + CleanupInterval int64 `json:"cleanup_interval" env:"CLEANUP_INTERVAL"` } type Config struct { 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"` + Address string `json:"address" env:"ADDR"` + Port int `json:"port" env:"PORT"` + Assets string `json:"assets" env:"ASSETS"` Database Database `json:"database"` Scheme Scheme `json:"scheme"` Cache CacheConfig `json:"cache"` - TempDir string `json:"temp_dir" env:"A_LIST_TEMP_DIR"` + TempDir string `json:"temp_dir" env:"TEMP_DIR"` } func DefaultConfig() *Config { diff --git a/conf/var.go b/conf/var.go index 33fb84b4..09429ab4 100644 --- a/conf/var.go +++ b/conf/var.go @@ -24,6 +24,7 @@ var ( Debug bool Version bool Password bool + Docker bool DB *gorm.DB Cache *cache.Cache