diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-02-14 16:07:17 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-02-14 16:07:17 +0100 |
commit | 90acc67af9a7d372be5fa9cab7a34412ce4ad824 (patch) | |
tree | aa733238bcbcc2b0c447d890b5c256fa7f0e21b1 /main.go | |
parent | 6544171b7a6eb2e0156b66a1ad4c58d3a6cacd86 (diff) | |
download | gosten-90acc67af9a7d372be5fa9cab7a34412ce4ad824.tar.gz gosten-90acc67af9a7d372be5fa9cab7a34412ce4ad824.tar.bz2 gosten-90acc67af9a7d372be5fa9cab7a34412ce4ad824.zip |
Load .env file
Diffstat (limited to '')
-rw-r--r-- | main.go | 29 |
1 files changed, 18 insertions, 11 deletions
@@ -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) { |