Improve compatibility with old browsers (#136)
All checks were successful
Backend Deploy / build (push) Successful in 3m16s
Backend Build and Test / build (push) Successful in 3m22s

Reviewed-on: #136
This commit is contained in:
Sebastiaan de Schaetzen 2025-05-29 13:54:52 +02:00
parent 02c5c6ea68
commit 5a20e76df2
5 changed files with 36 additions and 10 deletions

1
backend/.gitignore vendored
View File

@ -1,3 +1,4 @@
*.db3 *.db3
*.db3-* *.db3-*
*.db3.*
/allowance_planner /allowance_planner

View File

@ -43,6 +43,11 @@ type ServerConfig struct {
Started chan bool Started chan bool
} }
const DefaultDomain = "localhost:8080"
// The domain that the server is reachable at.
var domain = DefaultDomain
func getUsers(c *gin.Context) { func getUsers(c *gin.Context) {
users, err := db.GetUsers() users, err := db.GetUsers()
if err != nil { if err != nil {
@ -706,5 +711,10 @@ func main() {
config.Datasource = "allowance_planner.db3" config.Datasource = "allowance_planner.db3"
log.Printf("Warning: No DB_PATH set, using default of %s", config.Datasource) 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) start(context.Background(), &config)
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"log"
"net/http" "net/http"
"strconv" "strconv"
) )
@ -26,11 +27,22 @@ func loadWebEndpoints(router *gin.Engine) {
router.GET("/completeAllowance", renderCompleteAllowance) 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) { func renderLogin(c *gin.Context) {
if c.Query("user") != "" { 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) { func renderIndex(c *gin.Context) {
@ -68,7 +80,7 @@ func renderCreateTask(c *gin.Context) {
return return
} }
c.Redirect(http.StatusFound, "/") redirectToPageStatus(c, "/", http.StatusFound)
} }
func renderCompleteTask(c *gin.Context) { func renderCompleteTask(c *gin.Context) {
@ -85,7 +97,7 @@ func renderCompleteTask(c *gin.Context) {
return return
} }
c.Redirect(http.StatusFound, "/") redirectToPageStatus(c, "/", http.StatusFound)
} }
func renderCreateAllowance(c *gin.Context) { func renderCreateAllowance(c *gin.Context) {
@ -122,7 +134,7 @@ func renderCreateAllowance(c *gin.Context) {
return return
} }
c.Redirect(http.StatusFound, "/") redirectToPageStatus(c, "/", http.StatusFound)
} }
func renderCompleteAllowance(c *gin.Context) { func renderCompleteAllowance(c *gin.Context) {
@ -144,11 +156,12 @@ func renderCompleteAllowance(c *gin.Context) {
return return
} }
c.Redirect(http.StatusFound, "/") redirectToPageStatus(c, "/", http.StatusFound)
} }
func getCurrentUser(c *gin.Context) *int { func getCurrentUser(c *gin.Context) *int {
currentUserStr, err := c.Cookie("user") currentUserStr, err := c.Cookie("user")
log.Println("Cookie string:", currentUserStr)
if errors.Is(err, http.ErrNoCookie) { if errors.Is(err, http.ErrNoCookie) {
renderNoUser(c) renderNoUser(c)
return nil return nil
@ -172,7 +185,7 @@ func getCurrentUser(c *gin.Context) *int {
func unsetUserCookie(c *gin.Context) { func unsetUserCookie(c *gin.Context) {
c.SetCookie("user", "", -1, "/", "localhost", false, true) c.SetCookie("user", "", -1, "/", "localhost", false, true)
c.Redirect(http.StatusFound, "/") redirectToPageStatus(c, "/", http.StatusFound)
} }
func renderNoUser(c *gin.Context) { func renderNoUser(c *gin.Context) {

View File

@ -3,9 +3,11 @@
<head> <head>
<title>Allowance Planner 2000</title> <title>Allowance Planner 2000</title>
<style> <style>
<!--
tr:hover { tr:hover {
background-color: #f0f0f0; background-color: #f0f0f0;
} }
-->
</style> </style>
</head> </head>
<body> <body>
@ -27,7 +29,7 @@
{{if ne .CurrentUser 0}} {{if ne .CurrentUser 0}}
<h2>Allowances</h2> <h2>Allowances</h2>
<form action="/createAllowance" method="post"> <form action="/createAllowance" method="post">
<table border="1"> <table border=1>
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
@ -43,7 +45,7 @@
<td></td> <td></td>
<td><label><input type="number" name="target" placeholder="Target"></label></td> <td><label><input type="number" name="target" placeholder="Target"></label></td>
<td><label><input type="number" name="weight" placeholder="Weight"></label></td> <td><label><input type="number" name="weight" placeholder="Weight"></label></td>
<td><button>Create</button></td> <td><input type="submit" value="Create"></td>
</tr> </tr>
{{range .Allowances}} {{range .Allowances}}
{{if eq .ID 0}} {{if eq .ID 0}}
@ -103,7 +105,7 @@
<td><label><input type="text" name="name" placeholder="Name"></label></td> <td><label><input type="text" name="name" placeholder="Name"></label></td>
<td></td> <td></td>
<td><label><input type="number" name="reward" placeholder="Reward"></label></td> <td><label><input type="number" name="reward" placeholder="Reward"></label></td>
<td><button>Create</button></td> <td><input type="submit" value="Create"></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>