diff options
| author | Christian Krinitsin <mail@krinitsin.com> | 2025-04-21 17:10:39 +0200 |
|---|---|---|
| committer | Christian Krinitsin <mail@krinitsin.com> | 2025-04-21 17:10:39 +0200 |
| commit | 996f7a0d798452bd7ca134ada2871bc24c91e519 (patch) | |
| tree | d7ed2372a2e2b3bae76682af46fb15dd8c845a5e | |
| parent | 9594dac4ac43b4e038d307da59709887e24eb441 (diff) | |
| download | shopping-list-996f7a0d798452bd7ca134ada2871bc24c91e519.tar.gz shopping-list-996f7a0d798452bd7ca134ada2871bc24c91e519.zip | |
add a base_path environment variable
| -rw-r--r-- | handlers/shopping_list.go | 22 | ||||
| -rw-r--r-- | templates/template.html | 6 |
2 files changed, 19 insertions, 9 deletions
diff --git a/handlers/shopping_list.go b/handlers/shopping_list.go index 5ce0845..959c309 100644 --- a/handlers/shopping_list.go +++ b/handlers/shopping_list.go @@ -2,13 +2,22 @@ package shopping_list import ( "net/http" + "os" "github.com/ckrinitsin/shopping-list/models" "github.com/gin-gonic/gin" ) -func LoadElements(c *gin.Context) { +func getBasePath() string { + basePath := os.Getenv("BASE_PATH") + if basePath == "" { + basePath = "/" + } + return basePath +} + +func LoadElements(c *gin.Context) { title := "Shopping List" var entries []models.Entry @@ -24,8 +33,9 @@ func LoadElements(c *gin.Context) { } c.HTML(http.StatusOK, "template.html", gin.H{ - "name": title, - "entries": entries, + "name": title, + "entries": entries, + "base_path": getBasePath(), }) } @@ -46,13 +56,13 @@ func CreateEntry(c *gin.Context) { return } - c.Redirect(http.StatusFound, "/") + c.Redirect(http.StatusFound, getBasePath() + "/") } func DeleteEntries(c *gin.Context) { models.DB.Delete(&models.Entry{}, "checked = 1") - c.Redirect(http.StatusFound, "/") + c.Redirect(http.StatusFound, getBasePath() + "/") } func ToggleEntry(c *gin.Context) { @@ -79,5 +89,5 @@ func ToggleEntry(c *gin.Context) { return } - c.Redirect(http.StatusFound, "/") + c.Redirect(http.StatusFound, getBasePath() + "/") } diff --git a/templates/template.html b/templates/template.html index 5c0c84f..bbb10cb 100644 --- a/templates/template.html +++ b/templates/template.html @@ -176,7 +176,7 @@ {{ end }} {{ .Text }} - <form action="toggle" method="POST"> + <form action="{{ $.base_path }}toggle" method="POST"> <input type="hidden" name="id" value="{{ .ID }}" /> <button type="submit" /> </form> @@ -184,13 +184,13 @@ {{ end }} </ul> - <form action="create" method="POST"> + <form action="{{ .base_path }}create" method="POST"> <div class="input-container"> <input type="text" id="newItem" name="newItem" tabindex="0"/> </div> </form> - <form action="delete" method="POST"> + <form action="{{ .base_path }}delete" method="POST"> <button type="submit" class="delete-button">Delete Selected Items</button> </form> |