From ea0a76ff7d8349616723d7453e6915673518f4da Mon Sep 17 00:00:00 2001 From: Ada Date: Mon, 2 Oct 2023 15:03:04 +0200 Subject: [PATCH] add(backend): config management --- .gitignore | 1 + config.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 12 +++--------- 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 config.go diff --git a/.gitignore b/.gitignore index 00b2937..f91ae19 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # Test binary, built with `go test -c` *.test +plakken # Output of the go coverage tool, specifically when used with LiteIDE *.out diff --git a/config.go b/config.go new file mode 100644 index 0000000..e6eef3f --- /dev/null +++ b/config.go @@ -0,0 +1,46 @@ +package main + +import ( + "os" + "strconv" +) + +type config struct { + host string + port string + redisAddr string + redisUser string + redisPassword string + redisDB int +} + +func setConfig() config { + host := os.Getenv("PLAKKEN_HOST") + + port := os.Getenv("PLAKKEN_PORT") + if port == "" { + port = "3000" + } + redisAddr := os.Getenv("PLAKKEN_REDIS_ADDR") + if redisAddr == "" { + redisAddr = "localhost:6379" + } + + redisUser := os.Getenv("PLAKKEN_REDIS_USER") + redisPassword := os.Getenv("PLAKKEN_REDIS_PASSWORD") + redisDB, err := strconv.Atoi(os.Getenv("PLAKKEN_REDIS_DB")) + if err != nil { + redisDB = 0 + } + + s := config{ + host: host, + port: port, + redisAddr: redisAddr, + redisUser: redisUser, + redisPassword: redisPassword, + redisDB: redisDB, + } + + return s +} diff --git a/main.go b/main.go index 373757f..82b3630 100644 --- a/main.go +++ b/main.go @@ -6,14 +6,8 @@ import ( ) func main() { - i := 0 - i = 5 - - var k int8 = 0 - - fmt.Println(i) - fmt.Println(k) - + currentConfig := setConfig() + listen := currentConfig.host + ":" + currentConfig.port http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { _, err := fmt.Fprintf(w, "Hello, you're at %s", r.URL.Path) if err != nil { @@ -21,7 +15,7 @@ func main() { } }) - err := http.ListenAndServe(":8080", nil) + err := http.ListenAndServe(listen, nil) if err != nil { return }