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 {
var env map[string]string = make(map[string]string)
validEnv := []string{"MODE", "OPENWEATHERMAP_API_KEY", "LATITUDE", "LONGITUDE", "PORT"}
envpath := "./.env"
if _, err := os.Stat(envpath); err == nil {
@ -24,29 +26,11 @@ func Load() map[string]string {
fmt.Println("No .env file found", err)
}
mode := os.Getenv("MODE")
if mode != "" {
env["MODE"] = os.Getenv("MODE")
}
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
for _, key := range validEnv {
tempenv := os.Getenv(key)
if tempenv != "" {
env[key] = tempenv
}
}
if len(env) == 0 {
@ -54,5 +38,61 @@ func Load() map[string]string {
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
}
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,6 +3,8 @@ services:
restart: always
container_name: humiditycalc2
image: ghcr.io/leroid-hub/humiditycalc:latest
ports:
- 80:8080
environment:
OPENWEATHERMAP_API_KEY: "YOURAPIKEY"
LATITUDE: "52.51"

40
main.go
View File

@ -11,32 +11,16 @@ import (
func main() {
env := configs.Load()
if val, ok := env["MODE"]; ok {
if strings.ToLower(val) == "both" {
checkEnv(env)
server.Run(env)
} else if strings.ToLower(val) == "weather" {
checkEnv(env)
// weather.Run()
} else if strings.ToLower(val) == "calc" {
// calc.Run()
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)
}
}
switch env["MODE"] {
case "both":
server.Run(env)
case "weather":
server.Run(env)
// weather.Run()
case "calc":
server.Run(env)
// calc.Run()
default:
server.Run(env)