From b13e73e72694ce268bdd7478bc5ba953a6f77364 Mon Sep 17 00:00:00 2001 From: log101 Date: Thu, 30 May 2024 14:43:50 +0300 Subject: [PATCH] chore: put hx post url in a variable --- main.go | 22 +++++++++++++++------- templates/emoji_form.tmpl | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 30b91d4..66acaae 100644 --- a/main.go +++ b/main.go @@ -103,6 +103,13 @@ func main() { corsConfig.AllowHeaders = []string{"hx-current-url", "hx-request"} r.Use(cors.New(corsConfig)) + var hxPostUrl string + if ginMode == gin.DebugMode { + hxPostUrl = "http://localhost:8000/blog/api/forms/emoji/post" + } else { + hxPostUrl = "https://log101.dev/blog/api/forms/emoji/post" + } + blogForm := r.Group("/blog/api/") { // Get the emoji form, you must provide postId query parameter @@ -112,14 +119,15 @@ func main() { // get emoji counts for each emoji emojiCounter, err := countEmojis(postId) if err != nil { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId, "hxPostUrl": hxPostUrl}) return } // Return the html template c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{ - "postId": c.Query("postId"), - "results": emojiCounter, + "postId": c.Query("postId"), + "results": emojiCounter, + "hxPostUrl": hxPostUrl, }) }) @@ -132,26 +140,26 @@ func main() { // Check if parameters are missing if postId == "" || emoji == "" { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postId}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postId, "hxPostUrl": hxPostUrl}) return } // Add the new emoji entry to the database _, err := db.Exec("INSERT INTO emoji_clicks (post_id, emoji) VALUES (?, ?)", postId, emoji) if err != nil { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postId}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postId, "hxPostUrl": hxPostUrl}) return } // get emoji counts for each emoji emojiCounter, err := countEmojis(postId) if err != nil { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId, "hxPostUrl": hxPostUrl}) return } // Return the html with the updated emoji counter - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": emojiCounter}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": emojiCounter, "hxPostUrl": hxPostUrl}) }) } diff --git a/templates/emoji_form.tmpl b/templates/emoji_form.tmpl index ef45f51..fb86881 100644 --- a/templates/emoji_form.tmpl +++ b/templates/emoji_form.tmpl @@ -1,4 +1,4 @@ -
+
{{ range .results }}