package main import ( "errors" "log" "net/http" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/joho/godotenv" "gorm.io/driver/sqlite" "gorm.io/gorm" "log101-blog-services/middleware" ) var db *gorm.DB // Emoji counts for each post are retrieved in this format type PostReaction struct { Emoji string TotalCount int } type EmojiReaction struct { UserAnonIp string PostId string Emoji string } // Get the emoji counts foe a given post id func countEmojis(postId string) (map[string]int, error) { postReactions := []PostReaction{} // Get the emoji counts for each emoji // rows, err := db.Query("SELECT emoji, COUNT(*) FROM emoji_clicks WHERE post_id = ? GROUP BY emoji;", postId) rows := db.Model(&EmojiReaction{}).Where("post_id = ?", postId).Select([]string{"emoji", "COUNT(*) as total_count"}).Group("emoji").Find(&postReactions) if rows.Error != nil { return nil, errors.New("couldn't get emoji counts") } // Transform rows to map result := make(map[string]int) for _, reaction := range postReactions { result[reaction.Emoji] = reaction.TotalCount } return result, nil } func main() { // Load environment variables err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) if err != nil { panic("failed to connect database") } db.AutoMigrate(&EmojiReaction{}) r := gin.Default() r.LoadHTMLGlob("templates/*") // CORS configuration corsConfig := cors.DefaultConfig() corsConfig.AllowOrigins = []string{"https://log101.dev"} ginMode := gin.Mode() if ginMode == gin.DebugMode { corsConfig.AllowOrigins = append(corsConfig.AllowOrigins, "http://localhost:4321") } corsConfig.AllowHeaders = []string{"hx-current-url", "hx-request", "hx-target", "hx-trigger"} // Middlewares r.Use(cors.New(corsConfig)) r.Use(middleware.AnonymizeIPMiddleware()) 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 blogForm.GET("/forms/emoji", func(c *gin.Context) { postId := c.Query("postId") // 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, "hxPostUrl": hxPostUrl}) return } // Return the html template c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{ "postId": c.Query("postId"), "results": emojiCounter, "hxPostUrl": hxPostUrl, }) }) // Update the emoji counter, this handler will // add a new entry to the database with anonymized ip // then sums the results to get the emoji counter blogForm.POST("/forms/emoji/post", func(c *gin.Context) { reactedPostId := c.PostForm("postId") reaction := c.PostForm("emojiInput") // Check if parameters are missing if reactedPostId == "" || reaction == "" { c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": reactedPostId, "hxPostUrl": hxPostUrl}) return } // Add the new emoji entry to the database // _, err := db.Exec("INSERT INTO emoji_clicks (user_anon_data, post_id, emoji) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE emoji = ?", c.Request.RemoteAddr, postId, emoji, emoji) result := db.Create(&EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId}) if result.Error != nil { c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": reactedPostId, "hxPostUrl": hxPostUrl}) return } // get emoji counts for each emoji emojiCounter, err := countEmojis(reactedPostId) if err != nil { c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": reactedPostId, "hxPostUrl": hxPostUrl}) return } // Return the html with the updated emoji counter c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": reactedPostId, "results": emojiCounter, "hxPostUrl": hxPostUrl}) }) } r.Run(":8000") }