Added Ports to compose.yml + moved the env check to the config file

This commit is contained in:
Jan Barfuss 2024-11-07 10:17:33 +01:00
parent 16739666dc
commit afea4a5d26
3 changed files with 75 additions and 49 deletions

View File

@ -10,6 +10,8 @@ import (
func Load() map[string]string { func Load() map[string]string {
var env map[string]string = make(map[string]string) var env map[string]string = make(map[string]string)
validEnv := []string{"MODE", "OPENWEATHERMAP_API_KEY", "LATITUDE", "LONGITUDE", "PORT"}
envpath := "./.env" envpath := "./.env"
if _, err := os.Stat(envpath); err == nil { if _, err := os.Stat(envpath); err == nil {
@ -24,35 +26,73 @@ func Load() map[string]string {
fmt.Println("No .env file found", err) fmt.Println("No .env file found", err)
} }
mode := os.Getenv("MODE") for _, key := range validEnv {
if mode != "" { tempenv := os.Getenv(key)
env["MODE"] = os.Getenv("MODE") if tempenv != "" {
} env[key] = tempenv
}
openweathermapAPIKey := os.Getenv("OPENWEATHERMAP_API_KEY")
if openweathermapAPIKey != "" {
env["OPENWEATHERMAP_API_KEY"] = openweathermapAPIKey
}
latitude := os.Getenv("LATITUDE")
if latitude != "" {
env["LATITUDE"] = latitude
}
longitude := os.Getenv("LONGITUDE")
if longitude != "" {
env["LONGITUDE"] = longitude
}
port := os.Getenv("PORT")
if port != "" {
env["PORT"] = port
} }
if len(env) == 0 { if len(env) == 0 {
fmt.Println("no environment variables are set") fmt.Println("no environment variables are set")
os.Exit(1) os.Exit(1)
} }
if val, ok := env["MODE"]; ok {
if val == "" {
env["MODE"] = "calc"
}
} else {
env["MODE"] = "calc"
}
env["MODE"] = strings.ToLower(env["MODE"])
switch env["MODE"] {
case "both":
checkEnvWeather(env)
checkEnvCalc(env)
case "weather":
checkEnvWeather(env)
default:
env["MODE"] = "calc"
}
return env return env
} }
func checkEnvWeather(env map[string]string) {
// Is there an API key for openweathermap?
if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok {
if val == "" {
print("OPENWEATHERMAP_API_KEY is not set")
os.Exit(1)
}
} else {
fmt.Println("OPENWEATHERMAP_API_KEY is not set")
os.Exit(1)
}
// Is there a LATITUDE and LONGITUDE?
if val, ok := env["LATITUDE"]; ok {
if val == "" {
print("LATITUDE is not set")
os.Exit(1)
}
} else {
fmt.Println("LATITUDE is not set")
os.Exit(1)
}
if val, ok := env["LONGITUDE"]; ok {
if val == "" {
print("LONGITUDE is not set")
os.Exit(1)
}
} else {
fmt.Println("LONGITUDE is not set")
os.Exit(1)
}
}
func checkEnvCalc(env map[string]string) {
// Check for calc variables
}

View File

@ -3,8 +3,10 @@ services:
restart: always restart: always
container_name: humiditycalc2 container_name: humiditycalc2
image: ghcr.io/leroid-hub/humiditycalc:latest image: ghcr.io/leroid-hub/humiditycalc:latest
ports:
- 80:8080
environment: environment:
OPENWEATHERMAP_API_KEY: "YOURAPIKEY" OPENWEATHERMAP_API_KEY: "YOURAPIKEY"
LATITUDE: "52.51" LATITUDE: "52.51"
LONGITUDE: "13.404" LONGITUDE: "13.404"
MODE: "BOTH" MODE: "BOTH"

34
main.go
View File

@ -11,32 +11,16 @@ import (
func main() { func main() {
env := configs.Load() env := configs.Load()
if val, ok := env["MODE"]; ok {
if strings.ToLower(val) == "both" { switch env["MODE"] {
checkEnv(env) case "both":
server.Run(env)
case "weather":
server.Run(env) server.Run(env)
} else if strings.ToLower(val) == "weather" {
checkEnv(env)
// weather.Run() // weather.Run()
} else if strings.ToLower(val) == "calc" { case "calc":
// calc.Run() server.Run(env)
// calc.Run()
default:
server.Run(env) server.Run(env)
}
} else {
// calc.Run()
server.Run(env)
}
}
func checkEnv(env map[string]string) {
// Is there an API key for openweathermap?
if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok {
if val == "" {
print("OPENWEATHERMAP_API_KEY is not set")
os.Exit(1)
}
} else {
fmt.Println("OPENWEATHERMAP_API_KEY is not set")
os.Exit(1)
}
}