summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2024-02-14 16:07:17 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2024-02-14 16:07:17 +0100
commit90acc67af9a7d372be5fa9cab7a34412ce4ad824 (patch)
treeaa733238bcbcc2b0c447d890b5c256fa7f0e21b1
parent6544171b7a6eb2e0156b66a1ad4c58d3a6cacd86 (diff)
downloadgosten-90acc67af9a7d372be5fa9cab7a34412ce4ad824.tar.gz
gosten-90acc67af9a7d372be5fa9cab7a34412ce4ad824.tar.bz2
gosten-90acc67af9a7d372be5fa9cab7a34412ce4ad824.zip
Load .env file
-rw-r--r--.gitignore3
-rw-r--r--go.mod1
-rw-r--r--go.sum2
-rw-r--r--main.go29
-rw-r--r--sqlite.go3
5 files changed, 24 insertions, 14 deletions
diff --git a/.gitignore b/.gitignore
index 881a4c7..f219121 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,5 @@ out
gen
/test.sqlite
*.exe
-/gosten \ No newline at end of file
+/gosten
+/.env
diff --git a/go.mod b/go.mod
index e2d4b2e..537c367 100644
--- a/go.mod
+++ b/go.mod
@@ -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
diff --git a/go.sum b/go.sum
index ccc5424..2388f15 100644
--- a/go.sum
+++ b/go.sum
@@ -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=
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) {
diff --git a/sqlite.go b/sqlite.go
index bc3c733..8ee6325 100644
--- a/sqlite.go
+++ b/sqlite.go
@@ -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)