changed to env config

alpha
alex 2023-02-18 15:43:39 +01:00
parent 54c86a7d9f
commit 1496e06582
5 changed files with 72 additions and 13 deletions

16
example.env Normal file
View File

@ -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

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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)
}

View File

@ -2,16 +2,21 @@ 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 {
ServiceName uint8
ServiceType uint8
Debug bool
ScyllaDB ScyllaDB `yaml:"scylladb"`
Host string
Port string
ScyllaDB ScyllaDB
RabbitMq RabbitMq
}
@ -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
}