From 90acc67af9a7d372be5fa9cab7a34412ce4ad824 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Wed, 14 Feb 2024 16:07:17 +0100 Subject: Load .env file --- main.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 38bfd93..f68b7b7 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,12 @@ package main import ( - "flag" "log" - "net" "net/http" "os" - "strconv" "github.com/gorilla/handlers" + "github.com/joho/godotenv" "gosten/model" "gosten/templ" @@ -20,17 +18,27 @@ var ( host string ) -func init() { - flag.StringVar(&host, "h", "localhost", "address to listen on") - flag.Uint64Var(&port, "p", 8080, "port to listen on") +var Q *model.Queries + +func checkEnvEntry(e string) { + if os.Getenv(e) == "" { + log.Fatalf("Variable '%s' not set", e) + } } -var Q *model.Queries +func checkEnv() { + checkEnvEntry("GOSTEN_DSN") + checkEnvEntry("GOSTEN_ADDRESS") +} func main() { - flag.Parse() + if err := godotenv.Load(); err != nil { + log.Fatal("Loading env file: ", err) + } + + checkEnv() - db := openDB("") + db := openDB(os.Getenv("GOSTEN_DSN")) if err := db.Ping(); err != nil { log.Fatal(err) } @@ -55,8 +63,7 @@ func main() { authMux.Handle("GET /{$}", indexPage()) - address := net.JoinHostPort(host, strconv.FormatUint(port, 10)) - log.Fatal(http.ListenAndServe(address, handler)) + log.Fatal(http.ListenAndServe(os.Getenv("GOSTEN_ADDRESS"), handler)) } func showTemplate(w http.ResponseWriter, tpl string, data any) { -- cgit v1.2.3-70-g09d2