diff --git a/backend/.gitignore b/backend/.gitignore index 23487f5..83e7943 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,4 @@ *.db3 *.db3-* +*.db3.* /allowance_planner diff --git a/backend/allowance_planner.db3.backup.3 b/backend/allowance_planner.db3.backup.3 index b7a3e07..380cafd 100644 Binary files a/backend/allowance_planner.db3.backup.3 and b/backend/allowance_planner.db3.backup.3 differ diff --git a/backend/main.go b/backend/main.go index d18c841..c1481a2 100644 --- a/backend/main.go +++ b/backend/main.go @@ -43,6 +43,11 @@ type ServerConfig struct { Started chan bool } +const DefaultDomain = "localhost:8080" + +// The domain that the server is reachable at. +var domain = DefaultDomain + func getUsers(c *gin.Context) { users, err := db.GetUsers() if err != nil { @@ -706,5 +711,10 @@ func main() { config.Datasource = "allowance_planner.db3" log.Printf("Warning: No DB_PATH set, using default of %s", config.Datasource) } + domain = os.Getenv("DOMAIN") + if domain == "" { + domain = DefaultDomain + log.Printf("Warning: No DOMAIN set, using default of %s", domain) + } start(context.Background(), &config) } diff --git a/backend/web.go b/backend/web.go index 820e89e..d8188e1 100644 --- a/backend/web.go +++ b/backend/web.go @@ -3,6 +3,7 @@ package main import ( "errors" "github.com/gin-gonic/gin" + "log" "net/http" "strconv" ) @@ -26,11 +27,22 @@ func loadWebEndpoints(router *gin.Engine) { router.GET("/completeAllowance", renderCompleteAllowance) } +func redirectToPage(c *gin.Context, page string) { + redirectToPageStatus(c, page, http.StatusSeeOther) +} + +func redirectToPageStatus(c *gin.Context, page string, status int) { + scheme := c.Request.URL.Scheme + target := scheme + domain + page + c.Redirect(status, target) +} + func renderLogin(c *gin.Context) { if c.Query("user") != "" { - c.SetCookie("user", c.Query("user"), 3600, "/", "localhost", false, true) + log.Println("Set cookie for user:", c.Query("user")) + c.SetCookie("user", c.Query("user"), 3600, "", "", false, true) } - c.Redirect(http.StatusFound, "/") + redirectToPage(c, "/") } func renderIndex(c *gin.Context) { @@ -68,7 +80,7 @@ func renderCreateTask(c *gin.Context) { return } - c.Redirect(http.StatusFound, "/") + redirectToPageStatus(c, "/", http.StatusFound) } func renderCompleteTask(c *gin.Context) { @@ -85,7 +97,7 @@ func renderCompleteTask(c *gin.Context) { return } - c.Redirect(http.StatusFound, "/") + redirectToPageStatus(c, "/", http.StatusFound) } func renderCreateAllowance(c *gin.Context) { @@ -122,7 +134,7 @@ func renderCreateAllowance(c *gin.Context) { return } - c.Redirect(http.StatusFound, "/") + redirectToPageStatus(c, "/", http.StatusFound) } func renderCompleteAllowance(c *gin.Context) { @@ -144,11 +156,12 @@ func renderCompleteAllowance(c *gin.Context) { return } - c.Redirect(http.StatusFound, "/") + redirectToPageStatus(c, "/", http.StatusFound) } func getCurrentUser(c *gin.Context) *int { currentUserStr, err := c.Cookie("user") + log.Println("Cookie string:", currentUserStr) if errors.Is(err, http.ErrNoCookie) { renderNoUser(c) return nil @@ -172,7 +185,7 @@ func getCurrentUser(c *gin.Context) *int { func unsetUserCookie(c *gin.Context) { c.SetCookie("user", "", -1, "/", "localhost", false, true) - c.Redirect(http.StatusFound, "/") + redirectToPageStatus(c, "/", http.StatusFound) } func renderNoUser(c *gin.Context) { diff --git a/backend/web.gohtml b/backend/web.gohtml index 1ed49ee..cd23150 100644 --- a/backend/web.gohtml +++ b/backend/web.gohtml @@ -3,9 +3,11 @@