2024-05-17 16:01:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-28 19:19:30 +00:00
|
|
|
"log"
|
|
|
|
|
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-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
|
|
|
{
|
2024-06-25 12:11:03 +00:00
|
|
|
// Get the emoji form, postId query parameter is required
|
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)
|
|
|
|
|
2024-06-25 12:11:03 +00:00
|
|
|
// Get the comments for a post, postId query parameter is required
|
2024-06-20 16:40:38 +00:00
|
|
|
blog.GET("/comments", handlers.GetComments)
|
|
|
|
|
|
|
|
// Drop comment on a post, postId and comment body is required
|
|
|
|
blog.POST("/comments", handlers.PostComment)
|
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
|
|
|
}
|