log101-dot-dev-services/main.go
log101 fe6c39379a
All checks were successful
/ build-and-push-image (push) Successful in 50s
refactor: split main.go into directories
2024-06-12 21:42:40 +03:00

58 lines
1.3 KiB
Go

package main
import (
"log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
DB "log101-blog-services/db"
"log101-blog-services/handlers"
"log101-blog-services/middleware"
)
func main() {
// Load environment variables
err := godotenv.Load()
if err != nil {
log.Println("Error loading .env file")
}
// initialize db
DB.InitDB()
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")
}
// these are required for htmx to work
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", handlers.GetEmojiForm)
// 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", handlers.PostEmojiForm)
}
r.Run(":8000")
}