diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | main.go | 29 | ||||
-rw-r--r-- | sqlite.go | 3 |
5 files changed, 24 insertions, 14 deletions
@@ -4,4 +4,5 @@ out gen /test.sqlite *.exe -/gosten
\ No newline at end of file +/gosten +/.env @@ -10,6 +10,7 @@ require ( github.com/gorilla/schema v1.2.1 github.com/gorilla/securecookie v1.1.2 github.com/gorilla/sessions v1.2.2 + github.com/joho/godotenv v1.5.1 github.com/mattn/go-sqlite3 v1.14.22 github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551 golang.org/x/crypto v0.19.0 @@ -26,6 +26,8 @@ github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kX github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY= github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -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) { @@ -34,8 +34,7 @@ func (l logger) Log(ctx context.Context, level sqldblogger.Level, msg string, da slog.LogAttrs(ctx, lvl, msg, attrs...) } -func openDB(_ string) *sql.DB { - dsn := "test.sqlite" +func openDB(dsn string) *sql.DB { db, err := sql.Open(driverName, dsn) if err != nil { log.Fatal(err) |