SimpleCloudNotifier/scnserver/api/router.go

154 lines
5.6 KiB
Go
Raw Normal View History

2022-11-13 19:17:07 +01:00
package api
import (
2022-12-20 13:55:09 +01:00
"blackforestbytes.com/simplecloudnotifier/api/ginext"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
2022-11-13 19:17:07 +01:00
"blackforestbytes.com/simplecloudnotifier/api/handler"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/swagger"
"github.com/gin-gonic/gin"
)
type Router struct {
app *logic.Application
2022-11-18 21:25:40 +01:00
commonHandler handler.CommonHandler
compatHandler handler.CompatHandler
websiteHandler handler.WebsiteHandler
apiHandler handler.APIHandler
messageHandler handler.MessageHandler
2022-11-13 19:17:07 +01:00
}
func NewRouter(app *logic.Application) *Router {
return &Router{
app: app,
2022-11-18 21:25:40 +01:00
commonHandler: handler.NewCommonHandler(app),
compatHandler: handler.NewCompatHandler(app),
websiteHandler: handler.NewWebsiteHandler(app),
apiHandler: handler.NewAPIHandler(app),
messageHandler: handler.NewMessageHandler(app),
2022-11-13 19:17:07 +01:00
}
}
// Init swaggerdocs
2022-11-23 19:32:23 +01:00
//
2022-11-13 19:17:07 +01:00
// @title SimpleCloudNotifier API
// @version 2.0
// @description API for SCN
// @host scn.blackforestbytes.com
2022-11-23 19:32:23 +01:00
//
// @tag.name External
// @tag.name API-v1
// @tag.name API-v2
2022-11-30 17:58:04 +01:00
// @tag.name Common
2022-11-23 19:32:23 +01:00
//
2022-11-18 21:25:40 +01:00
// @BasePath /
2022-11-13 19:17:07 +01:00
func (r *Router) Init(e *gin.Engine) {
2022-11-18 21:25:40 +01:00
// ================ General ================
2022-11-23 19:32:23 +01:00
commonAPI := e.Group("/api")
{
commonAPI.Any("/ping", ginresp.Wrap(r.commonHandler.Ping))
commonAPI.POST("/db-test", ginresp.Wrap(r.commonHandler.DatabaseTest))
commonAPI.GET("/health", ginresp.Wrap(r.commonHandler.Health))
2022-11-23 20:21:49 +01:00
commonAPI.POST("/sleep/:secs", ginresp.Wrap(r.commonHandler.Sleep))
2022-11-23 19:32:23 +01:00
}
2022-11-18 21:25:40 +01:00
// ================ Swagger ================
2022-11-23 19:32:23 +01:00
docs := e.Group("/documentation")
{
docs.GET("/swagger", ginext.RedirectTemporary("/documentation/swagger/"))
2022-12-21 11:03:31 +01:00
docs.GET("/swagger/*sub", ginresp.Wrap(swagger.Handle))
2022-11-23 19:32:23 +01:00
}
2022-11-18 21:25:40 +01:00
// ================ Website ================
2022-11-23 19:32:23 +01:00
frontend := e.Group("")
{
frontend.GET("/", ginresp.Wrap(r.websiteHandler.Index))
frontend.GET("/index.php", ginresp.Wrap(r.websiteHandler.Index))
frontend.GET("/index.html", ginresp.Wrap(r.websiteHandler.Index))
frontend.GET("/index", ginresp.Wrap(r.websiteHandler.Index))
2022-11-18 21:25:40 +01:00
2022-11-23 19:32:23 +01:00
frontend.GET("/api", ginresp.Wrap(r.websiteHandler.APIDocs))
frontend.GET("/api.php", ginresp.Wrap(r.websiteHandler.APIDocs))
frontend.GET("/api.html", ginresp.Wrap(r.websiteHandler.APIDocs))
2022-11-18 21:25:40 +01:00
2022-11-23 19:32:23 +01:00
frontend.GET("/api_more", ginresp.Wrap(r.websiteHandler.APIDocsMore))
frontend.GET("/api_more.php", ginresp.Wrap(r.websiteHandler.APIDocsMore))
frontend.GET("/api_more.html", ginresp.Wrap(r.websiteHandler.APIDocsMore))
2022-11-18 21:25:40 +01:00
2022-11-23 19:32:23 +01:00
frontend.GET("/message_sent", ginresp.Wrap(r.websiteHandler.MessageSent))
frontend.GET("/message_sent.php", ginresp.Wrap(r.websiteHandler.MessageSent))
frontend.GET("/message_sent.html", ginresp.Wrap(r.websiteHandler.MessageSent))
2022-11-18 21:25:40 +01:00
2022-11-23 19:32:23 +01:00
frontend.GET("/favicon.ico", ginresp.Wrap(r.websiteHandler.FaviconIco))
frontend.GET("/favicon.png", ginresp.Wrap(r.websiteHandler.FaviconPNG))
2022-11-18 21:25:40 +01:00
2022-11-23 19:32:23 +01:00
frontend.GET("/js/:fn", ginresp.Wrap(r.websiteHandler.Javascript))
frontend.GET("/css/:fn", ginresp.Wrap(r.websiteHandler.CSS))
}
2022-11-18 21:25:40 +01:00
// ================ Compat (v1) ================
compat := e.Group("/api/")
{
compat.GET("/register.php", ginresp.Wrap(r.compatHandler.Register))
compat.GET("/info.php", ginresp.Wrap(r.compatHandler.Info))
compat.GET("/ack.php", ginresp.Wrap(r.compatHandler.Ack))
compat.GET("/requery.php", ginresp.Wrap(r.compatHandler.Requery))
compat.GET("/update.php", ginresp.Wrap(r.compatHandler.Update))
compat.GET("/expand.php", ginresp.Wrap(r.compatHandler.Expand))
compat.GET("/upgrade.php", ginresp.Wrap(r.compatHandler.Upgrade))
}
// ================ Manage API ================
2022-11-23 19:32:23 +01:00
apiv2 := e.Group("/api/")
2022-11-18 21:25:40 +01:00
{
2022-11-20 03:06:08 +01:00
apiv2.POST("/users", ginresp.Wrap(r.apiHandler.CreateUser))
apiv2.GET("/users/:uid", ginresp.Wrap(r.apiHandler.GetUser))
apiv2.PATCH("/users/:uid", ginresp.Wrap(r.apiHandler.UpdateUser))
apiv2.GET("/users/:uid/clients", ginresp.Wrap(r.apiHandler.ListClients))
apiv2.GET("/users/:uid/clients/:cid", ginresp.Wrap(r.apiHandler.GetClient))
apiv2.POST("/users/:uid/clients", ginresp.Wrap(r.apiHandler.AddClient))
apiv2.DELETE("/users/:uid/clients/:cid", ginresp.Wrap(r.apiHandler.DeleteClient))
apiv2.GET("/users/:uid/channels", ginresp.Wrap(r.apiHandler.ListChannels))
2022-12-14 14:29:59 +01:00
apiv2.POST("/users/:uid/channels", ginresp.Wrap(r.apiHandler.CreateChannel))
apiv2.GET("/users/:uid/channels/:cid", ginresp.Wrap(r.apiHandler.GetChannel))
apiv2.PATCH("/users/:uid/channels/:cid", ginresp.Wrap(r.apiHandler.UpdateChannel))
2022-11-20 00:30:30 +01:00
apiv2.GET("/users/:uid/channels/:cid/messages", ginresp.Wrap(r.apiHandler.ListChannelMessages))
apiv2.GET("/users/:uid/channels/:cid/subscriptions", ginresp.Wrap(r.apiHandler.ListChannelSubscriptions))
apiv2.GET("/users/:uid/subscriptions", ginresp.Wrap(r.apiHandler.ListUserSubscriptions))
apiv2.POST("/users/:uid/subscriptions", ginresp.Wrap(r.apiHandler.CreateSubscription))
apiv2.GET("/users/:uid/subscriptions/:sid", ginresp.Wrap(r.apiHandler.GetSubscription))
apiv2.DELETE("/users/:uid/subscriptions/:sid", ginresp.Wrap(r.apiHandler.CancelSubscription))
apiv2.PATCH("/users/:uid/subscriptions/:sid", ginresp.Wrap(r.apiHandler.UpdateSubscription))
2022-11-18 21:25:40 +01:00
apiv2.GET("/messages", ginresp.Wrap(r.apiHandler.ListMessages))
apiv2.GET("/messages/:mid", ginresp.Wrap(r.apiHandler.GetMessage))
apiv2.DELETE("/messages/:mid", ginresp.Wrap(r.apiHandler.DeleteMessage))
}
// ================ Send API ================
sendAPI := e.Group("")
{
sendAPI.POST("/", ginresp.Wrap(r.messageHandler.SendMessage))
sendAPI.POST("/send", ginresp.Wrap(r.messageHandler.SendMessage))
2022-11-20 03:06:08 +01:00
sendAPI.POST("/send.php", ginresp.Wrap(r.messageHandler.SendMessageCompat))
2022-11-18 21:25:40 +01:00
}
if r.app.Config.ReturnRawErrors {
e.NoRoute(ginresp.Wrap(r.commonHandler.NoRoute))
}
2022-11-13 19:17:07 +01:00
}