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"
|
2023-01-14 00:48:51 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
2022-11-13 19:17:07 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/swagger"
|
2023-01-14 00:48:51 +01:00
|
|
|
"errors"
|
2022-11-13 19:17:07 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-01-14 00:48:51 +01:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/go-playground/validator/v10"
|
2022-11-13 19:17:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @title SimpleCloudNotifier API
|
|
|
|
// @version 2.0
|
|
|
|
// @description API for SCN
|
|
|
|
// @host scn.blackforestbytes.com
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @tag.name External
|
|
|
|
// @tag.name API-v1
|
|
|
|
// @tag.name API-v2
|
|
|
|
// @tag.name Common
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @BasePath /
|
2023-01-14 00:48:51 +01:00
|
|
|
func (r *Router) Init(e *gin.Engine) error {
|
|
|
|
|
|
|
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
|
|
|
err := v.RegisterValidation("entityid", models.ValidateEntityID, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("failed to add validators - wrong engine")
|
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2023-01-27 10:04:06 +01:00
|
|
|
// ================ General (unversioned) ================
|
2022-11-18 21:25:40 +01:00
|
|
|
|
2022-11-23 19:32:23 +01:00
|
|
|
commonAPI := e.Group("/api")
|
|
|
|
{
|
2023-01-13 17:17:17 +01:00
|
|
|
commonAPI.Any("/ping", r.Wrap(r.commonHandler.Ping))
|
|
|
|
commonAPI.POST("/db-test", r.Wrap(r.commonHandler.DatabaseTest))
|
|
|
|
commonAPI.GET("/health", r.Wrap(r.commonHandler.Health))
|
|
|
|
commonAPI.POST("/sleep/:secs", r.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/"))
|
2023-01-13 17:17:17 +01:00
|
|
|
docs.GET("/swagger/*sub", r.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("")
|
|
|
|
{
|
2023-01-13 17:17:17 +01:00
|
|
|
frontend.GET("/", r.Wrap(r.websiteHandler.Index))
|
|
|
|
frontend.GET("/index.php", r.Wrap(r.websiteHandler.Index))
|
|
|
|
frontend.GET("/index.html", r.Wrap(r.websiteHandler.Index))
|
|
|
|
frontend.GET("/index", r.Wrap(r.websiteHandler.Index))
|
2022-11-18 21:25:40 +01:00
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
frontend.GET("/api", r.Wrap(r.websiteHandler.APIDocs))
|
|
|
|
frontend.GET("/api.php", r.Wrap(r.websiteHandler.APIDocs))
|
|
|
|
frontend.GET("/api.html", r.Wrap(r.websiteHandler.APIDocs))
|
2022-11-18 21:25:40 +01:00
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
frontend.GET("/api_more", r.Wrap(r.websiteHandler.APIDocsMore))
|
|
|
|
frontend.GET("/api_more.php", r.Wrap(r.websiteHandler.APIDocsMore))
|
|
|
|
frontend.GET("/api_more.html", r.Wrap(r.websiteHandler.APIDocsMore))
|
2022-11-18 21:25:40 +01:00
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
frontend.GET("/message_sent", r.Wrap(r.websiteHandler.MessageSent))
|
|
|
|
frontend.GET("/message_sent.php", r.Wrap(r.websiteHandler.MessageSent))
|
|
|
|
frontend.GET("/message_sent.html", r.Wrap(r.websiteHandler.MessageSent))
|
2022-11-18 21:25:40 +01:00
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
frontend.GET("/favicon.ico", r.Wrap(r.websiteHandler.FaviconIco))
|
|
|
|
frontend.GET("/favicon.png", r.Wrap(r.websiteHandler.FaviconPNG))
|
2022-11-18 21:25:40 +01:00
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
frontend.GET("/js/:fn", r.Wrap(r.websiteHandler.Javascript))
|
|
|
|
frontend.GET("/css/:fn", r.Wrap(r.websiteHandler.CSS))
|
2022-11-23 19:32:23 +01:00
|
|
|
}
|
2022-11-18 21:25:40 +01:00
|
|
|
|
|
|
|
// ================ Compat (v1) ================
|
|
|
|
|
2023-01-14 00:48:51 +01:00
|
|
|
compat := e.Group("/api")
|
2022-11-18 21:25:40 +01:00
|
|
|
{
|
2023-01-13 17:17:17 +01:00
|
|
|
compat.GET("/register.php", r.Wrap(r.compatHandler.Register))
|
|
|
|
compat.GET("/info.php", r.Wrap(r.compatHandler.Info))
|
|
|
|
compat.GET("/ack.php", r.Wrap(r.compatHandler.Ack))
|
|
|
|
compat.GET("/requery.php", r.Wrap(r.compatHandler.Requery))
|
|
|
|
compat.GET("/update.php", r.Wrap(r.compatHandler.Update))
|
|
|
|
compat.GET("/expand.php", r.Wrap(r.compatHandler.Expand))
|
|
|
|
compat.GET("/upgrade.php", r.Wrap(r.compatHandler.Upgrade))
|
2022-11-18 21:25:40 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:04:06 +01:00
|
|
|
// ================ Manage API (v2) ================
|
2022-11-18 21:25:40 +01:00
|
|
|
|
2023-01-27 10:04:06 +01:00
|
|
|
apiv2 := e.Group("/api/v2/")
|
2022-11-18 21:25:40 +01:00
|
|
|
{
|
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
apiv2.POST("/users", r.Wrap(r.apiHandler.CreateUser))
|
|
|
|
apiv2.GET("/users/:uid", r.Wrap(r.apiHandler.GetUser))
|
|
|
|
apiv2.PATCH("/users/:uid", r.Wrap(r.apiHandler.UpdateUser))
|
|
|
|
|
2023-04-21 21:45:16 +02:00
|
|
|
apiv2.GET("/users/:uid/keys", r.Wrap(r.apiHandler.ListUserKeys))
|
|
|
|
apiv2.POST("/users/:uid/keys", r.Wrap(r.apiHandler.CreateUserKey))
|
|
|
|
apiv2.GET("/users/:uid/keys/:kid", r.Wrap(r.apiHandler.GetUserKey))
|
|
|
|
apiv2.PATCH("/users/:uid/keys/:kid", r.Wrap(r.apiHandler.UpdateUserKey))
|
|
|
|
apiv2.DELETE("/users/:uid/keys/:kid", r.Wrap(r.apiHandler.DeleteUserKey))
|
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
apiv2.GET("/users/:uid/clients", r.Wrap(r.apiHandler.ListClients))
|
|
|
|
apiv2.GET("/users/:uid/clients/:cid", r.Wrap(r.apiHandler.GetClient))
|
|
|
|
apiv2.POST("/users/:uid/clients", r.Wrap(r.apiHandler.AddClient))
|
|
|
|
apiv2.DELETE("/users/:uid/clients/:cid", r.Wrap(r.apiHandler.DeleteClient))
|
|
|
|
|
|
|
|
apiv2.GET("/users/:uid/channels", r.Wrap(r.apiHandler.ListChannels))
|
|
|
|
apiv2.POST("/users/:uid/channels", r.Wrap(r.apiHandler.CreateChannel))
|
|
|
|
apiv2.GET("/users/:uid/channels/:cid", r.Wrap(r.apiHandler.GetChannel))
|
|
|
|
apiv2.PATCH("/users/:uid/channels/:cid", r.Wrap(r.apiHandler.UpdateChannel))
|
|
|
|
apiv2.GET("/users/:uid/channels/:cid/messages", r.Wrap(r.apiHandler.ListChannelMessages))
|
|
|
|
apiv2.GET("/users/:uid/channels/:cid/subscriptions", r.Wrap(r.apiHandler.ListChannelSubscriptions))
|
|
|
|
|
|
|
|
apiv2.GET("/users/:uid/subscriptions", r.Wrap(r.apiHandler.ListUserSubscriptions))
|
|
|
|
apiv2.POST("/users/:uid/subscriptions", r.Wrap(r.apiHandler.CreateSubscription))
|
|
|
|
apiv2.GET("/users/:uid/subscriptions/:sid", r.Wrap(r.apiHandler.GetSubscription))
|
|
|
|
apiv2.DELETE("/users/:uid/subscriptions/:sid", r.Wrap(r.apiHandler.CancelSubscription))
|
|
|
|
apiv2.PATCH("/users/:uid/subscriptions/:sid", r.Wrap(r.apiHandler.UpdateSubscription))
|
|
|
|
|
|
|
|
apiv2.GET("/messages", r.Wrap(r.apiHandler.ListMessages))
|
|
|
|
apiv2.GET("/messages/:mid", r.Wrap(r.apiHandler.GetMessage))
|
|
|
|
apiv2.DELETE("/messages/:mid", r.Wrap(r.apiHandler.DeleteMessage))
|
2022-11-18 21:25:40 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:04:06 +01:00
|
|
|
// ================ Send API (unversioned) ================
|
2022-11-18 21:25:40 +01:00
|
|
|
|
|
|
|
sendAPI := e.Group("")
|
|
|
|
{
|
2023-01-13 17:17:17 +01:00
|
|
|
sendAPI.POST("/", r.Wrap(r.messageHandler.SendMessage))
|
|
|
|
sendAPI.POST("/send", r.Wrap(r.messageHandler.SendMessage))
|
|
|
|
sendAPI.POST("/send.php", r.Wrap(r.messageHandler.SendMessageCompat))
|
2022-11-18 21:25:40 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:04:06 +01:00
|
|
|
// ================
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
if r.app.Config.ReturnRawErrors {
|
2023-01-13 17:17:17 +01:00
|
|
|
e.NoRoute(r.Wrap(r.commonHandler.NoRoute))
|
2022-11-18 21:25:40 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 00:48:51 +01:00
|
|
|
// ================
|
|
|
|
|
|
|
|
return nil
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
2023-01-13 17:17:17 +01:00
|
|
|
|
|
|
|
func (r *Router) Wrap(fn ginresp.WHandlerFunc) gin.HandlerFunc {
|
|
|
|
return ginresp.Wrap(r.app, fn)
|
|
|
|
}
|