Improve compatibility with ancient browsers

This commit is contained in:
Sebastiaan de Schaetzen 2025-05-25 19:25:28 +02:00
parent f35e35e1de
commit 1674988751
2 changed files with 20 additions and 2 deletions

View File

@ -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)
}

View File

@ -3,6 +3,7 @@ package main
import (
"errors"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strconv"
)
@ -26,11 +27,17 @@ func loadWebEndpoints(router *gin.Engine) {
router.GET("/completeAllowance", renderCompleteAllowance)
}
func redirectToPage(c *gin.Context, page string) {
scheme := c.Request.URL.Scheme
c.Redirect(http.StatusSeeOther, scheme+domain+page)
}
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) {
@ -149,6 +156,7 @@ func renderCompleteAllowance(c *gin.Context) {
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