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,21 +0,0 @@
package ginext
import (
"github.com/gin-gonic/gin"
"net/http"
)
func CorsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, PATCH, DELETE")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusOK)
} else {
c.Next()
}
}
}

View File

@@ -1,31 +0,0 @@
package ginext
import (
scn "blackforestbytes.com/simplecloudnotifier"
"github.com/gin-gonic/gin"
)
var SuppressGinLogs = false
func NewEngine(cfg scn.Config) *gin.Engine {
engine := gin.New()
engine.RedirectFixedPath = false
engine.RedirectTrailingSlash = false
if cfg.Cors {
engine.Use(CorsMiddleware())
}
if cfg.GinDebug {
ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
if SuppressGinLogs {
return
}
ginlogger(context)
})
}
return engine
}

View File

@@ -1,24 +0,0 @@
package ginext
import (
"github.com/gin-gonic/gin"
"net/http"
)
func RedirectFound(newuri string) gin.HandlerFunc {
return func(g *gin.Context) {
g.Redirect(http.StatusFound, newuri)
}
}
func RedirectTemporary(newuri string) gin.HandlerFunc {
return func(g *gin.Context) {
g.Redirect(http.StatusTemporaryRedirect, newuri)
}
}
func RedirectPermanent(newuri string) gin.HandlerFunc {
return func(g *gin.Context) {
g.Redirect(http.StatusPermanentRedirect, newuri)
}
}

View File

