LocalhostBunny/api/router.go

75 lines
1.8 KiB
Go
Raw Normal View History

2023-12-01 09:56:06 +01:00
package api
import (
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
bunny "locbunny"
"locbunny/api/handler"
"locbunny/logic"
"locbunny/swagger"
)
type Router struct {
app *logic.Application
commonHandler handler.CommonHandler
2023-12-01 13:44:58 +01:00
apiHandler handler.APIHandler
2023-12-01 09:56:06 +01:00
webHandler handler.WebHandler
}
func NewRouter(app *logic.Application) *Router {
return &Router{
app: app,
commonHandler: handler.NewCommonHandler(app),
2023-12-01 13:44:58 +01:00
apiHandler: handler.NewAPIHandler(app),
2023-12-01 09:56:06 +01:00
webHandler: handler.NewWebHandler(app),
}
}
// Init swaggerdocs
//
// @title LocalHostBunny
// @version 1.0
// @host localhost
//
// @BasePath /api/v1/
func (r *Router) Init(e *ginext.GinWrapper) {
api := e.Routes().Group("/api").Group(fmt.Sprintf("/v%d", bunny.APILevel))
// ================ General ================
api.Any("/ping").Handle(r.commonHandler.Ping)
api.GET("/health").Handle(r.commonHandler.Health)
api.POST("/sleep/:secs").Handle(r.commonHandler.Sleep)
// ================ Swagger ================
docs := e.Routes().Group("/documentation")
{
docs.GET("/swagger").Handle(ginext.RedirectTemporary("/documentation/swagger/"))
docs.GET("/swagger/*sub").Handle(swagger.Handle)
}
2023-12-01 13:44:58 +01:00
// ================ Website ================
e.Routes().GET("/").Handle(r.webHandler.ServeIndexHTML)
e.Routes().GET("/index.html").Handle(r.webHandler.ServeIndexHTML)
e.Routes().GET("/scripts/script.js").Handle(r.webHandler.ServeScriptJS)
e.Routes().GET("/:fp1").Handle(r.webHandler.ServeAssets)
e.Routes().GET("/:fp1/:fp2").Handle(r.webHandler.ServeAssets)
e.Routes().GET("/:fp1/:fp2/:fp3").Handle(r.webHandler.ServeAssets)
2023-12-01 09:56:06 +01:00
// ================ API ================
2023-12-01 13:44:58 +01:00
api.GET("/server").Handle(r.apiHandler.ListServer)
2023-12-01 09:56:06 +01:00
// ================ ================
if r.app.Config.Custom404 {
e.NoRoute(r.commonHandler.NoRoute)
}
}