static-hoster/src/router/router.go

37 lines
820 B
Go
Raw Normal View History

2023-01-24 14:50:38 +00:00
package router
import (
2023-01-26 18:21:07 +00:00
"fmt"
2023-01-24 14:50:38 +00:00
"net/http"
"github.com/gin-gonic/gin"
"nikurasu.gay/static-hoster/api"
2023-01-25 21:14:27 +00:00
"nikurasu.gay/static-hoster/envloader"
2023-01-24 14:50:38 +00:00
"nikurasu.gay/static-hoster/middleware/auth"
)
2023-01-25 21:14:27 +00:00
func Create(env *envloader.Environment) *gin.Engine {
2023-01-24 14:50:38 +00:00
router := gin.Default()
2023-01-26 17:52:03 +00:00
apiRoutes := router.Group("/api", auth.AuthMiddleware(env))
2023-01-24 14:50:38 +00:00
{
2023-01-26 17:52:03 +00:00
apiRoutes.POST("/update", api.PostUpdate(env))
2023-01-24 14:50:38 +00:00
}
// Ping test
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
router.Static(env.BaseRoute, env.StaticDir)
router.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusPermanentRedirect, env.BaseRoute)
})
2023-01-26 17:52:03 +00:00
2023-01-26 18:21:07 +00:00
router.LoadHTMLGlob(fmt.Sprintf("%s404.html", env.StaticDir))
router.NoRoute(func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, "404.html", gin.H{})
})
2023-01-24 14:50:38 +00:00
return router
}