@@ -7,114 +7,43 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug"
"strings"
)
type HTTPResponse interface {
Write(g *gin.Context)
Statuscode() int
BodyString() *string
ContentType() string
type cookieval struct {
name string
value string
maxAge int
path string
domain string
secure bool
httpOnly bool
}
type jsonHTTPResponse struct {
statusCode int
data any
}
func (j jsonHTTPResponse) Write(g *gin.Context) {
g.Render(j.statusCode, json.GoJsonRender{Data: j.data, NilSafeSlices: true, NilSafeMaps: true})
}
func (j jsonHTTPResponse) Statuscode() int {
return j.statusCode
}
func (j jsonHTTPResponse) BodyString() *string {
v, err := json.Marshal(j.data)
if err != nil {
return nil
}
return langext.Ptr(string(v))
}
func (j jsonHTTPResponse) ContentType() string {
return "application/json"
}
type emptyHTTPResponse struct {
statusCode int
}
func (j emptyHTTPResponse) Write(g *gin.Context) {
g.Status(j.statusCode)
}
func (j emptyHTTPResponse) Statuscode() int {
return j.statusCode
}
func (j emptyHTTPResponse) BodyString() *string {
return nil
}
func (j emptyHTTPResponse) ContentType() string {
return ""
}
type textHTTPResponse struct {
statusCode int
data string
}
func (j textHTTPResponse) Write(g *gin.Context) {
g.String(j.statusCode, "%s", j.data)
}
func (j textHTTPResponse) Statuscode() int {
return j.statusCode
}
func (j textHTTPResponse) BodyString() *string {
return langext.Ptr(j.data)
}
func (j textHTTPResponse) ContentType() string {
return "text/plain"
}
type dataHTTPResponse struct {
statusCode int
data []byte
contentType string
}
func (j dataHTTPResponse) Write(g *gin.Context) {
g.Data(j.statusCode, j.contentType, j.data)
}
func (j dataHTTPResponse) Statuscode() int {
return j.statusCode
}
func (j dataHTTPResponse) BodyString() *string {
return langext.Ptr(string(j.data))
}
func (j dataHTTPResponse) ContentType() string {
return j.contentType
type headerval struct {
Key string
Val string
}
type errorHTTPResponse struct {
statusCode int
data any
error error
headers []headerval
cookies []cookieval
}
func (j errorHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.JSON(j.statusCode, j.data)
}
@@ -122,7 +51,7 @@ func (j errorHTTPResponse) Statuscode() int {
return j.statusCode
}
func (j errorHTTPResponse) BodyString() *string {
func (j errorHTTPResponse) BodyString(g *gin.Context) *string {
v, err := json.Marshal(j.data)
if err != nil {
return nil
@@ -134,39 +63,41 @@ func (j errorHTTPResponse) ContentType() string {
return "application/json"
}
func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc}
func (j errorHTTPResponse) WithHeader(k string, v string) ginext.HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
func JSON(sc int, data any) HTTPResponse {
return &jsonHTTPResponse{statusCode: sc, data: data}
func (j errorHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) ginext.HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func Data(sc int, contentType string, data []byte) HTTPResponse {
return &dataHTTPResponse{statusCode: sc, contentType: contentType, data: data}
func (j errorHTTPResponse) IsSuccess() bool {
return false
}
func Text(sc int, data string) HTTPResponse {
return &textHTTPResponse{statusCode: sc, data: data}
func (j errorHTTPResponse) Headers() []string {
return langext.ArrMap(j.headers, func(v headerval) string { return v.Key + "=" + v.Val })
}
func InternalError(e error) HTTPResponse {
func InternalError(e error) ginext.HTTPResponse {
return createApiError(nil, "InternalError", 500, apierr.INTERNAL_EXCEPTION, 0, e.Error(), e)
}
func APIError(g *gin.Context, status int, errorid apierr.APIError, msg string, e error) HTTPResponse {
func APIError(g *gin.Context, status int, errorid apierr.APIError, msg string, e error) ginext.HTTPResponse {
return createApiError(g, "APIError", status, errorid, 0, msg, e)
}
func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) ginext.HTTPResponse {
return createApiError(g, "SendAPIError", status, errorid, highlight, msg, e)
}
func NotImplemented(g *gin.Context) HTTPResponse {
func NotImplemented(pctx ginext.PreContext) ginext.HTTPResponse {
return createApiError(g, "NotImplemented", 500, apierr.NOT_IMPLEMENTED, 0, "Not Implemented", nil)
}
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) ginext.HTTPResponse {
reqUri := ""
if g != nil && g.Request != nil {
reqUri = g.Request.Method + " :: " + g.Request.RequestURI
@@ -207,6 +138,6 @@ func createApiError(g *gin.Context, ident string, status int, errorid apierr.API
}
}
func CompatAPIError(errid int, msg string) HTTPResponse {
return &jsonHTTPResponse{statusCode: 200, data: compatAPIError{Success: false, ErrorID: errid, Message: msg}}
func CompatAPIError(errid int, msg string) ginext.HTTPResponse {
return ginext.JSON(200, compatAPIError{Success: false, ErrorID: errid, Message: msg})
}

View File

@@ -8,7 +8,7 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
"net/http"
@@ -37,7 +37,7 @@ import (
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/channels [GET]
func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -50,6 +50,12 @@ func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse {
var u uri
var q query
ctx, g, errResp := pctx.URI(&u).Query(&q).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
ctx, errResp := h.app.StartRequest(g, &u, &q, nil, nil)
if errResp != nil {
return *errResp
@@ -110,7 +116,7 @@ func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse {
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Channels: res}))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
}
// GetChannel swaggerdoc
@@ -129,14 +135,14 @@ func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/channels/{cid} [GET]
func (h APIHandler) GetChannel(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
ChannelID models.ChannelID `uri:"cid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -154,7 +160,7 @@ func (h APIHandler) GetChannel(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, channel.JSON(true)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
}
// CreateChannel swaggerdoc
@@ -173,7 +179,7 @@ func (h APIHandler) GetChannel(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/channels [POST]
func (h APIHandler) CreateChannel(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -186,7 +192,7 @@ func (h APIHandler) CreateChannel(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -247,11 +253,11 @@ func (h APIHandler) CreateChannel(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true)))
} else {
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
}
@@ -277,7 +283,7 @@ func (h APIHandler) CreateChannel(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/channels/{cid} [PATCH]
func (h APIHandler) UpdateChannel(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) UpdateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
ChannelID models.ChannelID `uri:"cid" binding:"entityid"`
@@ -290,7 +296,7 @@ func (h APIHandler) UpdateChannel(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -367,7 +373,7 @@ func (h APIHandler) UpdateChannel(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, channel.JSON(true)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
}
// ListChannelMessages swaggerdoc
@@ -391,7 +397,7 @@ func (h APIHandler) UpdateChannel(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/channels/{cid}/messages [GET]
func (h APIHandler) ListChannelMessages(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
ChannelUserID models.UserID `uri:"uid" binding:"entityid"`
ChannelID models.ChannelID `uri:"cid" binding:"entityid"`
@@ -410,7 +416,7 @@ func (h APIHandler) ListChannelMessages(g *gin.Context) ginresp.HTTPResponse {
var u uri
var q query
ctx, errResp := h.app.StartRequest(g, &u, &q, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Start())
if errResp != nil {
return *errResp
}
@@ -455,5 +461,5 @@ func (h APIHandler) ListChannelMessages(g *gin.Context) ginresp.HTTPResponse {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
}

View File

@@ -6,7 +6,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
)
@@ -25,7 +25,7 @@ import (
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/clients [GET]
func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -34,7 +34,7 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -51,7 +51,7 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Clients: res}))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
}
// GetClient swaggerdoc
@@ -70,14 +70,14 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/clients/{cid} [GET]
func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
ClientID models.ClientID `uri:"cid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -95,7 +95,7 @@ func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
}
// AddClient swaggerdoc
@@ -114,7 +114,7 @@ func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/clients [POST]
func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -128,7 +128,7 @@ func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -153,7 +153,7 @@ func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
}
// DeleteClient swaggerdoc
@@ -172,14 +172,14 @@ func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/clients/{cid} [DELETE]
func (h APIHandler) DeleteClient(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
ClientID models.ClientID `uri:"cid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -202,7 +202,7 @@ func (h APIHandler) DeleteClient(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
}
// UpdateClient swaggerdoc
@@ -225,7 +225,7 @@ func (h APIHandler) DeleteClient(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/clients/{cid} [PATCH]
func (h APIHandler) UpdateClient(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
ClientID models.ClientID `uri:"cid" binding:"entityid"`
@@ -239,7 +239,7 @@ func (h APIHandler) UpdateClient(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -303,5 +303,5 @@ func (h APIHandler) UpdateClient(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
}

View File

@@ -6,7 +6,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
)
@@ -27,7 +27,7 @@ import (
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/keys [GET]
func (h APIHandler) ListUserKeys(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -36,7 +36,7 @@ func (h APIHandler) ListUserKeys(g *gin.Context) ginresp.HTTPResponse {
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -53,7 +53,7 @@ func (h APIHandler) ListUserKeys(g *gin.Context) ginresp.HTTPResponse {
res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() })
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Keys: res}))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Keys: res}))
}
// GetCurrentUserKey swaggerdoc
@@ -73,13 +73,13 @@ func (h APIHandler) ListUserKeys(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/keys/current [GET]
func (h APIHandler) GetCurrentUserKey(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -102,7 +102,7 @@ func (h APIHandler) GetCurrentUserKey(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
}
// GetUserKey swaggerdoc
@@ -122,14 +122,14 @@ func (h APIHandler) GetCurrentUserKey(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/keys/{kid} [GET]
func (h APIHandler) GetUserKey(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
KeyID models.KeyTokenID `uri:"kid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -147,7 +147,7 @@ func (h APIHandler) GetUserKey(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, keytoken.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
}
// UpdateUserKey swaggerdoc
@@ -168,7 +168,7 @@ func (h APIHandler) GetUserKey(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/keys/{kid} [PATCH]
func (h APIHandler) UpdateUserKey(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
KeyID models.KeyTokenID `uri:"kid" binding:"entityid"`
@@ -182,7 +182,7 @@ func (h APIHandler) UpdateUserKey(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -245,7 +245,7 @@ func (h APIHandler) UpdateUserKey(g *gin.Context) ginresp.HTTPResponse {
keytoken.Channels = *b.Channels
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, keytoken.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
}
// CreateUserKey swaggerdoc
@@ -265,7 +265,7 @@ func (h APIHandler) UpdateUserKey(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/keys [POST]
func (h APIHandler) CreateUserKey(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -278,7 +278,7 @@ func (h APIHandler) CreateUserKey(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -314,7 +314,7 @@ func (h APIHandler) CreateUserKey(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create keytoken in db", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, keytok.JSON().WithToken(token)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytok.JSON().WithToken(token)))
}
// DeleteUserKey swaggerdoc
@@ -334,14 +334,14 @@ func (h APIHandler) CreateUserKey(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/keys/{kid} [DELETE]
func (h APIHandler) DeleteUserKey(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) DeleteUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
KeyID models.KeyTokenID `uri:"kid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -368,5 +368,5 @@ func (h APIHandler) DeleteUserKey(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
}

View File

@@ -3,6 +3,7 @@ package handler
import (
"database/sql"
"errors"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"net/http"
"strings"
"time"
@@ -11,7 +12,6 @@ import (
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
"blackforestbytes.com/simplecloudnotifier/models"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
)
@@ -34,7 +34,7 @@ import (
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/messages [GET]
func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
PageSize *int `json:"page_size" form:"page_size"`
NextPageToken *string `json:"next_page_token" form:"next_page_token"`
@@ -155,7 +155,7 @@ func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
}
// GetMessage swaggerdoc
@@ -176,13 +176,13 @@ func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/messages/{mid} [GET]
func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
MessageID models.MessageID `uri:"mid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -204,7 +204,7 @@ func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
// or we subscribe (+confirmed) to the channel and have read/admin key
if ctx.CheckPermissionMessageRead(msg) {
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, msg.FullJSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
}
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil {
@@ -222,7 +222,7 @@ func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
}
// => perm okay
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, msg.FullJSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
}
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
@@ -244,13 +244,13 @@ func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/messages/{mid} [DELETE]
func (h APIHandler) DeleteMessage(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) DeleteMessage(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
MessageID models.MessageID `uri:"mid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -282,5 +282,5 @@ func (h APIHandler) DeleteMessage(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, msg.FullJSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
}

View File

@@ -6,7 +6,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"net/http"
)
@@ -25,13 +25,13 @@ import (
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/preview/users/{uid} [GET]
func (h APIHandler) GetUserPreview(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -49,7 +49,7 @@ func (h APIHandler) GetUserPreview(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSONPreview()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview()))
}
// GetChannelPreview swaggerdoc
@@ -67,13 +67,13 @@ func (h APIHandler) GetUserPreview(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/preview/channels/{cid} [GET]
func (h APIHandler) GetChannelPreview(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
ChannelID models.ChannelID `uri:"cid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -91,7 +91,7 @@ func (h APIHandler) GetChannelPreview(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, channel.JSONPreview()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview()))
}
// GetUserKeyPreview swaggerdoc
@@ -109,13 +109,13 @@ func (h APIHandler) GetChannelPreview(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/preview/keys/{kid} [GET]
func (h APIHandler) GetUserKeyPreview(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
KeyID models.KeyTokenID `uri:"kid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -133,5 +133,5 @@ func (h APIHandler) GetUserKeyPreview(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, keytoken.JSONPreview()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview()))
}

View File

@@ -6,7 +6,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
"strings"
@@ -47,7 +47,7 @@ import (
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/subscriptions [GET]
func (h APIHandler) ListUserSubscriptions(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -64,7 +64,7 @@ func (h APIHandler) ListUserSubscriptions(g *gin.Context) ginresp.HTTPResponse {
var u uri
var q query
ctx, errResp := h.app.StartRequest(g, &u, &q, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Start())
if errResp != nil {
return *errResp
}
@@ -128,7 +128,7 @@ func (h APIHandler) ListUserSubscriptions(g *gin.Context) ginresp.HTTPResponse {
jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Subscriptions: jsonres}))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres}))
}
// ListChannelSubscriptions swaggerdoc
@@ -147,7 +147,7 @@ func (h APIHandler) ListUserSubscriptions(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/channels/{cid}/subscriptions [GET]
func (h APIHandler) ListChannelSubscriptions(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
ChannelID models.ChannelID `uri:"cid" binding:"entityid"`
@@ -157,7 +157,7 @@ func (h APIHandler) ListChannelSubscriptions(g *gin.Context) ginresp.HTTPRespons
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -182,7 +182,7 @@ func (h APIHandler) ListChannelSubscriptions(g *gin.Context) ginresp.HTTPRespons
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Subscriptions: res}))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
}
// GetSubscription swaggerdoc
@@ -201,14 +201,14 @@ func (h APIHandler) ListChannelSubscriptions(g *gin.Context) ginresp.HTTPRespons
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/subscriptions/{sid} [GET]
func (h APIHandler) GetSubscription(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
SubscriptionID models.SubscriptionID `uri:"sid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -229,7 +229,7 @@ func (h APIHandler) GetSubscription(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
}
// CancelSubscription swaggerdoc
@@ -248,14 +248,14 @@ func (h APIHandler) GetSubscription(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/subscriptions/{sid} [DELETE]
func (h APIHandler) CancelSubscription(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
SubscriptionID models.SubscriptionID `uri:"sid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -281,7 +281,7 @@ func (h APIHandler) CancelSubscription(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
}
// CreateSubscription swaggerdoc
@@ -301,7 +301,7 @@ func (h APIHandler) CancelSubscription(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/subscriptions [POST]
func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -317,7 +317,7 @@ func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
var u uri
var q query
var b body
ctx, errResp := h.app.StartRequest(g, &u, &q, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -378,7 +378,7 @@ func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
existingSub.Confirmed = true
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, existingSub.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON()))
}
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID)
@@ -386,7 +386,7 @@ func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, sub.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, sub.JSON()))
}
// UpdateSubscription swaggerdoc
@@ -406,7 +406,7 @@ func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid}/subscriptions/{sid} [PATCH]
func (h APIHandler) UpdateSubscription(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) UpdateSubscription(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
SubscriptionID models.SubscriptionID `uri:"sid" binding:"entityid"`
@@ -417,7 +417,7 @@ func (h APIHandler) UpdateSubscription(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -455,5 +455,5 @@ func (h APIHandler) UpdateSubscription(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
}

View File

@@ -7,8 +7,8 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
)
@@ -26,7 +26,7 @@ import (
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users [POST]
func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
type body struct {
FCMToken string `json:"fcm_token"`
ProToken *string `json:"pro_token"`
@@ -39,7 +39,7 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
}
var b body
ctx, errResp := h.app.StartRequest(g, nil, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -117,7 +117,7 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
log.Info().Msg(fmt.Sprintf("Sucessfully created new user %s (client: %v)", userobj.UserID, b.NoClient))
if b.NoClient {
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, userobj.JSONWithClients(make([]models.Client, 0), adminKey, sendKey, readKey)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients(make([]models.Client, 0), adminKey, sendKey, readKey)))
} else {
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
if err != nil {
@@ -129,7 +129,7 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey)))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey)))
}
}
@@ -149,13 +149,13 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid} [GET]
func (h APIHandler) GetUser(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
if errResp != nil {
return *errResp
}
@@ -173,7 +173,7 @@ func (h APIHandler) GetUser(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
}
// UpdateUser swaggerdoc
@@ -195,7 +195,7 @@ func (h APIHandler) GetUser(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/users/{uid} [PATCH]
func (h APIHandler) UpdateUser(g *gin.Context) ginresp.HTTPResponse {
func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
@@ -206,7 +206,7 @@ func (h APIHandler) UpdateUser(g *gin.Context) ginresp.HTTPResponse {
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b, nil)
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -261,5 +261,5 @@ func (h APIHandler) UpdateUser(g *gin.Context) ginresp.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
}

View File

@@ -6,10 +6,10 @@ import (
"blackforestbytes.com/simplecloudnotifier/db/simplectx"
"blackforestbytes.com/simplecloudnotifier/logic"
"bytes"
"context"
"errors"
"github.com/gin-gonic/gin"
sqlite3 "github.com/mattn/go-sqlite3"
"github.com/mattn/go-sqlite3"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"net/http"
@@ -51,12 +51,18 @@ type pingResponseInfo struct {
// @Router /api/ping [put]
// @Router /api/ping [delete]
// @Router /api/ping [patch]
func (h CommonHandler) Ping(g *gin.Context) ginresp.HTTPResponse {
func (h CommonHandler) Ping(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(g.Request.Body)
resuestBody := buf.String()
return ginresp.JSON(http.StatusOK, pingResponse{
return ginext.JSON(http.StatusOK, pingResponse{
Message: "Pong",
Info: pingResponseInfo{
Method: g.Request.Method,
@@ -78,7 +84,7 @@ func (h CommonHandler) Ping(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError
//
// @Router /api/db-test [post]
func (h CommonHandler) DatabaseTest(g *gin.Context) ginresp.HTTPResponse {
func (h CommonHandler) DatabaseTest(pctx ginext.PreContext) ginext.HTTPResponse {
type response struct {
Success bool `json:"success"`
LibVersion string `json:"libVersion"`
@@ -86,8 +92,11 @@ func (h CommonHandler) DatabaseTest(g *gin.Context) ginresp.HTTPResponse {
SourceID string `json:"sourceID"`
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
ctx, _, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
libVersion, libVersionNumber, sourceID := sqlite3.Version()
@@ -96,7 +105,7 @@ func (h CommonHandler) DatabaseTest(g *gin.Context) ginresp.HTTPResponse {
return ginresp.InternalError(err)
}
return ginresp.JSON(http.StatusOK, response{
return ginext.JSON(http.StatusOK, response{
Success: true,
LibVersion: libVersion,
LibVersionNumber: libVersionNumber,
@@ -114,13 +123,16 @@ func (h CommonHandler) DatabaseTest(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError
//
// @Router /api/health [get]
func (h CommonHandler) Health(g *gin.Context) ginresp.HTTPResponse {
func (h CommonHandler) Health(pctx ginext.PreContext) ginext.HTTPResponse {
type response struct {
Status string `json:"status"`
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
ctx, _, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
_, libVersionNumber, _ := sqlite3.Version()
@@ -161,7 +173,7 @@ func (h CommonHandler) Health(g *gin.Context) ginresp.HTTPResponse {
}
return ginresp.JSON(http.StatusOK, response{Status: "ok"})
return ginext.JSON(http.StatusOK, response{Status: "ok"})
}
// Sleep swaggerdoc
@@ -177,7 +189,7 @@ func (h CommonHandler) Health(g *gin.Context) ginresp.HTTPResponse {
// @Failure 500 {object} ginresp.apiError
//
// @Router /api/sleep/{secs} [post]
func (h CommonHandler) Sleep(g *gin.Context) ginresp.HTTPResponse {
func (h CommonHandler) Sleep(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
Seconds float64 `uri:"secs"`
}
@@ -187,6 +199,12 @@ func (h CommonHandler) Sleep(g *gin.Context) ginresp.HTTPResponse {
Duration float64 `json:"duration"`
}
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
t0 := time.Now().Format(time.RFC3339Nano)
var u uri
@@ -198,15 +216,21 @@ func (h CommonHandler) Sleep(g *gin.Context) ginresp.HTTPResponse {
t1 := time.Now().Format(time.RFC3339Nano)
return ginresp.JSON(http.StatusOK, response{
return ginext.JSON(http.StatusOK, response{
Start: t0,
End: t1,
Duration: u.Seconds,
})
}
func (h CommonHandler) NoRoute(g *gin.Context) ginresp.HTTPResponse {
return ginresp.JSON(http.StatusNotFound, gin.H{
func (h CommonHandler) NoRoute(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return ginext.JSON(http.StatusNotFound, gin.H{
"": "================ ROUTE NOT FOUND ================",
"FullPath": g.FullPath(),
"Method": g.Request.Method,

View File

@@ -11,8 +11,8 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
)
@@ -47,7 +47,7 @@ func NewCompatHandler(app *logic.Application) CompatHandler {
// @Failure 500 {object} ginresp.apiError
//
// @Router /send.php [POST]
func (h CompatHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse {
type combined struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
@@ -92,7 +92,7 @@ func (h CompatHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
if errResp != nil {
return *errResp
} else {
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
ErrorID: apierr.NO_ERROR,
ErrorHighlight: -1,
@@ -127,7 +127,7 @@ func (h CompatHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
// @Failure default {object} ginresp.compatAPIError
//
// @Router /api/register.php [get]
func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) Register(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
FCMToken *string `json:"fcm_token" form:"fcm_token"`
Pro *string `json:"pro" form:"pro"`
@@ -216,7 +216,7 @@ func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create userid<old>", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "New user registered",
UserID: oldid,
@@ -245,7 +245,7 @@ func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
// @Failure default {object} ginresp.compatAPIError
//
// @Router /api/info.php [get]
func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) Info(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
@@ -321,7 +321,7 @@ func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query user")
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
UserID: *data.UserID,
@@ -354,7 +354,7 @@ func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
// @Failure default {object} ginresp.compatAPIError
//
// @Router /api/ack.php [get]
func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) Ack(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
@@ -434,7 +434,7 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
}
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
PrevAckValue: langext.Conditional(ackBefore, 1, 0),
@@ -460,7 +460,7 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
// @Failure default {object} ginresp.compatAPIError
//
// @Router /api/requery.php [get]
func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) Requery(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
@@ -545,7 +545,7 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
})
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
Count: len(compMsgs),
@@ -573,7 +573,7 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
// @Failure default {object} ginresp.compatAPIError
//
// @Router /api/update.php [get]
func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) Update(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
@@ -673,7 +673,7 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query user")
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "user updated",
UserID: *data.UserID,
@@ -704,7 +704,7 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
// @Failure default {object} ginresp.compatAPIError
//
// @Router /api/expand.php [get]
func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) Expand(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
@@ -779,7 +779,7 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query message")
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
Data: models.CompatMessage{
@@ -816,7 +816,7 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
// @Failure default {object} ginresp.compatAPIError
//
// @Router /api/upgrade.php [get]
func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
func (h CompatHandler) Upgrade(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
@@ -921,7 +921,7 @@ func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query user")
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "user updated",
UserID: *data.UserID,

View File

@@ -7,7 +7,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"fmt"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
"time"
@@ -41,7 +41,7 @@ func NewExternalHandler(app *logic.Application) ExternalHandler {
// @Failure 500 {object} ginresp.apiError "An internal server error occurred - try again later"
//
// @Router /external/v1/uptime-kuma [POST]
func (h ExternalHandler) UptimeKuma(g *gin.Context) ginresp.HTTPResponse {
func (h ExternalHandler) UptimeKuma(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
UserID *models.UserID `form:"user_id" example:"7725"`
KeyToken *string `form:"key" example:"P3TNH8mvv14fm"`
@@ -128,7 +128,7 @@ func (h ExternalHandler) UptimeKuma(g *gin.Context) ginresp.HTTPResponse {
return *errResp
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
MessageID: okResp.Message.MessageID,
}))
}

View File

@@ -2,12 +2,11 @@ package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
primarydb "blackforestbytes.com/simplecloudnotifier/db/impl/primary"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
)
@@ -49,7 +48,7 @@ func NewMessageHandler(app *logic.Application) MessageHandler {
//
// @Router / [POST]
// @Router /send [POST]
func (h MessageHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
func (h MessageHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse {
type combined struct {
UserID *models.UserID `json:"user_id" form:"user_id" example:"7725" `
KeyToken *string `json:"key" form:"key" example:"P3TNH8mvv14fm" `
@@ -78,7 +77,7 @@ func (h MessageHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
var b combined
var q combined
var f combined
ctx, errResp := h.app.StartRequest(g, nil, &q, &b, &f, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := h.app.StartRequest(pctx.Form(&f).Query(&q).Body(&b).Start())
if errResp != nil {
return *errResp
}
@@ -91,7 +90,7 @@ func (h MessageHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
if errResp != nil {
return *errResp
} else {
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
ErrorID: apierr.NO_ERROR,
ErrorHighlight: -1,

View File

@@ -7,6 +7,7 @@ import (
"errors"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/rext"
"net/http"
"regexp"
@@ -27,60 +28,104 @@ func NewWebsiteHandler(app *logic.Application) WebsiteHandler {
}
}
func (h WebsiteHandler) Index(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) Index(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.serveAsset(g, "index.html", true)
}
func (h WebsiteHandler) APIDocs(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) APIDocs(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.serveAsset(g, "api.html", true)
}
func (h WebsiteHandler) APIDocsMore(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) APIDocsMore(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.serveAsset(g, "api_more.html", true)
}
func (h WebsiteHandler) MessageSent(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) MessageSent(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.serveAsset(g, "message_sent.html", true)
}
func (h WebsiteHandler) FaviconIco(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) FaviconIco(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.serveAsset(g, "favicon.ico", false)
}
func (h WebsiteHandler) FaviconPNG(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) FaviconPNG(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.serveAsset(g, "favicon.png", false)
}
func (h WebsiteHandler) Javascript(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
type uri struct {
Filename string `uri:"fn"`
}
var u uri
if err := g.ShouldBindUri(&u); err != nil {
return ginresp.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return ginext.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
return h.serveAsset(g, "js/"+u.Filename, false)
}
func (h WebsiteHandler) CSS(g *gin.Context) ginresp.HTTPResponse {
func (h WebsiteHandler) CSS(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
Filename string `uri:"fn"`
}
var u uri
if err := g.ShouldBindUri(&u); err != nil {
return ginresp.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.serveAsset(g, "css/"+u.Filename, false)
}
func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginresp.HTTPResponse {
func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginext.HTTPResponse {
_data, err := website.Assets.ReadFile(fn)
if err != nil {
return ginresp.Status(http.StatusNotFound)
return ginext.Status(http.StatusNotFound)
}
data := string(_data)
@@ -141,7 +186,7 @@ func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginresp
mime = "image/svg+xml"
}
return ginresp.Data(http.StatusOK, mime, []byte(data))
return ginext.Data(http.StatusOK, mime, []byte(data))
}
func (h WebsiteHandler) getReplConfig(key string) (string, bool) {

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)
}

View File

@@ -1,33 +1,33 @@
package ginresp
package api
import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/models"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/mattn/go-sqlite3"
"github.com/glebarez/go-sqlite"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"math/rand"
"runtime/debug"
"time"
)
type WHandlerFunc func(*gin.Context) HTTPResponse
type RequestLogAcceptor interface {
InsertRequestLog(data models.RequestLog)
}
func Wrap(rlacc RequestLogAcceptor, fn WHandlerFunc) gin.HandlerFunc {
func Wrap(rlacc RequestLogAcceptor, fn ginext.WHandlerFunc) ginext.WHandlerFunc {
maxRetry := scn.Conf.RequestMaxRetry
retrySleep := scn.Conf.RequestRetrySleep
return func(g *gin.Context) {
return func(pctx *ginext.PreContext) {
reqctx := g.Request.Context()
@@ -43,7 +43,7 @@ func Wrap(rlacc RequestLogAcceptor, fn WHandlerFunc) gin.HandlerFunc {
if panicObj != nil {
log.Error().Interface("panicObj", panicObj).Msg("Panic occured (in gin handler)")
log.Error().Msg(stackTrace)
wrap = APIError(g, 500, apierr.PANIC, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v\n\n@:\n%s", panicObj, stackTrace)))
wrap = ginresp.APIError(g, 500, apierr.PANIC, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v\n\n@:\n%s", panicObj, stackTrace)))
}
if g.Writer.Written() {
@@ -70,9 +70,14 @@ func Wrap(rlacc RequestLogAcceptor, fn WHandlerFunc) gin.HandlerFunc {
rlacc.InsertRequestLog(createRequestLog(g, t0, ctr, wrap, nil))
}
statuscode := wrap.Statuscode()
if statuscode/100 != 2 {
log.Warn().Str("url", g.Request.Method+"::"+g.Request.URL.String()).Msg(fmt.Sprintf("Request failed with statuscode %d", statuscode))
if scw, ok := wrap.(ginext.InspectableHTTPResponse); ok {
statuscode := scw.Statuscode()
if statuscode/100 != 2 {
log.Warn().Str("url", g.Request.Method+"::"+g.Request.URL.String()).Msg(fmt.Sprintf("Request failed with statuscode %d", statuscode))
}
} else {
log.Warn().Str("url", g.Request.Method+"::"+g.Request.URL.String()).Msg(fmt.Sprintf("Request failed with statuscode [unknown]"))
}
wrap.Write(g)
@@ -85,7 +90,7 @@ func Wrap(rlacc RequestLogAcceptor, fn WHandlerFunc) gin.HandlerFunc {
}
func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp HTTPResponse, panicstr *string) models.RequestLog {
func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp ginext.HTTPResponse, panicstr *string) models.RequestLog {
t1 := time.Now()
@@ -109,9 +114,11 @@ func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp HTTPResponse,
var strrespbody *string = nil
if resp != nil {
respbody = resp.BodyString()
if respbody != nil && len(*respbody) < scn.Conf.ReqLogMaxBodySize {
strrespbody = respbody
if resp2, ok := resp.(ginext.InspectableHTTPResponse); ok {
respbody = resp2.BodyString(g)
if respbody != nil && len(*respbody) < scn.Conf.ReqLogMaxBodySize {
strrespbody = respbody
}
}
}
@@ -147,7 +154,7 @@ func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp HTTPResponse,
}
}
func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, stackTrace string, panicObj any) {
func callPanicSafe(fn ginext.WHandlerFunc, g ginext.PreContext) (res ginext.HTTPResponse, stackTrace string, panicObj any) {
defer func() {
if rec := recover(); rec != nil {
res = nil
@@ -173,17 +180,14 @@ func resetBody(g *gin.Context) error {
return nil
}
func isSqlite3Busy(r HTTPResponse) bool {
if errwrap, ok := r.(*errorHTTPResponse); ok && errwrap != nil {
if errors.Is(errwrap.error, sqlite3.ErrBusy) {
return true
}
var s3err sqlite3.Error
if errors.As(errwrap.error, &s3err) {
if errors.Is(s3err.Code, sqlite3.ErrBusy) {
return true
func isSqlite3Busy(r ginext.HTTPResponse) bool {
if errwrap, ok := r.(interface{ Unwrap() error }); ok && errwrap != nil {
{
var s3err *sqlite.Error
if errors.As(errwrap.Unwrap(), &s3err) {
if s3err.Code() == 5 { // [5] == SQLITE_BUSY
return true
}
}
}
}