This commit is contained in:
Jan 2024-11-02 13:24:36 +01:00
parent e72a267cd4
commit 80cdd9de4f
6 changed files with 59 additions and 3 deletions

View File

@ -1 +1,7 @@
# humiditycalc # humiditycalc
## ENV
MODES: CALC, WEATHER, BOTH
BOTH is standard
OPENWEATHERMAP_API_KEY is required in WEATHER and BOTH mode

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/LeRoid-hub/humiditycalc module github.com/LeRoid-hub/humiditycalc
go 1.22.2 go 1.22.2
require github.com/joho/godotenv v1.5.1 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

1
internal/weather.go Normal file
View File

@ -0,0 +1 @@
package internal

45
main.go
View File

@ -1,9 +1,54 @@
package main package main
import ( import (
"fmt"
"os"
"strings"
"github.com/LeRoid-hub/humiditycalc/server" "github.com/LeRoid-hub/humiditycalc/server"
"github.com/joho/godotenv"
) )
func main() { func main() {
env := loadEnv()
if val, ok := env["MODE"]; ok {
if strings.ToLower(val) == "both" {
checkEnv(env)
server.Run() server.Run()
} else if strings.ToLower(val) == "weather" {
checkEnv(env)
// weather.Run()
} else if strings.ToLower(val) == "calc" {
// calc.Run()
server.Run()
}
}
}
func loadEnv() map[string]string {
var env map[string]string
env, err := godotenv.Read()
if err != nil {
fmt.Println("Error loading .env file: ", err)
os.Exit(1)
}
if len(env) == 0 {
fmt.Println(".env file is empty")
os.Exit(1)
}
return env
}
func checkEnv(env map[string]string) {
// Is there an API key for openweathermap?
if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok {
fmt.Println(val)
if val == "" {
print("OPENWEATHERMAP_API_KEY is not set")
os.Exit(1)
}
} else {
fmt.Println("OPENWEATHERMAP_API_KEY is not set")
os.Exit(1)
}
} }

View File

@ -12,7 +12,7 @@ type result struct {
AbsoluteHumidity float64 AbsoluteHumidity float64
} }
// StartServer starts the HTTP server. // Run starts the HTTP server.
func Run() { func Run() {
tmpl := template.Must(template.ParseFiles("./web/templates/index.html")) tmpl := template.Must(template.ParseFiles("./web/templates/index.html"))