diff options
author | Samuel Johnson <[email protected]> | 2025-04-28 22:35:17 -0400 |
---|---|---|
committer | Samuel Johnson <[email protected]> | 2025-04-28 22:35:17 -0400 |
commit | 0b77727cdb52cb22ae55db5b747345c1d89a29d3 (patch) | |
tree | 385fba102ad2d8a9340834aea97b2c904372c6f5 | |
parent | 9de239a9badbbc075f1cc8079a6435f0780e5d26 (diff) |
Move to using .env file
-rw-r--r-- | cmd/web/main.go | 27 | ||||
-rw-r--r-- | go.mod | 5 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | internal/environment.go | 13 |
4 files changed, 40 insertions, 7 deletions
diff --git a/cmd/web/main.go b/cmd/web/main.go index 748e4bb..31137cf 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -1,19 +1,34 @@ package main import ( - "flag" "fmt" "log" "net/http" + "os" + "strconv" + + "github.com/joho/godotenv" "paterissa.net/mblog/cmd/web/handlers" + "paterissa.net/mblog/internal" ) func main() { - webmaster := flag.String("webmaster", "", "host of the website") - port := flag.Int("port", 5050, "the hosting port") - flag.Parse() + err := godotenv.Load() + if err != nil { + log.Fatal("Failed to load env") + } + + var Env internal.Environment + + Env.Webmaster = os.Getenv("webmaster") + Env.Db.Username = os.Getenv("db_user") + Env.Db.Password = os.Getenv("db_pass") + Env.AppPort, err = strconv.ParseUint(os.Getenv("app_port"), 10, 64) + if err != nil { + Env.AppPort = 5005 + } - router := handlers.RegisterEndpoints(*webmaster) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), router)) + router := handlers.RegisterEndpoints(Env.Webmaster) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", Env.AppPort), router)) } @@ -2,4 +2,7 @@ module paterissa.net/mblog go 1.23.5 -require github.com/gorilla/mux v1.8.1 // indirect +require ( + github.com/gorilla/mux v1.8.1 + github.com/joho/godotenv v1.5.1 +) @@ -1,2 +1,4 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/internal/environment.go b/internal/environment.go new file mode 100644 index 0000000..cbdd17c --- /dev/null +++ b/internal/environment.go @@ -0,0 +1,13 @@ +package internal + +type DbCredentials struct { + Username string + Password string +} + +type Environment struct { + Webmaster string + AppPort uint64 + + Db DbCredentials +} |