140 lines
3.7 KiB
Go
140 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/joho/godotenv"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
"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 `gorm:"index:idx_anon,unqiue"`
|
|
PostId string `gorm:"index:idx_anon,unique"`
|
|
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 := 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.Println("Error loading .env file")
|
|
}
|
|
|
|
dbPath := os.Getenv("DB_PATH")
|
|
db, err = gorm.Open(sqlite.Open(dbPath), &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", "http://blog.log101.local", "https://blog.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())
|
|
|
|
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"})
|
|
return
|
|
}
|
|
|
|
// Return the html template
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{
|
|
"results": emojiCounter,
|
|
})
|
|
})
|
|
|
|
// Update the user's reaction to post, this handler will
|
|
// add a new entry to the database with anonymized ip
|
|
// updates if user reacted before
|
|
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"})
|
|
return
|
|
}
|
|
|
|
// Add the new emoji reaction to the database
|
|
result := db.Clauses(clause.OnConflict{
|
|
DoUpdates: clause.AssignmentColumns([]string{"emoji"}),
|
|
}).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"})
|
|
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"})
|
|
return
|
|
}
|
|
|
|
// Return the html with the updated emoji counter
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"results": emojiCounter})
|
|
})
|
|
}
|
|
|
|
r.Run(":8000")
|
|
}
|