Files
CatsOfMastodonGo/internal/server/router.go

55 lines
1.8 KiB
Go

package server
import (
"CatsOfMastodonBotGo/internal/auth"
"CatsOfMastodonBotGo/internal/web/handlers"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func SetupRouter() *gin.Engine {
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"https://extra-mama-chiz.surge.sh"}, // Just for test
AllowMethods: []string{"POST", "GET", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
AllowCredentials: true,
}))
r.LoadHTMLGlob("internal/web/templates/**/*")
auth.InitJwtTokenGenerator() // Must be befor initializing admin handler, otherwise 'panic: runtime error: invalid memory address or nil pointer dereference'
handlers.InitAdminDashboardHandler()
handlers.InitApiEndpointHandler()
handlers.InitEmbedCardHandler()
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "home/index.html", nil)
})
r.GET("/embed", handlers.EmbedCardHandlerInstance.GetEmbedCard)
admin := r.Group("/admin")
// My man, this is done way more efficient and fast in .NET, specially the authentication part
admin.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "admin/index.html", nil)
})
admin.GET("/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "admin/login.html", nil)
})
adminApi := admin.Group("/api")
adminApi.POST("/login", handlers.AdminDashboardHandlerInstance.Login)
adminApi.GET("/getmedia", auth.JwtTokenGeneratorInstance.GinMiddleware(), handlers.AdminDashboardHandlerInstance.GetMedia)
adminApi.POST("/approve", auth.JwtTokenGeneratorInstance.GinMiddleware(), handlers.AdminDashboardHandlerInstance.ApproveMedia)
adminApi.POST("/reject", auth.JwtTokenGeneratorInstance.GinMiddleware(), handlers.AdminDashboardHandlerInstance.RejectMedia)
api := r.Group("/api")
api.GET("/post/random", handlers.ApiEndpointHandlerInstance.GetRandomPost)
return r
}