From bd667a7923f3b81b5b7bb15c742c28e54d91c765 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 18 Feb 2023 15:34:28 +0100 Subject: [PATCH] changed to env config --- config.example.yml | 8 ------ example.env | 13 +++++++++ go.mod | 1 + go.sum | 2 ++ main.go | 12 +++------ modules/config/config.go | 58 +++++++++++++++++++++++++++++++++------- modules/utils/utils.go | 2 +- 7 files changed, 70 insertions(+), 26 deletions(-) delete mode 100644 config.example.yml create mode 100644 example.env diff --git a/config.example.yml b/config.example.yml deleted file mode 100644 index 693c409..0000000 --- a/config.example.yml +++ /dev/null @@ -1,8 +0,0 @@ -debug: false -storagePath: "./storage/" -imageUrlAddress: "http://localhost:8081/v1/" -scylladb: - host: 127.0.0.1 - username: user - password: password - keyspace: keyspace \ No newline at end of file diff --git a/example.env b/example.env new file mode 100644 index 0000000..4f57f18 --- /dev/null +++ b/example.env @@ -0,0 +1,13 @@ +SERVICE_NAME=0 +SERVICE_TYPE=0 +DEBUG=true +HOST=127.0.0.1 +PORT=50029 +STORAGE_PATH=./storage/ +STORAGE_URL_ADDRESS=https://alpha-storage.clickandjoin.umbach.dev/v1/ + +# ScyllaDB +SCYLLADB_HOST=127.0.0.1 +SCYLLADB_USERNAME=user +SCYLLADB_PASSWORD=password +SCYLLADB_KEYSPACE=keyspace \ No newline at end of file diff --git a/go.mod b/go.mod index 7fe1c36..2a0152e 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/andybalholm/brotli v1.0.4 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/klauspost/compress v1.15.9 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect diff --git a/go.sum b/go.sum index 5e10717..918da53 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/h2non/bimg v1.1.9 h1:WH20Nxko9l/HFm4kZCA3Phbgu2cbHvYzxwxn9YROEGg= github.com/h2non/bimg v1.1.9/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= diff --git a/main.go b/main.go index 1979342..4b26d84 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,6 @@ package main import ( - "os" - "clickandjoin.app/storageserver/modules/config" "clickandjoin.app/storageserver/modules/scylladb" "clickandjoin.app/storageserver/routers/router" @@ -39,7 +37,9 @@ func main() { app.Use(cors.New()) - if config.Cfg.Debug { + cfg := config.Cfg + + if cfg.Debug { app.Use(logger.New(logger.Config{ Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}​\n", })) @@ -47,9 +47,5 @@ func main() { router.SetupRoutes(app) - if len(os.Args) < 2 { - gocnjhelper.LogFatal("Please specify port") - } - - app.Listen("127.0.0.1:" + os.Args[1]) + app.Listen(cfg.Host + ":" + cfg.Port) } diff --git a/modules/config/config.go b/modules/config/config.go index 02db7ce..b52296f 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -2,18 +2,23 @@ package config import ( "os" + "strconv" gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper" - "gopkg.in/yaml.v2" + "github.com/joho/godotenv" ) var Cfg Config type Config struct { - Debug bool - StoragePath string `yaml:"storagePath"` - ImageUrlAddress string `yaml:"imageUrlAddress"` - ScyllaDB ScyllaDB `yaml:"scylladb"` + ServiceName uint8 + ServiceType uint8 + Debug bool + Host string + Port string + StoragePath string + StorageUrlAddress string + ScyllaDB ScyllaDB } type ScyllaDB struct { @@ -24,13 +29,48 @@ type ScyllaDB struct { } func LoadConfig() { - data, err := os.ReadFile("config.yml") + // argument to start the server locally for development + if len(os.Args) > 1 { + if os.Args[1] == "--local" || os.Args[1] == "-l" { + if err := godotenv.Load("local.env"); err != nil { + gocnjhelper.LogFatalf("Failed to load env, err: %s", err) + } + } + } + + debug, err := strconv.ParseBool(os.Getenv("DEBUG")) if err != nil { - gocnjhelper.LogFatalf("Failed to read config file, err: %s", err) + gocnjhelper.LogFatalf("Failed to parse boolean, err: %s", err) } - if err := yaml.Unmarshal(data, &Cfg); err != nil { - gocnjhelper.LogFatalf("Failed to unmarshal config file, err: %s", err) + serviceName, err := strconv.Atoi(os.Getenv("SERVICE_NAME")) + + if err != nil { + gocnjhelper.LogFatalf("Failed to parse int, err: %s", err) } + + serviceType, err := strconv.Atoi(os.Getenv("SERVICE_TYPE")) + + if err != nil { + gocnjhelper.LogFatalf("Failed to parse int, err: %s", err) + } + + cfg := Config{ + ServiceName: uint8(serviceName), + ServiceType: uint8(serviceType), + Debug: debug, + Host: os.Getenv("HOST"), + Port: os.Getenv("PORT"), + StoragePath: os.Getenv("STORAGE_PATH"), + StorageUrlAddress: os.Getenv("STORAGE_URL_ADDRESS"), + ScyllaDB: ScyllaDB{ + Host: os.Getenv("SCYLLADB_HOST"), + Username: os.Getenv("SCYLLADB_USERNAME"), + Password: os.Getenv("SCYLLADB_PASSWORD"), + Keyspace: os.Getenv("SCYLLADB_KEYSPACE"), + }, + } + + Cfg = cfg } diff --git a/modules/utils/utils.go b/modules/utils/utils.go index e5475da..5b8f462 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -16,5 +16,5 @@ func GetUserStoragePath(userId string) string { } func GetUserAvatarUrl(userId string, filename string) string { - return config.Cfg.ImageUrlAddress + "avatars/" + userId + "/" + filename + ".webp" + return config.Cfg.StorageUrlAddress + "avatars/" + userId + "/" + filename + ".webp" }