Refactor server to go-sqlite and ginext [WIP]

This commit is contained in:
2024-07-15 17:26:55 +02:00
parent e6fbf85e6e
commit 55d0dea835
39 changed files with 880 additions and 996 deletions

View File

@@ -1,7 +1,6 @@
package api
import (
"blackforestbytes.com/simplecloudnotifier/api/ginext"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/api/handler"
"blackforestbytes.com/simplecloudnotifier/logic"
@@ -11,6 +10,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
)
type Router struct {
@@ -50,7 +50,7 @@ func NewRouter(app *logic.Application) *Router {
// @tag.name Common
//
// @BasePath /
func (r *Router) Init(e *gin.Engine) error {
func (r *Router) Init(e *ginext.GinWrapper) error {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
err := v.RegisterValidation("entityid", models.ValidateEntityID, true)
@@ -61,131 +61,129 @@ func (r *Router) Init(e *gin.Engine) error {
return errors.New("failed to add validators - wrong engine")
}
wrap := func(fn ginext.WHandlerFunc) ginext.WHandlerFunc{return Wrap(r.app, fn)}
// ================ General (unversioned) ================
commonAPI := e.Group("/api")
commonAPI := e.Routes().Group("/api")
{
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))
commonAPI.Any("/ping").Handle(wrap(r.commonHandler.Ping))
commonAPI.POST("/db-test").Handle(wrap(r.commonHandler.DatabaseTest))
commonAPI.GET("/health").Handle(wrap(r.commonHandler.Health))
commonAPI.POST("/sleep/:secs").Handle(wrap(r.commonHandler.Sleep))
}
// ================ Swagger ================
docs := e.Group("/documentation")
docs := e.Routes().Group("/documentation")
{
docs.GET("/swagger", ginext.RedirectTemporary("/documentation/swagger/"))
docs.GET("/swagger/*sub", r.Wrap(swagger.Handle))
docs.GET("/swagger").Handle(wrap(ginext.RedirectTemporary("/documentation/swagger/")))
docs.GET("/swagger/*sub").Handle(wrap(swagger.Handle))
}
// ================ Website ================
frontend := e.Group("")
frontend := e.Routes().Group("")
{
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))
frontend.GET("/").Handle(wrap(r.websiteHandler.Index))
frontend.GET("/index.php").Handle(wrap(r.websiteHandler.Index))
frontend.GET("/index.html").Handle(wrap(r.websiteHandler.Index))
frontend.GET("/index").Handle(wrap(r.websiteHandler.Index))
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))
frontend.GET("/api").Handle(wrap(r.websiteHandler.APIDocs))
frontend.GET("/api.php").Handle(wrap(r.websiteHandler.APIDocs))
frontend.GET("/api.html").Handle(wrap(r.websiteHandler.APIDocs))
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))
frontend.GET("/api_more").Handle(wrap(r.websiteHandler.APIDocsMore))
frontend.GET("/api_more.php").Handle(wrap(r.websiteHandler.APIDocsMore))
frontend.GET("/api_more.html").Handle(wrap(r.websiteHandler.APIDocsMore))
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))
frontend.GET("/message_sent").Handle(wrap(r.websiteHandler.MessageSent))
frontend.GET("/message_sent.php").Handle(wrap(r.websiteHandler.MessageSent))
frontend.GET("/message_sent.html").Handle(wrap(r.websiteHandler.MessageSent))
frontend.GET("/favicon.ico", r.Wrap(r.websiteHandler.FaviconIco))
frontend.GET("/favicon.png", r.Wrap(r.websiteHandler.FaviconPNG))
frontend.GET("/favicon.ico").Handle(wrap(r.websiteHandler.FaviconIco))
frontend.GET("/favicon.png").Handle(wrap(r.websiteHandler.FaviconPNG))
frontend.GET("/js/:fn", r.Wrap(r.websiteHandler.Javascript))
frontend.GET("/css/:fn", r.Wrap(r.websiteHandler.CSS))
frontend.GET("/js/:fn").Handle(wrap(r.websiteHandler.Javascript))
frontend.GET("/css/:fn").Handle(wrap(r.websiteHandler.CSS))
}
// ================ Compat (v1) ================
compat := e.Group("/api")
compat := e.Routes().Group("/api")
{
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))
compat.GET("/register.php").Handle(wrap(r.compatHandler.Register))
compat.GET("/info.php").Handle(wrap(r.compatHandler.Info))
compat.GET("/ack.php").Handle(wrap(r.compatHandler.Ack))
compat.GET("/requery.php").Handle(wrap(r.compatHandler.Requery))
compat.GET("/update.php").Handle(wrap(r.compatHandler.Update))
compat.GET("/expand.php").Handle(wrap(r.compatHandler.Expand))
compat.GET("/upgrade.php").Handle(wrap(r.compatHandler.Upgrade))
}
// ================ Manage API (v2) ================
apiv2 := e.Group("/api/v2/")
apiv2 := e.Routes().Group("/api/v2/")
{
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))
apiv2.POST("/users").Handle(wrap(r.apiHandler.CreateUser))
apiv2.GET("/users/:uid").Handle(wrap(r.apiHandler.GetUser))
apiv2.PATCH("/users/:uid").Handle(wrap(r.apiHandler.UpdateUser))
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/current", r.Wrap(r.apiHandler.GetCurrentUserKey))
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))
apiv2.GET("/users/:uid/keys").Handle(wrap(r.apiHandler.ListUserKeys))
apiv2.POST("/users/:uid/keys").Handle(wrap(r.apiHandler.CreateUserKey))
apiv2.GET("/users/:uid/keys/current").Handle(wrap(r.apiHandler.GetCurrentUserKey))
apiv2.GET("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.GetUserKey))
apiv2.PATCH("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.UpdateUserKey))
apiv2.DELETE("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.DeleteUserKey))
apiv2.GET("/users/:uid/clients", r.Wrap(r.apiHandler.ListClients))
apiv2.GET("/users/:uid/clients/:cid", r.Wrap(r.apiHandler.GetClient))
apiv2.PATCH("/users/:uid/clients/:cid", r.Wrap(r.apiHandler.UpdateClient))
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/clients").Handle(wrap(r.apiHandler.ListClients))
apiv2.GET("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.GetClient))
apiv2.PATCH("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.UpdateClient))
apiv2.POST("/users/:uid/clients").Handle(wrap(r.apiHandler.AddClient))
apiv2.DELETE("/users/:uid/clients/:cid").Handle(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/channels").Handle(wrap(r.apiHandler.ListChannels))
apiv2.POST("/users/:uid/channels").Handle(wrap(r.apiHandler.CreateChannel))
apiv2.GET("/users/:uid/channels/:cid").Handle(wrap(r.apiHandler.GetChannel))
apiv2.PATCH("/users/:uid/channels/:cid").Handle(wrap(r.apiHandler.UpdateChannel))
apiv2.GET("/users/:uid/channels/:cid/messages").Handle(wrap(r.apiHandler.ListChannelMessages))
apiv2.GET("/users/:uid/channels/:cid/subscriptions").Handle(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("/users/:uid/subscriptions").Handle(wrap(r.apiHandler.ListUserSubscriptions))
apiv2.POST("/users/:uid/subscriptions").Handle(wrap(r.apiHandler.CreateSubscription))
apiv2.GET("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.GetSubscription))
apiv2.DELETE("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.CancelSubscription))
apiv2.PATCH("/users/:uid/subscriptions/:sid").Handle(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))
apiv2.GET("/messages").Handle(wrap(r.apiHandler.ListMessages))
apiv2.GET("/messages/:mid").Handle(wrap(r.apiHandler.GetMessage))
apiv2.DELETE("/messages/:mid").Handle(wrap(r.apiHandler.DeleteMessage))
apiv2.GET("/preview/users/:uid", r.Wrap(r.apiHandler.GetUserPreview))
apiv2.GET("/preview/keys/:kid", r.Wrap(r.apiHandler.GetUserKeyPreview))
apiv2.GET("/preview/channels/:cid", r.Wrap(r.apiHandler.GetChannelPreview))
apiv2.GET("/preview/users/:uid").Handle(wrap(r.apiHandler.GetUserPreview))
apiv2.GET("/preview/keys/:kid").Handle(wrap(r.apiHandler.GetUserKeyPreview))
apiv2.GET("/preview/channels/:cid").Handle(wrap(r.apiHandler.GetChannelPreview))
}
// ================ Send API (unversioned) ================
sendAPI := e.Group("")
sendAPI := e.Routes().Group("")
{
sendAPI.POST("/", r.Wrap(r.messageHandler.SendMessage))
sendAPI.POST("/send", r.Wrap(r.messageHandler.SendMessage))
sendAPI.POST("/send.php", r.Wrap(r.compatHandler.SendMessage))
sendAPI.POST("/").Handle(wrap(r.messageHandler.SendMessage)
sendAPI.POST("/send").Handle(wrap(r.messageHandler.SendMessage)
sendAPI.POST("/send.php").Handle(wrap(r.compatHandler.SendMessage)
sendAPI.POST("/external/v1/uptime-kuma", r.Wrap(r.externalHandler.UptimeKuma))
sendAPI.POST("/external/v1/uptime-kuma").Handle(wrap(r.externalHandler.UptimeKuma)
}
// ================
if r.app.Config.ReturnRawErrors {
e.NoRoute(r.Wrap(r.commonHandler.NoRoute))
e.NoRoute(r.commonHandler.NoRoute)
}
// ================
return nil
}
func (r *Router) Wrap(fn ginresp.WHandlerFunc) gin.HandlerFunc {
return ginresp.Wrap(r.app, fn)
}