add(backend): config management

This commit is contained in:
Ada 2023-10-02 15:03:04 +02:00
parent 6e5dcb5fe1
commit ea0a76ff7d
3 changed files with 50 additions and 9 deletions

1
.gitignore vendored
View file

@ -7,6 +7,7 @@
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test
plakken
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out

46
config.go Normal file
View file

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

12
main.go
View file

@ -6,14 +6,8 @@ import (
) )
func main() { func main() {
i := 0 currentConfig := setConfig()
i = 5 listen := currentConfig.host + ":" + currentConfig.port
var k int8 = 0
fmt.Println(i)
fmt.Println(k)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintf(w, "Hello, you're at %s", r.URL.Path) _, err := fmt.Fprintf(w, "Hello, you're at %s", r.URL.Path)
if err != nil { if err != nil {
@ -21,7 +15,7 @@ func main() {
} }
}) })
err := http.ListenAndServe(":8080", nil) err := http.ListenAndServe(listen, nil)
if err != nil { if err != nil {
return return
} }