From 8a490909d264c4629891eb565c61086ebd6df052 Mon Sep 17 00:00:00 2001 From: Shugo Kawamura Date: Sat, 5 Aug 2023 03:13:55 +0900 Subject: [PATCH] =?UTF-8?q?Config=E3=81=AE=E5=8F=96=E5=BE=97=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PATCHNOTE.MD | 2 +- backend/go.mod | 1 + backend/go.sum | 3 ++ backend/src/cfg/cfg.go | 89 ++++++++++++++++++------------------------ 4 files changed, 43 insertions(+), 52 deletions(-) diff --git a/PATCHNOTE.MD b/PATCHNOTE.MD index a3fa5168..ae2123cc 100644 --- a/PATCHNOTE.MD +++ b/PATCHNOTE.MD @@ -1,7 +1,7 @@ ### Patch Notes and ROADMAPS ROADMAP OF get5-web-go (temporary,Roadmap and update details may heavily change) -- v0.1.0(ETA. Spring 2020) +- v0.2.0(ETA. Summer 2023) - v0.1.0(Jan.7 2020) : Initial Release - v0.0.1(Dec.29 2019) : Experimental Beta. Most functions wont work yet diff --git a/backend/go.mod b/backend/go.mod index 54795c12..5adcc3f2 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -17,6 +17,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/bytedance/sonic v1.9.1 // indirect + github.com/caarlos0/env/v9 v9.0.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/denisenkom/go-mssqldb v0.9.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect diff --git a/backend/go.sum b/backend/go.sum index cfc6b743..af186da3 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -10,6 +10,8 @@ github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9Pq github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc= +github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= @@ -150,6 +152,7 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.48.0 h1:oJWvHb9BIZToTQS3MuQ2R3bJZiNSa2KiNdeI8A+79Tc= github.com/valyala/fasthttp v1.48.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= diff --git a/backend/src/cfg/cfg.go b/backend/src/cfg/cfg.go index f980ebe2..0fd24b08 100644 --- a/backend/src/cfg/cfg.go +++ b/backend/src/cfg/cfg.go @@ -1,70 +1,57 @@ package config import ( - "flag" - "strings" + "github.com/caarlos0/env/v9" +) - "github.com/go-ini/ini" +var ( + mapPool = []string{ + "de_inferno", + "de_mirage", + "de_nuke", + "de_overpass", + "de_vertigo", + "de_ancient", + "de_anubis", + } ) // Config Configration Struct for config.ini type Config struct { - SteamAPIKey string - DefaultPage string - SQLHost string - SQLUser string - SQLPass string - SQLPort int - SQLDBName string - SQLDebugMode bool - HOST string - Cookie string - APIONLY bool - ActiveMapPool []string - ReserveMapPool []string - UserMaxResources UserMaxResources + SteamAPIKey string `env:"STEAM_API_KEY"` + DefaultPage string `env:"DEFAULT_PAGE"` + + // Database writing + DBWriteHost string `env:"DB_WRITE_HOST"` + DBWritePort int `env:"DB_WRITE_PORT" envDefault:"3306"` + DBWriteUser string `env:"DB_WRITE_USER"` + DBWritePass string `env:"DB_WRITE_PASS,unset"` + DBWriteName string `env:"DB_WRITE_NAME"` + + // Database reading + DBReadHost string `env:"DB_READ_HOST"` + DBReadPort int `env:"DB_READ_PORT" envDefault:"3306"` + DBReadUser string `env:"DB_READ_USER"` + DBReadPass string `env:"DB_READ_PASS,unset"` + DBReadName string `env:"DB_READ_NAME"` + + Cookie string `env:"COOKIE"` + APIONLY bool `env:"API_ONLY" envDefault:"false"` + // UserMaxResources UserMaxResources } +/* type UserMaxResources struct { Servers uint16 Teams uint16 Matches uint16 } +*/ -var ( - // Cnf is configration struct - Cnf Config - ConfigPath *string -) - -func init() { - ConfigPath = flag.String("cfg", "config.ini", "path to config.ini") - flag.Parse() - c, err := ini.Load(*ConfigPath) - if err != nil { +func GetConfig() Config { + cfg := Config{} + if err := env.Parse(&cfg); err != nil { panic(err) } - active := c.Section("MAPLIST").Key("Active").MustString("de_dust2,de_mirage,de_inferno,de_overpass,de_train,de_nuke,de_vertigo") - reserve := c.Section("MAPLIST").Key("Reserve").MustString("de_cache,de_season") - Cnf = Config{ - SteamAPIKey: c.Section("Steam").Key("APIKey").MustString(""), - DefaultPage: c.Section("GET5").Key("DefaultPage").MustString(""), - HOST: c.Section("GET5").Key("HOST").MustString(""), - SQLHost: c.Section("sql").Key("host").MustString(""), - SQLUser: c.Section("sql").Key("user").MustString(""), - SQLPass: c.Section("sql").Key("pass").MustString(""), - SQLPort: c.Section("sql").Key("port").MustInt(3306), - SQLDBName: c.Section("sql").Key("database").MustString(""), - SQLDebugMode: c.Section("sql").Key("debug").MustBool(false), - Cookie: c.Section("GET5").Key("Cookie").MustString("SecretString"), - APIONLY: c.Section("GET5").Key("API_ONLY").MustBool(false), - ActiveMapPool: strings.Split(strings.ToLower(strings.TrimSpace(active)), ","), - ReserveMapPool: strings.Split(strings.ToLower(strings.TrimSpace(reserve)), ","), - UserMaxResources: UserMaxResources{ - Servers: uint16(c.Section("USER").Key("Max_Servers").MustUint(10)), - Teams: uint16(c.Section("USER").Key("Max_Teams").MustUint(100)), - Matches: uint16(c.Section("USER").Key("Max_Matches").MustUint(1000)), - }, - } - + return cfg }