summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--handlers/shopping_list.go22
-rw-r--r--templates/template.html6
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>