From 018baa0e0f2b06a8a360dc996908c5ae11d3c532 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Mon, 13 May 2024 22:32:31 +1200 Subject: [PATCH] feat: copy user reference link --- .gitignore | 1 + auth/auth.go | 2 +- auth/login.go | 1 + context/home.go | 4 +++- env/env.go | 29 ++++++++++++++++++++++++++++- main.go | 3 ++- templates/home.gotmpl | 5 ++++- 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 96baa7e..72aebf1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store gin-bin lishwist.db +.env diff --git a/auth/auth.go b/auth/auth.go index d28c1c7..ffea72c 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -41,7 +41,7 @@ func (auth *AuthMiddleware) ExpectUser(r *http.Request) *db.User { } func NewAuthMiddleware(protectedHandler http.Handler, publicHandler http.Handler) *AuthMiddleware { - store := sessions.NewCookieStore([]byte(env.Secret)) + store := sessions.NewCookieStore([]byte(env.JwtSecret)) store.Options.MaxAge = 86_400 return &AuthMiddleware{store, protectedHandler, publicHandler} } diff --git a/auth/login.go b/auth/login.go index 279dad9..bdb64b0 100644 --- a/auth/login.go +++ b/auth/login.go @@ -39,6 +39,7 @@ func (auth *AuthMiddleware) LoginPost(w http.ResponseWriter, r *http.Request) { session, err := auth.Store.Get(r, "lishwist_user") if err != nil { + log.Println("Couldn't get jwt:", err) http.Error(w, "Something went wrong. Error code: Sokka", http.StatusInternalServerError) return } diff --git a/context/home.go b/context/home.go index 8dd5263..4dfe814 100644 --- a/context/home.go +++ b/context/home.go @@ -4,6 +4,7 @@ import ( "net/http" "lishwist/db" + "lishwist/env" "lishwist/templates" ) @@ -12,6 +13,7 @@ type HomeProps struct { Gifts []db.Gift Todo []db.Gift Reference string + HostUrl string } func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) { @@ -26,6 +28,6 @@ func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) { http.Error(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError) return } - p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference} + p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.HostUrl.String()} templates.Execute(w, "home.gotmpl", p) } diff --git a/env/env.go b/env/env.go index ca24fce..a79ec84 100644 --- a/env/env.go +++ b/env/env.go @@ -1,3 +1,30 @@ package env -const Secret = "BLAHBLAHLBAH" +import ( + "log" + "net/url" + "os" +) + +func GuaranteeEnv(key string) (variable string) { + variable, ok := os.LookupEnv(key) + if !ok || variable == "" { + log.Fatalln("Missing environment variable:", key) + } + return +} + +var JwtSecret = GuaranteeEnv("LISHWIST_JWT_SECRET") +var HostDomain = GuaranteeEnv("LISHWIST_DOMAIN") +var HostPort = os.Getenv("LISHWIST_PORT") +var HostUrl = func() *url.URL { + rawUrl := "http://" + HostDomain + if HostPort != "" { + rawUrl += ":" + HostPort + } + u, err := url.Parse(rawUrl) + if err != nil { + log.Fatalln("Couldn't parse host url:", err) + } + return u +}() diff --git a/main.go b/main.go index 0ffc82f..ae16967 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "lishwist/auth" "lishwist/context" "lishwist/db" + "lishwist/env" "lishwist/templates" ) @@ -44,5 +45,5 @@ func main() { http.Handle("/", authMiddleware) - http.ListenAndServe(":4000", nil) + http.ListenAndServe(":"+env.HostPort, nil) } diff --git a/templates/home.gotmpl b/templates/home.gotmpl index 92c7ae5..9b1b8ed 100644 --- a/templates/home.gotmpl +++ b/templates/home.gotmpl @@ -6,8 +6,11 @@
{{.Reference}}
+