From 1496e06582e989e98b4498a44962ace0599b08de Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 18 Feb 2023 15:43:39 +0100 Subject: [PATCH] changed to env config --- example.env | 16 +++++++++++ go.mod | 1 + go.sum | 2 ++ main.go | 7 ++--- modules/config/config.go | 59 ++++++++++++++++++++++++++++++++++------ 5 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 example.env diff --git a/example.env b/example.env new file mode 100644 index 0000000..fc50b7d --- /dev/null +++ b/example.env @@ -0,0 +1,16 @@ +SERVICE_NAME=4 +SERVICE_TYPE=0 +DEBUG=false +HOST=127.0.0.1 +PORT= + +# ScyllaDB +SCYLLADB_HOST=127.0.0.1 +SCYLLADB_USERNAME=user +SCYLLADB_PASSWORD=password +SCYLLADB_KEYSPACE=keyspace + +# RabbitMQ +RABBITMQ_HOST=127.0.0.1 +RABBITMQ_USERNAME=guest +RABBITMQ_PASSWORD=guest \ No newline at end of file diff --git a/go.mod b/go.mod index 14f913b..c3c0999 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/fasthttp/websocket v1.5.0 // 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.12 // 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 67aab1c..464793e 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.14.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM= diff --git a/main.go b/main.go index 4e62766..abe24a1 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "os" "time" "clickandjoin.app/websocketserver/modules/config" @@ -73,9 +72,7 @@ func main() { socketserver.WebSocketServer(app) - if len(os.Args) < 2 { - gocnjhelper.LogFatal("Please specify port") - } + cfg := config.Cfg - 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 d887025..4ddbe9e 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -2,17 +2,22 @@ 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 - ScyllaDB ScyllaDB `yaml:"scylladb"` - RabbitMq RabbitMq + ServiceName uint8 + ServiceType uint8 + Debug bool + Host string + Port string + ScyllaDB ScyllaDB + RabbitMq RabbitMq } type ScyllaDB struct { @@ -29,13 +34,51 @@ type RabbitMq 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"), + ScyllaDB: ScyllaDB{ + Host: os.Getenv("SCYLLADB_HOST"), + Username: os.Getenv("SCYLLADB_USERNAME"), + Password: os.Getenv("SCYLLADB_PASSWORD"), + Keyspace: os.Getenv("SCYLLADB_KEYSPACE"), + }, + RabbitMq: RabbitMq{ + Host: os.Getenv("RABBITMQ_HOST"), + Username: os.Getenv("RABBITMQ_USERNAME"), + Password: os.Getenv("RABBITMQ_PASSWORD"), + }, + } + + Cfg = cfg }