This commit is contained in:
@@ -2,11 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
// "kontor-api-gin/pkg/handler"
|
||||
"html/template"
|
||||
"kontor-api-gin/assets"
|
||||
"kontor-api-gin/pkg/globals"
|
||||
"kontor-api-gin/pkg/middleware"
|
||||
"kontor-api-gin/pkg/routes"
|
||||
"kontor-api-gin/pkg/schema"
|
||||
"kontor-api-gin/templates"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/cookie"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -27,18 +35,40 @@ func main() {
|
||||
|
||||
// Create Gin router with default middleware
|
||||
// Default includes Logger and Recovery middleware
|
||||
router := gin.Default()
|
||||
//router := gin.Default()
|
||||
// Create a new router without default mmiddleware
|
||||
router := gin.New()
|
||||
templ := template.Must(template.New("").ParseFS(templates.TemplateFiles, "html/*.tmpl"))
|
||||
router.SetHTMLTemplate(templ)
|
||||
|
||||
router.StaticFS("/public", http.FS(assets.StaticFiles))
|
||||
|
||||
// Add CORS middleware for frontend access
|
||||
router.Use(corsMiddleware())
|
||||
// router.Use(middleware.CorsMiddleware)
|
||||
|
||||
// Setup application routes
|
||||
router.Use(gin.LoggerWithConfig(gin.LoggerConfig{
|
||||
SkipPaths: []string{"/health", "/metrics"},
|
||||
}))
|
||||
// Add recovery middleware
|
||||
router.Use(gin.Recovery())
|
||||
|
||||
router.Use(sessions.Sessions("session", cookie.NewStore(globals.Secret)))
|
||||
// Setup api routes
|
||||
api := routes.SetupApiRoutes(router)
|
||||
routes.SetupMediaRoutes(api)
|
||||
|
||||
// Setup public Webapp routes
|
||||
public := router.Group("/")
|
||||
routes.PublicRoutes(public)
|
||||
|
||||
// Setup protected Webapp routes
|
||||
protected := router.Group("/")
|
||||
protected.Use(middleware.AuthRequired)
|
||||
routes.ProtectedRoutes(protected)
|
||||
|
||||
// Health check endpoint
|
||||
router.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "healthy",
|
||||
})
|
||||
})
|
||||
@@ -56,19 +86,3 @@ func main() {
|
||||
|
||||
log.Println("Kontor finished")
|
||||
}
|
||||
|
||||
// corsMiddleware adds CORS headers for cross-origin requests
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user