2024-05-17 16:01:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-28 19:19:30 +00:00
|
|
|
"log"
|
2024-06-18 18:36:30 +00:00
|
|
|
"net/http"
|
2024-05-28 19:19:30 +00:00
|
|
|
|
2024-05-28 17:17:30 +00:00
|
|
|
"github.com/gin-contrib/cors"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-06-12 18:42:40 +00:00
|
|
|
|
2024-05-31 14:45:28 +00:00
|
|
|
"github.com/joho/godotenv"
|
2024-05-30 15:42:33 +00:00
|
|
|
|
2024-06-12 18:42:40 +00:00
|
|
|
DB "log101-blog-services/db"
|
|
|
|
"log101-blog-services/handlers"
|
2024-05-30 15:42:33 +00:00
|
|
|
"log101-blog-services/middleware"
|
2024-06-18 18:36:30 +00:00
|
|
|
"log101-blog-services/models"
|
2024-05-17 16:01:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-06-02 08:44:39 +00:00
|
|
|
// Load environment variables
|
2024-05-31 14:45:28 +00:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
2024-06-11 09:30:33 +00:00
|
|
|
log.Println("Error loading .env file")
|
2024-05-31 14:45:28 +00:00
|
|
|
}
|
|
|
|
|
2024-06-12 18:42:40 +00:00
|
|
|
// initialize db
|
|
|
|
DB.InitDB()
|
2024-05-28 19:19:30 +00:00
|
|
|
|
2024-05-28 17:17:30 +00:00
|
|
|
r := gin.Default()
|
|
|
|
r.LoadHTMLGlob("templates/*")
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// CORS configuration
|
2024-05-28 17:17:30 +00:00
|
|
|
corsConfig := cors.DefaultConfig()
|
2024-06-11 09:05:44 +00:00
|
|
|
corsConfig.AllowOrigins = []string{"https://log101.dev", "http://blog.log101.local", "https://blog.log101.dev"}
|
2024-05-30 10:53:20 +00:00
|
|
|
|
|
|
|
ginMode := gin.Mode()
|
|
|
|
if ginMode == gin.DebugMode {
|
|
|
|
corsConfig.AllowOrigins = append(corsConfig.AllowOrigins, "http://localhost:4321")
|
|
|
|
}
|
|
|
|
|
2024-06-12 18:42:40 +00:00
|
|
|
// these are required for htmx to work
|
2024-06-10 04:50:15 +00:00
|
|
|
corsConfig.AllowHeaders = []string{"hx-current-url", "hx-request", "hx-target", "hx-trigger"}
|
2024-05-30 15:42:33 +00:00
|
|
|
|
|
|
|
// Middlewares
|
2024-05-28 17:17:30 +00:00
|
|
|
r.Use(cors.New(corsConfig))
|
2024-05-30 15:42:33 +00:00
|
|
|
r.Use(middleware.AnonymizeIPMiddleware())
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-06-18 18:36:30 +00:00
|
|
|
blog := r.Group("/api/blog/")
|
2024-05-30 10:30:32 +00:00
|
|
|
{
|
|
|
|
// Get the emoji form, you must provide postId query parameter
|
2024-06-18 18:36:30 +00:00
|
|
|
blog.GET("/forms/emoji", handlers.GetEmojiForm)
|
2024-05-28 19:19:30 +00:00
|
|
|
|
2024-06-11 07:41:06 +00:00
|
|
|
// Update the user's reaction to post, this handler will
|
2024-05-30 10:30:32 +00:00
|
|
|
// add a new entry to the database with anonymized ip
|
2024-06-11 07:41:06 +00:00
|
|
|
// updates if user reacted before
|
2024-06-18 18:36:30 +00:00
|
|
|
blog.POST("/forms/emoji", handlers.PostEmojiForm)
|
|
|
|
|
|
|
|
blog.GET("/comments", func(c *gin.Context) {
|
|
|
|
db := DB.GetDB()
|
|
|
|
postId := c.Query("postId")
|
|
|
|
comments := []models.Comment{}
|
|
|
|
|
2024-06-19 13:42:43 +00:00
|
|
|
if postId == "" {
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-06-18 18:36:30 +00:00
|
|
|
|
2024-06-19 13:42:43 +00:00
|
|
|
rows := db.Where("post_id = ?", postId).Find(&comments)
|
2024-06-18 18:36:30 +00:00
|
|
|
if rows.Error != nil {
|
2024-06-19 13:42:43 +00:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
2024-06-18 18:36:30 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 13:42:43 +00:00
|
|
|
c.HTML(http.StatusOK, "comments.tmpl", gin.H{"Comments": comments})
|
2024-06-18 18:36:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
blog.POST("/comments", func(c *gin.Context) {
|
|
|
|
db := DB.GetDB()
|
|
|
|
postId := c.PostForm("postId")
|
|
|
|
commentBody := c.PostForm("commentBody")
|
|
|
|
username := c.PostForm("username")
|
|
|
|
|
|
|
|
if postId == "" || commentBody == "" {
|
2024-06-19 13:42:43 +00:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
2024-06-18 18:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := db.Create(&models.Comment{Body: commentBody, PostId: postId, Username: username})
|
|
|
|
if result.Error != nil {
|
2024-06-19 13:42:43 +00:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
2024-06-18 18:36:30 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 13:42:43 +00:00
|
|
|
c.HTML(http.StatusOK, "comment.tmpl", gin.H{"Username": username, "Body": commentBody})
|
2024-06-18 18:36:30 +00:00
|
|
|
})
|
|
|
|
|
2024-05-30 10:30:32 +00:00
|
|
|
}
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-05-30 09:52:18 +00:00
|
|
|
r.Run(":8000")
|
2024-05-17 16:01:33 +00:00
|
|
|
}
|