add(backend): config management

This commit is contained in:
Ada 2023-10-02 15:03:04 +02:00
parent b14360dd8b
commit 0977e8d064
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
plakken
# Output of the go coverage tool, specifically when used with LiteIDE
*.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() {
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
}