diff options
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) { |