changed to env config

alpha
alex 2023-02-18 15:34:28 +01:00
parent c5565114bf
commit bd667a7923
7 changed files with 70 additions and 26 deletions

View File

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

13
example.env Normal file
View File

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

1
go.mod
View File

@ -16,6 +16,7 @@ require (
github.com/andybalholm/brotli v1.0.4 // indirect github.com/andybalholm/brotli v1.0.4 // indirect
github.com/golang/snappy v0.0.4 // indirect github.com/golang/snappy v0.0.4 // indirect
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // 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/klauspost/compress v1.15.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-isatty v0.0.17 // indirect

2
go.sum
View File

@ -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/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 h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= 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 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=

12
main.go
View File

@ -15,8 +15,6 @@
package main package main
import ( import (
"os"
"clickandjoin.app/storageserver/modules/config" "clickandjoin.app/storageserver/modules/config"
"clickandjoin.app/storageserver/modules/scylladb" "clickandjoin.app/storageserver/modules/scylladb"
"clickandjoin.app/storageserver/routers/router" "clickandjoin.app/storageserver/routers/router"
@ -39,7 +37,9 @@ func main() {
app.Use(cors.New()) app.Use(cors.New())
if config.Cfg.Debug { cfg := config.Cfg
if cfg.Debug {
app.Use(logger.New(logger.Config{ app.Use(logger.New(logger.Config{
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n", Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n",
})) }))
@ -47,9 +47,5 @@ func main() {
router.SetupRoutes(app) router.SetupRoutes(app)
if len(os.Args) < 2 { app.Listen(cfg.Host + ":" + cfg.Port)
gocnjhelper.LogFatal("Please specify port")
}
app.Listen("127.0.0.1:" + os.Args[1])
} }

View File

@ -2,18 +2,23 @@ package config
import ( import (
"os" "os"
"strconv"
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper" gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
"gopkg.in/yaml.v2" "github.com/joho/godotenv"
) )
var Cfg Config var Cfg Config
type Config struct { type Config struct {
ServiceName uint8
ServiceType uint8
Debug bool Debug bool
StoragePath string `yaml:"storagePath"` Host string
ImageUrlAddress string `yaml:"imageUrlAddress"` Port string
ScyllaDB ScyllaDB `yaml:"scylladb"` StoragePath string
StorageUrlAddress string
ScyllaDB ScyllaDB
} }
type ScyllaDB struct { type ScyllaDB struct {
@ -24,13 +29,48 @@ type ScyllaDB struct {
} }
func LoadConfig() { 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 { 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 { serviceName, err := strconv.Atoi(os.Getenv("SERVICE_NAME"))
gocnjhelper.LogFatalf("Failed to unmarshal config file, err: %s", err)
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
} }

View File

@ -16,5 +16,5 @@ func GetUserStoragePath(userId string) string {
} }
func GetUserAvatarUrl(userId string, filename 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"
} }