Refactor server to go-sqlite and ginext [WIP]

This commit is contained in:
Mike Schwörer 2024-07-16 17:19:55 +02:00
parent 55d0dea835
commit c204dc5a8b
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
38 changed files with 2547 additions and 1983 deletions

View File

@ -5,6 +5,8 @@ PORT=9090
NAMESPACE=$(shell git rev-parse --abbrev-ref HEAD)
HASH=$(shell git rev-parse HEAD)
TAGS="timetzdata sqlite_fts5 sqlite_foreign_keys"
.PHONY: test swagger pygmentize docker migrate dgi pygmentize lint docker
SWAGGO_VERSION=v1.8.12
@ -13,7 +15,7 @@ SWAGGO=github.com/swaggo/swag/cmd/swag@$(SWAGGO_VERSION)
build: ids enums swagger pygmentize fmt
mkdir -p _build
rm -f ./_build/scn_backend
CGO_ENABLED=1 go build -v -o _build/scn_backend -tags "timetzdata sqlite_fts5 sqlite_foreign_keys" ./cmd/scnserver
CGO_ENABLED=1 go build -v -o _build/scn_backend -tags $(TAGS) ./cmd/scnserver
enums:
go generate models/enums.go
@ -27,7 +29,7 @@ run: build
gow:
which gow || go install github.com/mitranim/gow@latest
gow -e "go,mod,html,css,json,yaml,js" run -tags "timetzdata sqlite_fts5 sqlite_foreign_keys" blackforestbytes.com/simplecloudnotifier/cmd/scnserver
gow -e "go,mod,html,css,json,yaml,js" run -tags $(TAGS) blackforestbytes.com/simplecloudnotifier/cmd/scnserver
dgi:
[ ! -f "DOCKER_GIT_INFO" ] || rm DOCKER_GIT_INFO
@ -99,10 +101,10 @@ fmt: swagger-setup
test:
which gotestsum || go install gotest.tools/gotestsum@latest
gotestsum --format "testname" -- -tags="timetzdata sqlite_fts5 sqlite_foreign_keys" "./test"
gotestsum --format "testname" -- -tags $(TAGS) "./test"
migrate:
CGO_ENABLED=1 go build -v -o _build/scn_migrate -tags "timetzdata sqlite_fts5 sqlite_foreign_keys" ./cmd/migrate
CGO_ENABLED=1 go build -v -o _build/scn_migrate -tags $(TAGS) ./cmd/migrate
./_build/scn_migrate
lint:

View File

@ -12,10 +12,6 @@
- (!) use goext.ginWrapper
- (!) use goext.exerr
- use bfcodegen (enums+id)
#### UNSURE
- (?) default-priority for channels

View File

@ -18,6 +18,7 @@ const (
BINDFAIL_QUERY_PARAM APIError = 1151
BINDFAIL_BODY_PARAM APIError = 1152
BINDFAIL_URI_PARAM APIError = 1153
BINDFAIL_HEADER_PARAM APIError = 1152
INVALID_BODY_PARAM APIError = 1161
INVALID_ENUM_VALUE APIError = 1171

View File

@ -93,10 +93,6 @@ func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight
return createApiError(g, "SendAPIError", status, errorid, highlight, msg, e)
}
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) ginext.HTTPResponse {
reqUri := ""
if g != nil && g.Request != nil {

View File

@ -4,6 +4,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
@ -56,11 +57,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
ctx, errResp := h.app.StartRequest(g, &u, &q, nil, nil)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
@ -116,7 +113,9 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
})
}
// GetChannel swaggerdoc
@ -142,12 +141,14 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
@ -160,7 +161,9 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
})
}
// CreateChannel swaggerdoc
@ -192,12 +195,14 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -253,14 +258,15 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true)))
return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true)))
} else {
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
}
})
}
// UpdateChannel swaggerdoc
@ -296,12 +302,14 @@ func (h APIHandler) UpdateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -373,7 +381,9 @@ func (h APIHandler) UpdateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
})
}
// ListChannelMessages swaggerdoc
@ -416,12 +426,14 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo
var u uri
var q query
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Start())
ctx, g, errResp := pctx.URI(&u).Query(&q).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
trimmed := langext.Coalesce(q.Trimmed, true)
maxPageSize := langext.Conditional(trimmed, 16, 256)
@ -461,5 +473,7 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
})
}

View File

@ -3,6 +3,7 @@ package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
@ -34,12 +35,14 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
@ -51,7 +54,9 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
})
}
// GetClient swaggerdoc
@ -77,12 +82,14 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
@ -95,7 +102,9 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
}
// AddClient swaggerdoc
@ -128,12 +137,14 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if !b.ClientType.Valid() {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
}
@ -153,7 +164,9 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
}
// DeleteClient swaggerdoc
@ -179,12 +192,14 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -202,7 +217,9 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
}
// UpdateClient swaggerdoc
@ -239,12 +256,14 @@ func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -303,5 +322,7 @@ func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
}

View File

@ -3,6 +3,7 @@ package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
@ -36,12 +37,14 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -53,7 +56,9 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() })
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Keys: res}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Keys: res}))
})
}
// GetCurrentUserKey swaggerdoc
@ -79,12 +84,14 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
@ -102,7 +109,9 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
})
}
// GetUserKey swaggerdoc
@ -129,12 +138,14 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -147,7 +158,9 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
})
}
// UpdateUserKey swaggerdoc
@ -182,12 +195,14 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -245,7 +260,9 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
keytoken.Channels = *b.Channels
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
})
}
// CreateUserKey swaggerdoc
@ -278,12 +295,14 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
channels := langext.Coalesce(b.Channels, make([]models.ChannelID, 0))
var allChan bool
@ -314,7 +333,9 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create keytoken in db", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytok.JSON().WithToken(token)))
return finishSuccess(ginext.JSON(http.StatusOK, keytok.JSON().WithToken(token)))
})
}
// DeleteUserKey swaggerdoc
@ -341,12 +362,14 @@ func (h APIHandler) DeleteUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -368,5 +391,7 @@ func (h APIHandler) DeleteUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
}

View File

@ -1,6 +1,7 @@
package handler
import (
"blackforestbytes.com/simplecloudnotifier/logic"
"database/sql"
"errors"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
@ -55,12 +56,14 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
}
var q query
ctx, errResp := h.app.StartRequest(g, nil, &q, nil, nil)
ctx, g, errResp := pctx.Query(&q).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
trimmed := langext.Coalesce(q.Trimmed, true)
maxPageSize := langext.Conditional(trimmed, 16, 256)
@ -155,7 +158,9 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
})
}
// GetMessage swaggerdoc
@ -182,12 +187,14 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
@ -204,7 +211,7 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
// or we subscribe (+confirmed) to the channel and have read/admin key
if ctx.CheckPermissionMessageRead(msg) {
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
}
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil {
@ -222,10 +229,12 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
}
// => perm okay
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
}
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
})
}
// DeleteMessage swaggerdoc
@ -250,12 +259,14 @@ func (h APIHandler) DeleteMessage(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
@ -282,5 +293,7 @@ func (h APIHandler) DeleteMessage(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
})
}

View File

@ -3,6 +3,7 @@ package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
@ -31,12 +32,14 @@ func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
@ -49,7 +52,9 @@ func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview()))
return finishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview()))
})
}
// GetChannelPreview swaggerdoc
@ -73,12 +78,14 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
@ -91,7 +98,9 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview()))
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview()))
})
}
// GetUserKeyPreview swaggerdoc
@ -115,12 +124,14 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
@ -133,5 +144,7 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview()))
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview()))
})
}

View File

@ -3,6 +3,7 @@ package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
@ -64,12 +65,14 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes
var u uri
var q query
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Start())
ctx, g, errResp := pctx.URI(&u).Query(&q).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
@ -128,7 +131,9 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes
jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres}))
})
}
// ListChannelSubscriptions swaggerdoc
@ -157,12 +162,14 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
@ -182,7 +189,9 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
})
}
// GetSubscription swaggerdoc
@ -208,12 +217,14 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
@ -229,7 +240,9 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
})
}
// CancelSubscription swaggerdoc
@ -255,12 +268,14 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -281,7 +296,9 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
})
}
// CreateSubscription swaggerdoc
@ -317,12 +334,14 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
var u uri
var q query
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Query(&q).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -378,7 +397,7 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
existingSub.Confirmed = true
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON()))
}
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID)
@ -386,7 +405,9 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, sub.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, sub.JSON()))
})
}
// UpdateSubscription swaggerdoc
@ -417,12 +438,14 @@ func (h APIHandler) UpdateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -455,5 +478,7 @@ func (h APIHandler) UpdateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
})
}

View File

@ -3,6 +3,7 @@ package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
@ -39,12 +40,14 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
}
var b body
ctx, g, errResp := h.app.StartRequest(pctx.Body(&b).Start())
ctx, g, errResp := pctx.Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
var clientType models.ClientType
if !b.NoClient {
if b.FCMToken == "" {
@ -117,7 +120,7 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
log.Info().Msg(fmt.Sprintf("Sucessfully created new user %s (client: %v)", userobj.UserID, b.NoClient))
if b.NoClient {
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients(make([]models.Client, 0), adminKey, sendKey, readKey)))
return 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,9 +132,9 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey)))
return finishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey)))
}
})
}
// GetUser swaggerdoc
@ -155,12 +158,14 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
}
var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
@ -173,7 +178,10 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
})
}
// UpdateUser swaggerdoc
@ -206,12 +214,14 @@ func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri
var b body
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
@ -261,5 +271,6 @@ func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
})
}

View File

@ -58,6 +58,8 @@ func (h CommonHandler) Ping(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(g.Request.Body)
resuestBody := buf.String()
@ -72,6 +74,8 @@ func (h CommonHandler) Ping(pctx ginext.PreContext) ginext.HTTPResponse {
Address: g.Request.RemoteAddr,
},
})
})
}
// DatabaseTest swaggerdoc
@ -92,12 +96,14 @@ func (h CommonHandler) DatabaseTest(pctx ginext.PreContext) ginext.HTTPResponse
SourceID string `json:"sourceID"`
}
ctx, _, errResp := pctx.Start()
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
libVersion, libVersionNumber, sourceID := sqlite3.Version()
err := h.app.Database.Ping(ctx)
@ -111,6 +117,8 @@ func (h CommonHandler) DatabaseTest(pctx ginext.PreContext) ginext.HTTPResponse
LibVersionNumber: libVersionNumber,
SourceID: sourceID,
})
})
}
// Health swaggerdoc
@ -128,12 +136,14 @@ func (h CommonHandler) Health(pctx ginext.PreContext) ginext.HTTPResponse {
Status string `json:"status"`
}
ctx, _, errResp := pctx.Start()
ctx, g, errResp := pctx.Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
_, libVersionNumber, _ := sqlite3.Version()
if libVersionNumber < 3039000 {
@ -174,6 +184,8 @@ func (h CommonHandler) Health(pctx ginext.PreContext) ginext.HTTPResponse {
}
return ginext.JSON(http.StatusOK, response{Status: "ok"})
})
}
// Sleep swaggerdoc
@ -205,6 +217,8 @@ func (h CommonHandler) Sleep(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
t0 := time.Now().Format(time.RFC3339Nano)
var u uri
@ -221,6 +235,8 @@ func (h CommonHandler) Sleep(pctx ginext.PreContext) ginext.HTTPResponse {
End: t1,
Duration: u.Seconds,
})
})
}
func (h CommonHandler) NoRoute(pctx ginext.PreContext) ginext.HTTPResponse {
@ -230,6 +246,8 @@ func (h CommonHandler) NoRoute(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return ginext.JSON(http.StatusNotFound, gin.H{
"": "================ ROUTE NOT FOUND ================",
"FullPath": g.FullPath(),
@ -240,4 +258,6 @@ func (h CommonHandler) NoRoute(pctx ginext.PreContext) ginext.HTTPResponse {
"Header": g.Request.Header,
"~": "================ ROUTE NOT FOUND ================",
})
})
}

View File

@ -72,12 +72,14 @@ func (h CompatHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse {
var f combined
var q combined
ctx, errResp := h.app.StartRequest(g, nil, &q, nil, &f, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&q).Form(&f).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(f, q)
newid, err := h.database.ConvertCompatID(ctx, langext.Coalesce(data.UserID, -1), "userid")
@ -92,7 +94,7 @@ func (h CompatHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse {
if errResp != nil {
return *errResp
} else {
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
ErrorID: apierr.NO_ERROR,
ErrorHighlight: -1,
@ -105,6 +107,7 @@ func (h CompatHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse {
SCNMessageID: okResp.CompatMessageID,
}))
}
})
}
// Register swaggerdoc
@ -145,12 +148,14 @@ func (h CompatHandler) Register(pctx ginext.PreContext) ginext.HTTPResponse {
var datq query
var datb query
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(datb, datq)
if data.FCMToken == nil {
@ -216,7 +221,7 @@ func (h CompatHandler) Register(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create userid<old>", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "New user registered",
UserID: oldid,
@ -225,6 +230,8 @@ func (h CompatHandler) Register(pctx ginext.PreContext) ginext.HTTPResponse {
QuotaMax: user.QuotaPerDay(),
IsPro: user.IsPro,
}))
})
}
// Info swaggerdoc
@ -264,12 +271,14 @@ func (h CompatHandler) Info(pctx ginext.PreContext) ginext.HTTPResponse {
var datq query
var datb query
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
@ -321,7 +330,7 @@ func (h CompatHandler) Info(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query user")
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
UserID: *data.UserID,
@ -332,6 +341,8 @@ func (h CompatHandler) Info(pctx ginext.PreContext) ginext.HTTPResponse {
FCMSet: len(clients) > 0,
UnackCount: unackCount,
}))
})
}
// Ack swaggerdoc
@ -369,12 +380,14 @@ func (h CompatHandler) Ack(pctx ginext.PreContext) ginext.HTTPResponse {
var datq query
var datb query
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
@ -434,12 +447,14 @@ func (h CompatHandler) Ack(pctx ginext.PreContext) ginext.HTTPResponse {
}
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
PrevAckValue: langext.Conditional(ackBefore, 1, 0),
NewAckValue: 1,
}))
})
}
// Requery swaggerdoc
@ -474,12 +489,14 @@ func (h CompatHandler) Requery(pctx ginext.PreContext) ginext.HTTPResponse {
var datq query
var datb query
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
@ -545,12 +562,14 @@ func (h CompatHandler) Requery(pctx ginext.PreContext) ginext.HTTPResponse {
})
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
Count: len(compMsgs),
Data: compMsgs,
}))
})
}
// Update swaggerdoc
@ -591,12 +610,14 @@ func (h CompatHandler) Update(pctx ginext.PreContext) ginext.HTTPResponse {
var datq query
var datb query
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
@ -673,7 +694,7 @@ func (h CompatHandler) Update(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query user")
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "user updated",
UserID: *data.UserID,
@ -682,6 +703,8 @@ func (h CompatHandler) Update(pctx ginext.PreContext) ginext.HTTPResponse {
QuotaMax: user.QuotaPerDay(),
IsPro: langext.Conditional(user.IsPro, 1, 0),
}))
})
}
// Expand swaggerdoc
@ -718,12 +741,14 @@ func (h CompatHandler) Expand(pctx ginext.PreContext) ginext.HTTPResponse {
var datq query
var datb query
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
@ -779,7 +804,7 @@ func (h CompatHandler) Expand(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query message")
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
Data: models.CompatMessage{
@ -792,6 +817,8 @@ func (h CompatHandler) Expand(pctx ginext.PreContext) ginext.HTTPResponse {
SCNMessageID: *data.MessageID,
},
}))
})
}
// Upgrade swaggerdoc
@ -834,12 +861,14 @@ func (h CompatHandler) Upgrade(pctx ginext.PreContext) ginext.HTTPResponse {
var datq query
var datb query
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb, logic.RequestOptions{IgnoreWrongContentType: true})
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
@ -921,7 +950,7 @@ func (h CompatHandler) Upgrade(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.CompatAPIError(0, "Failed to query user")
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
Message: "user updated",
UserID: *data.UserID,
@ -929,4 +958,6 @@ func (h CompatHandler) Upgrade(pctx ginext.PreContext) ginext.HTTPResponse {
QuotaMax: user.QuotaPerDay(),
IsPro: user.IsPro,
}))
})
}

View File

@ -74,12 +74,14 @@ func (h ExternalHandler) UptimeKuma(pctx ginext.PreContext) ginext.HTTPResponse
var b body
var q query
ctx, httpErr := h.app.StartRequest(g, nil, &q, &b, nil)
if httpErr != nil {
return *httpErr
ctx, g, errResp := pctx.Query(&q).Body(&b).Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if b.Heartbeat == nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'heartbeat' in request body", nil)
}
@ -128,7 +130,9 @@ func (h ExternalHandler) UptimeKuma(pctx ginext.PreContext) ginext.HTTPResponse
return *errResp
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
MessageID: okResp.Message.MessageID,
}))
})
}

View File

@ -77,12 +77,14 @@ func (h MessageHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse
var b combined
var q combined
var f combined
ctx, g, errResp := h.app.StartRequest(pctx.Form(&f).Query(&q).Body(&b).Start())
ctx, g, errResp := pctx.Form(&f).Query(&q).Body(&b).IgnoreWrongContentType().Start()
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
// query has highest prio, then form, then json
data := dataext.ObjectMerge(dataext.ObjectMerge(b, f), q)
@ -90,7 +92,7 @@ func (h MessageHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse
if errResp != nil {
return *errResp
} else {
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
return finishSuccess(ginext.JSON(http.StatusOK, response{
Success: true,
ErrorID: apierr.NO_ERROR,
ErrorHighlight: -1,
@ -103,4 +105,6 @@ func (h MessageHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse
SCNMessageID: okResp.Message.MessageID,
}))
}
})
}

View File

@ -35,7 +35,9 @@ func (h WebsiteHandler) Index(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return h.serveAsset(g, "index.html", true)
})
}
func (h WebsiteHandler) APIDocs(pctx ginext.PreContext) ginext.HTTPResponse {
@ -45,7 +47,9 @@ func (h WebsiteHandler) APIDocs(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return h.serveAsset(g, "api.html", true)
})
}
func (h WebsiteHandler) APIDocsMore(pctx ginext.PreContext) ginext.HTTPResponse {
@ -55,7 +59,9 @@ func (h WebsiteHandler) APIDocsMore(pctx ginext.PreContext) ginext.HTTPResponse
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return h.serveAsset(g, "api_more.html", true)
})
}
func (h WebsiteHandler) MessageSent(pctx ginext.PreContext) ginext.HTTPResponse {
@ -65,7 +71,9 @@ func (h WebsiteHandler) MessageSent(pctx ginext.PreContext) ginext.HTTPResponse
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return h.serveAsset(g, "message_sent.html", true)
})
}
func (h WebsiteHandler) FaviconIco(pctx ginext.PreContext) ginext.HTTPResponse {
@ -75,7 +83,9 @@ func (h WebsiteHandler) FaviconIco(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return h.serveAsset(g, "favicon.ico", false)
})
}
func (h WebsiteHandler) FaviconPNG(pctx ginext.PreContext) ginext.HTTPResponse {
@ -85,7 +95,9 @@ func (h WebsiteHandler) FaviconPNG(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return h.serveAsset(g, "favicon.png", false)
})
}
func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
@ -95,6 +107,8 @@ func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
type uri struct {
Filename string `uri:"fn"`
}
@ -105,6 +119,7 @@ func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
}
return h.serveAsset(g, "js/"+u.Filename, false)
})
}
func (h WebsiteHandler) CSS(pctx ginext.PreContext) ginext.HTTPResponse {
@ -119,7 +134,9 @@ func (h WebsiteHandler) CSS(pctx ginext.PreContext) ginext.HTTPResponse {
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return h.serveAsset(g, "css/"+u.Filename, false)
})
}
func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginext.HTTPResponse {

View File

@ -1,13 +1,11 @@
package api
import (
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/api/handler"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models"
"blackforestbytes.com/simplecloudnotifier/swagger"
"errors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
@ -61,119 +59,117 @@ func (r *Router) Init(e *ginext.GinWrapper) 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.Routes().Group("/api")
{
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))
commonAPI.Any("/ping").Handle(r.commonHandler.Ping)
commonAPI.POST("/db-test").Handle(r.commonHandler.DatabaseTest)
commonAPI.GET("/health").Handle(r.commonHandler.Health)
commonAPI.POST("/sleep/:secs").Handle(r.commonHandler.Sleep)
}
// ================ Swagger ================
docs := e.Routes().Group("/documentation")
{
docs.GET("/swagger").Handle(wrap(ginext.RedirectTemporary("/documentation/swagger/")))
docs.GET("/swagger/*sub").Handle(wrap(swagger.Handle))
docs.GET("/swagger").Handle(ginext.RedirectTemporary("/documentation/swagger/"))
docs.GET("/swagger/*sub").Handle(swagger.Handle)
}
// ================ Website ================
frontend := e.Routes().Group("")
{
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("/").Handle(r.websiteHandler.Index)
frontend.GET("/index.php").Handle(r.websiteHandler.Index)
frontend.GET("/index.html").Handle(r.websiteHandler.Index)
frontend.GET("/index").Handle(r.websiteHandler.Index)
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").Handle(r.websiteHandler.APIDocs)
frontend.GET("/api.php").Handle(r.websiteHandler.APIDocs)
frontend.GET("/api.html").Handle(r.websiteHandler.APIDocs)
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("/api_more").Handle(r.websiteHandler.APIDocsMore)
frontend.GET("/api_more.php").Handle(r.websiteHandler.APIDocsMore)
frontend.GET("/api_more.html").Handle(r.websiteHandler.APIDocsMore)
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("/message_sent").Handle(r.websiteHandler.MessageSent)
frontend.GET("/message_sent.php").Handle(r.websiteHandler.MessageSent)
frontend.GET("/message_sent.html").Handle(r.websiteHandler.MessageSent)
frontend.GET("/favicon.ico").Handle(wrap(r.websiteHandler.FaviconIco))
frontend.GET("/favicon.png").Handle(wrap(r.websiteHandler.FaviconPNG))
frontend.GET("/favicon.ico").Handle(r.websiteHandler.FaviconIco)
frontend.GET("/favicon.png").Handle(r.websiteHandler.FaviconPNG)
frontend.GET("/js/:fn").Handle(wrap(r.websiteHandler.Javascript))
frontend.GET("/css/:fn").Handle(wrap(r.websiteHandler.CSS))
frontend.GET("/js/:fn").Handle(r.websiteHandler.Javascript)
frontend.GET("/css/:fn").Handle(r.websiteHandler.CSS)
}
// ================ Compat (v1) ================
compat := e.Routes().Group("/api")
{
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))
compat.GET("/register.php").Handle(r.compatHandler.Register)
compat.GET("/info.php").Handle(r.compatHandler.Info)
compat.GET("/ack.php").Handle(r.compatHandler.Ack)
compat.GET("/requery.php").Handle(r.compatHandler.Requery)
compat.GET("/update.php").Handle(r.compatHandler.Update)
compat.GET("/expand.php").Handle(r.compatHandler.Expand)
compat.GET("/upgrade.php").Handle(r.compatHandler.Upgrade)
}
// ================ Manage API (v2) ================
apiv2 := e.Routes().Group("/api/v2/")
{
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.POST("/users").Handle(r.apiHandler.CreateUser)
apiv2.GET("/users/:uid").Handle(r.apiHandler.GetUser)
apiv2.PATCH("/users/:uid").Handle(r.apiHandler.UpdateUser)
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/keys").Handle(r.apiHandler.ListUserKeys)
apiv2.POST("/users/:uid/keys").Handle(r.apiHandler.CreateUserKey)
apiv2.GET("/users/:uid/keys/current").Handle(r.apiHandler.GetCurrentUserKey)
apiv2.GET("/users/:uid/keys/:kid").Handle(r.apiHandler.GetUserKey)
apiv2.PATCH("/users/:uid/keys/:kid").Handle(r.apiHandler.UpdateUserKey)
apiv2.DELETE("/users/:uid/keys/:kid").Handle(r.apiHandler.DeleteUserKey)
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/clients").Handle(r.apiHandler.ListClients)
apiv2.GET("/users/:uid/clients/:cid").Handle(r.apiHandler.GetClient)
apiv2.PATCH("/users/:uid/clients/:cid").Handle(r.apiHandler.UpdateClient)
apiv2.POST("/users/:uid/clients").Handle(r.apiHandler.AddClient)
apiv2.DELETE("/users/:uid/clients/:cid").Handle(r.apiHandler.DeleteClient)
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/channels").Handle(r.apiHandler.ListChannels)
apiv2.POST("/users/:uid/channels").Handle(r.apiHandler.CreateChannel)
apiv2.GET("/users/:uid/channels/:cid").Handle(r.apiHandler.GetChannel)
apiv2.PATCH("/users/:uid/channels/:cid").Handle(r.apiHandler.UpdateChannel)
apiv2.GET("/users/:uid/channels/:cid/messages").Handle(r.apiHandler.ListChannelMessages)
apiv2.GET("/users/:uid/channels/:cid/subscriptions").Handle(r.apiHandler.ListChannelSubscriptions)
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("/users/:uid/subscriptions").Handle(r.apiHandler.ListUserSubscriptions)
apiv2.POST("/users/:uid/subscriptions").Handle(r.apiHandler.CreateSubscription)
apiv2.GET("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.GetSubscription)
apiv2.DELETE("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.CancelSubscription)
apiv2.PATCH("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.UpdateSubscription)
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("/messages").Handle(r.apiHandler.ListMessages)
apiv2.GET("/messages/:mid").Handle(r.apiHandler.GetMessage)
apiv2.DELETE("/messages/:mid").Handle(r.apiHandler.DeleteMessage)
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))
apiv2.GET("/preview/users/:uid").Handle(r.apiHandler.GetUserPreview)
apiv2.GET("/preview/keys/:kid").Handle(r.apiHandler.GetUserKeyPreview)
apiv2.GET("/preview/channels/:cid").Handle(r.apiHandler.GetChannelPreview)
}
// ================ Send API (unversioned) ================
sendAPI := e.Routes().Group("")
{
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("/").Handle(r.messageHandler.SendMessage)
sendAPI.POST("/send").Handle(r.messageHandler.SendMessage)
sendAPI.POST("/send.php").Handle(r.compatHandler.SendMessage)
sendAPI.POST("/external/v1/uptime-kuma").Handle(wrap(r.externalHandler.UptimeKuma)
sendAPI.POST("/external/v1/uptime-kuma").Handle(r.externalHandler.UptimeKuma)
}

View File

@ -1,195 +0,0 @@
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/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 RequestLogAcceptor interface {
InsertRequestLog(data models.RequestLog)
}
func Wrap(rlacc RequestLogAcceptor, fn ginext.WHandlerFunc) ginext.WHandlerFunc {
maxRetry := scn.Conf.RequestMaxRetry
retrySleep := scn.Conf.RequestRetrySleep
return func(pctx *ginext.PreContext) {
reqctx := g.Request.Context()
if g.Request.Body != nil {
g.Request.Body = dataext.NewBufferedReadCloser(g.Request.Body)
}
t0 := time.Now()
for ctr := 1; ; ctr++ {
wrap, stackTrace, panicObj := callPanicSafe(fn, g)
if panicObj != nil {
log.Error().Interface("panicObj", panicObj).Msg("Panic occured (in gin handler)")
log.Error().Msg(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() {
if scn.Conf.ReqLogEnabled {
rlacc.InsertRequestLog(createRequestLog(g, t0, ctr, nil, langext.Ptr("Writing in WrapperFunc is not supported")))
}
panic("Writing in WrapperFunc is not supported")
}
if ctr < maxRetry && isSqlite3Busy(wrap) {
log.Warn().Int("counter", ctr).Str("url", g.Request.URL.String()).Msg("Retry request (ErrBusy)")
err := resetBody(g)
if err != nil {
panic(err)
}
time.Sleep(time.Duration(int64(float64(retrySleep) * (0.5 + rand.Float64()))))
continue
}
if reqctx.Err() == nil {
if scn.Conf.ReqLogEnabled {
rlacc.InsertRequestLog(createRequestLog(g, t0, ctr, wrap, nil))
}
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)
}
return
}
}
}
func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp ginext.HTTPResponse, panicstr *string) models.RequestLog {
t1 := time.Now()
ua := g.Request.UserAgent()
auth := g.Request.Header.Get("Authorization")
ct := g.Request.Header.Get("Content-Type")
var reqbody []byte = nil
if g.Request.Body != nil {
brcbody, err := g.Request.Body.(dataext.BufferedReadCloser).BufferedAll()
if err == nil {
reqbody = brcbody
}
}
var strreqbody *string = nil
if len(reqbody) < scn.Conf.ReqLogMaxBodySize {
strreqbody = langext.Ptr(string(reqbody))
}
var respbody *string = nil
var strrespbody *string = nil
if resp != nil {
if resp2, ok := resp.(ginext.InspectableHTTPResponse); ok {
respbody = resp2.BodyString(g)
if respbody != nil && len(*respbody) < scn.Conf.ReqLogMaxBodySize {
strrespbody = respbody
}
}
}
permObj, hasPerm := g.Get("perm")
hasTok := false
if hasPerm {
hasTok = permObj.(models.PermissionSet).Token != nil
}
return models.RequestLog{
Method: g.Request.Method,
URI: g.Request.URL.String(),
UserAgent: langext.Conditional(ua == "", nil, &ua),
Authentication: langext.Conditional(auth == "", nil, &auth),
RequestBody: strreqbody,
RequestBodySize: int64(len(reqbody)),
RequestContentType: ct,
RemoteIP: g.RemoteIP(),
KeyID: langext.ConditionalFn10(hasTok, func() *models.KeyTokenID { return langext.Ptr(permObj.(models.PermissionSet).Token.KeyTokenID) }, nil),
UserID: langext.ConditionalFn10(hasTok, func() *models.UserID { return langext.Ptr(permObj.(models.PermissionSet).Token.OwnerUserID) }, nil),
Permissions: langext.ConditionalFn10(hasTok, func() *string { return langext.Ptr(permObj.(models.PermissionSet).Token.Permissions.String()) }, nil),
ResponseStatuscode: langext.ConditionalFn10(resp != nil, func() *int64 { return langext.Ptr(int64(resp.Statuscode())) }, nil),
ResponseBodySize: langext.ConditionalFn10(strrespbody != nil, func() *int64 { return langext.Ptr(int64(len(*respbody))) }, nil),
ResponseBody: strrespbody,
ResponseContentType: langext.ConditionalFn10(resp != nil, func() string { return resp.ContentType() }, ""),
RetryCount: int64(ctr),
Panicked: panicstr != nil,
PanicStr: panicstr,
ProcessingTime: t1.Sub(t0),
TimestampStart: t0,
TimestampFinish: t1,
}
}
func callPanicSafe(fn ginext.WHandlerFunc, g ginext.PreContext) (res ginext.HTTPResponse, stackTrace string, panicObj any) {
defer func() {
if rec := recover(); rec != nil {
res = nil
stackTrace = string(debug.Stack())
panicObj = rec
}
}()
res = fn(g)
return res, "", nil
}
func resetBody(g *gin.Context) error {
if g.Request.Body == nil {
return nil
}
err := g.Request.Body.(dataext.BufferedReadCloser).Reset()
if err != nil {
return err
}
return nil
}
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
}
}
}
}
return false
}

View File

@ -3,8 +3,11 @@ package main
import (
"blackforestbytes.com/simplecloudnotifier/db/schema"
"context"
"database/sql"
"fmt"
"github.com/glebarez/go-sqlite"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -15,7 +18,9 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
sqlite3.Version() // ensure slite3 loaded
if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()
}
fmt.Println()

View File

@ -135,7 +135,7 @@ func main() {
if err != nil {
panic(err)
}
dbold := sq.NewDB(_dbold)
dbold := sq.NewDB(_dbold, sq.DBOptions{})
rowsUser, err := dbold.Query(ctx, "SELECT * FROM users", sq.PP{})
if err != nil {

View File

@ -11,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"time"
)
func main() {
@ -36,7 +37,8 @@ func main() {
AllowCors: &conf.Cors,
GinDebug: &conf.GinDebug,
BufferBody: langext.PTrue,
Timeout: &conf.RequestTimeout,
Timeout: langext.Ptr(time.Duration(int64(conf.RequestTimeout) * int64(conf.RequestMaxRetry))),
BuildRequestBindError: logic.BuildGinRequestError,
})
router := api.NewRouter(app)

View File

@ -26,7 +26,12 @@ type Database struct {
func NewLogsDatabase(cfg server.Config) (*Database, error) {
conf := cfg.DBLogs
url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", conf.File, conf.Journal, conf.Timeout.Milliseconds(), langext.FormatBool(conf.CheckForeignKeys, "true", "false"), conf.BusyTimeout.Milliseconds())
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)",
conf.File,
conf.Journal,
conf.Timeout.Milliseconds(),
langext.FormatBool(conf.CheckForeignKeys, "true", "false"),
conf.BusyTimeout.Milliseconds())
if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()

View File

@ -26,7 +26,12 @@ type Database struct {
func NewPrimaryDatabase(cfg server.Config) (*Database, error) {
conf := cfg.DBMain
url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", conf.File, conf.Journal, conf.Timeout.Milliseconds(), langext.FormatBool(conf.CheckForeignKeys, "true", "false"), conf.BusyTimeout.Milliseconds())
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)",
conf.File,
conf.Journal,
conf.Timeout.Milliseconds(),
langext.FormatBool(conf.CheckForeignKeys, "true", "false"),
conf.BusyTimeout.Milliseconds())
if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()

View File

@ -26,7 +26,12 @@ type Database struct {
func NewRequestsDatabase(cfg server.Config) (*Database, error) {
conf := cfg.DBRequests
url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", conf.File, conf.Journal, conf.Timeout.Milliseconds(), langext.FormatBool(conf.CheckForeignKeys, "true", "false"), conf.BusyTimeout.Milliseconds())
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)",
conf.File,
conf.Journal,
conf.Timeout.Milliseconds(),
langext.FormatBool(conf.CheckForeignKeys, "true", "false"),
conf.BusyTimeout.Milliseconds())
if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()

View File

@ -12,7 +12,7 @@ require (
github.com/jmoiron/sqlx v1.4.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/rs/zerolog v1.33.0
gogs.mikescher.com/BlackForestBytes/goext v0.0.482
gogs.mikescher.com/BlackForestBytes/goext v0.0.485
gopkg.in/loremipsum.v1 v1.1.2
)

View File

@ -113,6 +113,12 @@ go.mongodb.org/mongo-driver v1.16.0 h1:tpRsfBJMROVHKpdGyc1BBEzzjDUWjItxbVSZ8Ls4B
go.mongodb.org/mongo-driver v1.16.0/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
gogs.mikescher.com/BlackForestBytes/goext v0.0.482 h1:veU8oJdGZ9rjLB8sluagBduiBs3BbEDf60sGmEEv8lk=
gogs.mikescher.com/BlackForestBytes/goext v0.0.482/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM=
gogs.mikescher.com/BlackForestBytes/goext v0.0.483 h1:fxhe3U5bpkv1SvSae7F/ixPp7DUiRxga4Zvg82iQSsI=
gogs.mikescher.com/BlackForestBytes/goext v0.0.483/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM=
gogs.mikescher.com/BlackForestBytes/goext v0.0.484 h1:fu60J83OBtnUkXCIt+dycHrin5OUmL1B46IY6GTQosw=
gogs.mikescher.com/BlackForestBytes/goext v0.0.484/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM=
gogs.mikescher.com/BlackForestBytes/goext v0.0.485 h1:hjXxl7bwHkzYBpfsX81UZj929bKUDIoNFl0XQSvt4Qk=
gogs.mikescher.com/BlackForestBytes/goext v0.0.485/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=

View File

@ -71,8 +71,11 @@ func (ac *AppContext) Cancel() {
}
ac.transaction = nil
}
if ac.cancelFunc != nil {
ac.cancelFunc()
}
}
func (ac *AppContext) RequestURI() string {
if ac.ginContext != nil && ac.ginContext.Request != nil {
@ -82,7 +85,7 @@ func (ac *AppContext) RequestURI() string {
}
}
func (ac *AppContext) FinishSuccess(res ginext.HTTPResponse) ginext.HTTPResponse {
func (ac *AppContext) _FinishSuccess(res ginext.HTTPResponse) ginext.HTTPResponse {
if ac.cancelled {
panic("Cannot finish a cancelled request")
}

View File

@ -2,8 +2,6 @@ package logic
import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/db"
"blackforestbytes.com/simplecloudnotifier/db/simplectx"
"blackforestbytes.com/simplecloudnotifier/google"
@ -11,10 +9,8 @@ import (
"blackforestbytes.com/simplecloudnotifier/push"
"context"
"errors"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rext"
"gogs.mikescher.com/BlackForestBytes/goext/syncext"
"net"
@ -208,32 +204,6 @@ func (app *Application) Migrate() error {
return app.Database.Migrate(ctx)
}
type RequestOptions struct {
IgnoreWrongContentType bool
}
func (app *Application) StartRequest(gectx *ginext.AppContext, g *gin.Context, r *ginext.HTTPResponse) (*AppContext, *gin.Context, *ginext.HTTPResponse) {
if r != nil {
return nil, g, r
}
actx := CreateAppContext(app, g, gectx, gectx.Cancel)
authheader := g.GetHeader("Authorization")
perm, err := app.getPermissions(actx, authheader)
if err != nil {
gectx.Cancel()
return nil, g, langext.Ptr(ginresp.APIError(g, 400, apierr.PERM_QUERY_FAIL, "Failed to determine permissions", err))
}
actx.permissions = perm
g.Set("perm", perm)
return actx, g, nil
}
func (app *Application) NewSimpleTransactionContext(timeout time.Duration) *simplectx.SimpleContext {
ictx, cancel := context.WithTimeout(context.Background(), timeout)
return simplectx.CreateSimpleContext(ictx, cancel)

View File

@ -10,6 +10,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"

View File

@ -6,6 +6,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)

240
scnserver/logic/request.go Normal file
View File

@ -0,0 +1,240 @@
package logic
import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/models"
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"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 RequestOptions struct {
IgnoreWrongContentType bool
}
func (app *Application) DoRequest(gectx *ginext.AppContext, g *gin.Context, fn func(ctx *AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
maxRetry := scn.Conf.RequestMaxRetry
retrySleep := scn.Conf.RequestRetrySleep
reqctx := g.Request.Context()
t0 := time.Now()
for ctr := 1; ; ctr++ {
ictx, cancel := context.WithTimeout(gectx, app.Config.RequestTimeout)
actx := CreateAppContext(app, g, ictx, cancel)
wrap, stackTrace, panicObj := callPanicSafe(func(ctx *AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
authheader := g.GetHeader("Authorization")
perm, err := app.getPermissions(actx, authheader)
if err != nil {
cancel()
return ginresp.APIError(g, 400, apierr.PERM_QUERY_FAIL, "Failed to determine permissions", err)
}
actx.permissions = perm
g.Set("perm", perm)
return fn(actx, finishSuccess)
}, actx, actx._FinishSuccess)
if panicObj != nil {
log.Error().Interface("panicObj", panicObj).Msg("Panic occured (in gin handler)")
log.Error().Msg(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() {
if scn.Conf.ReqLogEnabled {
app.InsertRequestLog(createRequestLog(g, t0, ctr, nil, langext.Ptr("Writing in WrapperFunc is not supported")))
}
panic("Writing in WrapperFunc is not supported")
}
if ctr < maxRetry && isSqlite3Busy(wrap) {
log.Warn().Int("counter", ctr).Str("url", g.Request.URL.String()).Msg("Retry request (ErrBusy)")
err := resetBody(g)
if err != nil {
panic(err)
}
time.Sleep(time.Duration(int64(float64(retrySleep) * (0.5 + rand.Float64()))))
continue
}
if reqctx.Err() == nil {
if scn.Conf.ReqLogEnabled {
app.InsertRequestLog(createRequestLog(g, t0, ctr, wrap, nil))
}
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]"))
}
}
return wrap
}
}
func callPanicSafe(fn func(ctx *AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse, actx *AppContext, fnFin func(r ginext.HTTPResponse) ginext.HTTPResponse) (res ginext.HTTPResponse, stackTrace string, panicObj any) {
defer func() {
if rec := recover(); rec != nil {
res = nil
stackTrace = string(debug.Stack())
panicObj = rec
}
}()
res = fn(actx, fnFin)
return res, "", nil
}
func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp ginext.HTTPResponse, panicstr *string) models.RequestLog {
t1 := time.Now()
ua := g.Request.UserAgent()
auth := g.Request.Header.Get("Authorization")
ct := g.Request.Header.Get("Content-Type")
var reqbody []byte = nil
if g.Request.Body != nil {
brcbody, err := g.Request.Body.(dataext.BufferedReadCloser).BufferedAll()
if err == nil {
reqbody = brcbody
}
}
var strreqbody *string = nil
if len(reqbody) < scn.Conf.ReqLogMaxBodySize {
strreqbody = langext.Ptr(string(reqbody))
}
var respbody *string = nil
var strrespbody *string = nil
if resp != nil {
if resp2, ok := resp.(ginext.InspectableHTTPResponse); ok {
respbody = resp2.BodyString(g)
if respbody != nil && len(*respbody) < scn.Conf.ReqLogMaxBodySize {
strrespbody = respbody
}
}
}
permObj, hasPerm := g.Get("perm")
hasTok := false
if hasPerm {
hasTok = permObj.(models.PermissionSet).Token != nil
}
var statuscode *int64 = nil
if resp != nil {
if resp2, ok := resp.(ginext.InspectableHTTPResponse); ok {
statuscode = langext.Ptr(int64(resp2.Statuscode()))
}
}
var contentType = ""
if resp != nil {
if resp2, ok := resp.(ginext.InspectableHTTPResponse); ok {
contentType = resp2.ContentType()
}
}
return models.RequestLog{
Method: g.Request.Method,
URI: g.Request.URL.String(),
UserAgent: langext.Conditional(ua == "", nil, &ua),
Authentication: langext.Conditional(auth == "", nil, &auth),
RequestBody: strreqbody,
RequestBodySize: int64(len(reqbody)),
RequestContentType: ct,
RemoteIP: g.RemoteIP(),
KeyID: langext.ConditionalFn10(hasTok, func() *models.KeyTokenID { return langext.Ptr(permObj.(models.PermissionSet).Token.KeyTokenID) }, nil),
UserID: langext.ConditionalFn10(hasTok, func() *models.UserID { return langext.Ptr(permObj.(models.PermissionSet).Token.OwnerUserID) }, nil),
Permissions: langext.ConditionalFn10(hasTok, func() *string { return langext.Ptr(permObj.(models.PermissionSet).Token.Permissions.String()) }, nil),
ResponseStatuscode: statuscode,
ResponseBodySize: langext.ConditionalFn10(strrespbody != nil, func() *int64 { return langext.Ptr(int64(len(*respbody))) }, nil),
ResponseBody: strrespbody,
ResponseContentType: contentType,
RetryCount: int64(ctr),
Panicked: panicstr != nil,
PanicStr: panicstr,
ProcessingTime: t1.Sub(t0),
TimestampStart: t0,
TimestampFinish: t1,
}
}
func resetBody(g *gin.Context) error {
if g.Request.Body == nil {
return nil
}
err := g.Request.Body.(dataext.BufferedReadCloser).Reset()
if err != nil {
return err
}
return nil
}
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
}
}
}
}
return false
}
func BuildGinRequestError(g *gin.Context, fieldtype string, err error) ginext.HTTPResponse {
switch fieldtype {
case "URI":
return ginresp.APIError(g, 400, apierr.BINDFAIL_URI_PARAM, "Failed to read uri", err)
case "QUERY":
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Failed to read query", err)
case "JSON":
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Failed to read JSON body", err)
case "BODY":
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Failed to read query", err)
case "FORM":
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Failed to read multipart-form / urlencoded-form", err)
case "HEADER":
return ginresp.APIError(g, 400, apierr.BINDFAIL_HEADER_PARAM, "Failed to read header", err)
case "INIT":
return ginresp.APIError(g, 400, apierr.INTERNAL_EXCEPTION, "Failed to init context", err)
default:
return ginresp.APIError(g, 400, apierr.INTERNAL_EXCEPTION, "Failed to init", err)
}
}

View File

@ -5,7 +5,7 @@ package models
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
import "gogs.mikescher.com/BlackForestBytes/goext/enums"
const ChecksumEnumGenerator = "fd2e0463f7720d853f7a3394352c084ac7d086e9e012caa3d3d70a6e83749970" // GoExtVersion: 0.0.482
const ChecksumEnumGenerator = "ba14f2f5d0b0357f248dcbd12933de102c80f1e61be697a37ebb723609fc0c59" // GoExtVersion: 0.0.485
// ================================ ClientType ================================
//

View File

@ -15,7 +15,7 @@ import "reflect"
import "regexp"
import "strings"
const ChecksumCharsetIDGenerator = "fd2e0463f7720d853f7a3394352c084ac7d086e9e012caa3d3d70a6e83749970" // GoExtVersion: 0.0.482
const ChecksumCharsetIDGenerator = "ba14f2f5d0b0357f248dcbd12933de102c80f1e61be697a37ebb723609fc0c59" // GoExtVersion: 0.0.485
const idlen = 24

View File

@ -19,37 +19,61 @@
"parameters": [
{
"type": "string",
"example": "test",
"name": "channel",
"in": "query"
},
{
"type": "string",
"example": "This is a message",
"name": "content",
"in": "query"
},
{
"type": "string",
"example": "P3TNH8mvv14fm",
"name": "key",
"in": "query"
},
{
"type": "string",
"example": "db8b0e6a-a08c-4646",
"name": "msg_id",
"in": "query"
},
{
"enum": [
0,
1,
2
],
"type": "integer",
"example": 1,
"name": "priority",
"in": "query"
},
{
"type": "string",
"example": "example-server",
"name": "sender_name",
"in": "query"
},
{
"type": "number",
"example": 1669824037,
"name": "timestamp",
"in": "query"
},
{
"type": "string",
"example": "Hello World",
"name": "title",
"in": "query"
},
{
"type": "integer",
"name": "user_id",
"in": "query"
},
{
"type": "string",
"name": "user_key",
"example": "7725",
"name": "user_id",
"in": "query"
},
{
@ -62,37 +86,61 @@
},
{
"type": "string",
"example": "test",
"name": "channel",
"in": "formData"
},
{
"type": "string",
"example": "This is a message",
"name": "content",
"in": "formData"
},
{
"type": "string",
"example": "P3TNH8mvv14fm",
"name": "key",
"in": "formData"
},
{
"type": "string",
"example": "db8b0e6a-a08c-4646",
"name": "msg_id",
"in": "formData"
},
{
"enum": [
0,
1,
2
],
"type": "integer",
"example": 1,
"name": "priority",
"in": "formData"
},
{
"type": "string",
"example": "example-server",
"name": "sender_name",
"in": "formData"
},
{
"type": "number",
"example": 1669824037,
"name": "timestamp",
"in": "formData"
},
{
"type": "string",
"example": "Hello World",
"name": "title",
"in": "formData"
},
{
"type": "integer",
"name": "user_id",
"in": "formData"
},
{
"type": "string",
"name": "user_key",
"example": "7725",
"name": "user_id",
"in": "formData"
}
],
@ -2717,37 +2765,61 @@
"parameters": [
{
"type": "string",
"example": "test",
"name": "channel",
"in": "query"
},
{
"type": "string",
"example": "This is a message",
"name": "content",
"in": "query"
},
{
"type": "string",
"example": "P3TNH8mvv14fm",
"name": "key",
"in": "query"
},
{
"type": "string",
"example": "db8b0e6a-a08c-4646",
"name": "msg_id",
"in": "query"
},
{
"enum": [
0,
1,
2
],
"type": "integer",
"example": 1,
"name": "priority",
"in": "query"
},
{
"type": "string",
"example": "example-server",
"name": "sender_name",
"in": "query"
},
{
"type": "number",
"example": 1669824037,
"name": "timestamp",
"in": "query"
},
{
"type": "string",
"example": "Hello World",
"name": "title",
"in": "query"
},
{
"type": "integer",
"name": "user_id",
"in": "query"
},
{
"type": "string",
"name": "user_key",
"example": "7725",
"name": "user_id",
"in": "query"
},
{
@ -2760,37 +2832,61 @@
},
{
"type": "string",
"example": "test",
"name": "channel",
"in": "formData"
},
{
"type": "string",
"example": "This is a message",
"name": "content",
"in": "formData"
},
{
"type": "string",
"example": "P3TNH8mvv14fm",
"name": "key",
"in": "formData"
},
{
"type": "string",
"example": "db8b0e6a-a08c-4646",
"name": "msg_id",
"in": "formData"
},
{
"enum": [
0,
1,
2
],
"type": "integer",
"example": 1,
"name": "priority",
"in": "formData"
},
{
"type": "string",
"example": "example-server",
"name": "sender_name",
"in": "formData"
},
{
"type": "number",
"example": 1669824037,
"name": "timestamp",
"in": "formData"
},
{
"type": "string",
"example": "Hello World",
"name": "title",
"in": "formData"
},
{
"type": "integer",
"name": "user_id",
"in": "formData"
},
{
"type": "string",
"name": "user_key",
"example": "7725",
"name": "user_id",
"in": "formData"
}
],
@ -2839,72 +2935,120 @@
"parameters": [
{
"type": "string",
"example": "test",
"name": "channel",
"in": "query"
},
{
"type": "string",
"example": "This is a message",
"name": "content",
"in": "query"
},
{
"type": "string",
"example": "P3TNH8mvv14fm",
"name": "key",
"in": "query"
},
{
"type": "string",
"example": "db8b0e6a-a08c-4646",
"name": "msg_id",
"in": "query"
},
{
"enum": [
0,
1,
2
],
"type": "integer",
"example": 1,
"name": "priority",
"in": "query"
},
{
"type": "string",
"example": "example-server",
"name": "sender_name",
"in": "query"
},
{
"type": "number",
"example": 1669824037,
"name": "timestamp",
"in": "query"
},
{
"type": "string",
"example": "Hello World",
"name": "title",
"in": "query"
},
{
"type": "integer",
"type": "string",
"example": "7725",
"name": "user_id",
"in": "query"
},
{
"type": "string",
"name": "user_key",
"in": "query"
"example": "test",
"name": "channel",
"in": "formData"
},
{
"type": "string",
"example": "This is a message",
"name": "content",
"in": "formData"
},
{
"type": "string",
"example": "P3TNH8mvv14fm",
"name": "key",
"in": "formData"
},
{
"type": "string",
"example": "db8b0e6a-a08c-4646",
"name": "msg_id",
"in": "formData"
},
{
"enum": [
0,
1,
2
],
"type": "integer",
"example": 1,
"name": "priority",
"in": "formData"
},
{
"type": "string",
"example": "example-server",
"name": "sender_name",
"in": "formData"
},
{
"type": "number",
"example": 1669824037,
"name": "timestamp",
"in": "formData"
},
{
"type": "string",
"example": "Hello World",
"name": "title",
"in": "formData"
},
{
"type": "integer",
"name": "user_id",
"in": "formData"
},
{
"type": "string",
"name": "user_key",
"example": "7725",
"name": "user_id",
"in": "formData"
}
],
@ -2959,6 +3103,7 @@
1151,
1152,
1153,
1152,
1161,
1171,
1201,
@ -3004,6 +3149,7 @@
"BINDFAIL_QUERY_PARAM",
"BINDFAIL_BODY_PARAM",
"BINDFAIL_URI_PARAM",
"BINDFAIL_HEADER_PARAM",
"INVALID_BODY_PARAM",
"INVALID_ENUM_VALUE",
"NO_TITLE",
@ -3401,26 +3547,46 @@
"handler.SendMessage.combined": {
"type": "object",
"properties": {
"channel": {
"type": "string",
"example": "test"
},
"content": {
"type": "string"
"type": "string",
"example": "This is a message"
},
"key": {
"type": "string",
"example": "P3TNH8mvv14fm"
},
"msg_id": {
"type": "string"
"type": "string",
"example": "db8b0e6a-a08c-4646"
},
"priority": {
"type": "integer"
"type": "integer",
"enum": [
0,
1,
2
],
"example": 1
},
"sender_name": {
"type": "string",
"example": "example-server"
},
"timestamp": {
"type": "number"
"type": "number",
"example": 1669824037
},
"title": {
"type": "string"
"type": "string",
"example": "Hello World"
},
"user_id": {
"type": "integer"
},
"user_key": {
"type": "string"
"type": "string",
"example": "7725"
}
}
},
@ -3449,7 +3615,7 @@
"type": "integer"
},
"scn_msg_id": {
"type": "integer"
"type": "string"
},
"success": {
"type": "boolean"

View File

@ -14,6 +14,7 @@ definitions:
- 1151
- 1152
- 1153
- 1152
- 1161
- 1171
- 1201
@ -59,6 +60,7 @@ definitions:
- BINDFAIL_QUERY_PARAM
- BINDFAIL_BODY_PARAM
- BINDFAIL_URI_PARAM
- BINDFAIL_HEADER_PARAM
- INVALID_BODY_PARAM
- INVALID_ENUM_VALUE
- NO_TITLE
@ -327,19 +329,36 @@ definitions:
type: object
handler.SendMessage.combined:
properties:
channel:
example: test
type: string
content:
example: This is a message
type: string
key:
example: P3TNH8mvv14fm
type: string
msg_id:
example: db8b0e6a-a08c-4646
type: string
priority:
enum:
- 0
- 1
- 2
example: 1
type: integer
sender_name:
example: example-server
type: string
timestamp:
example: 1669824037
type: number
title:
example: Hello World
type: string
user_id:
type: integer
user_key:
example: "7725"
type: string
type: object
handler.SendMessage.response:
@ -359,7 +378,7 @@ definitions:
quota_max:
type: integer
scn_msg_id:
type: integer
type: string
success:
type: boolean
suppress_send:
@ -785,52 +804,90 @@ paths:
description: All parameter can be set via query-parameter or the json body.
Only UserID, UserKey and Title are required
parameters:
- in: query
- example: test
in: query
name: channel
type: string
- example: This is a message
in: query
name: content
type: string
- in: query
- example: P3TNH8mvv14fm
in: query
name: key
type: string
- example: db8b0e6a-a08c-4646
in: query
name: msg_id
type: string
- in: query
- enum:
- 0
- 1
- 2
example: 1
in: query
name: priority
type: integer
- in: query
- example: example-server
in: query
name: sender_name
type: string
- example: 1669824037
in: query
name: timestamp
type: number
- in: query
- example: Hello World
in: query
name: title
type: string
- in: query
- example: "7725"
in: query
name: user_id
type: integer
- in: query
name: user_key
type: string
- description: ' '
in: body
name: post_body
schema:
$ref: '#/definitions/handler.SendMessage.combined'
- in: formData
- example: test
in: formData
name: channel
type: string
- example: This is a message
in: formData
name: content
type: string
- in: formData
- example: P3TNH8mvv14fm
in: formData
name: key
type: string
- example: db8b0e6a-a08c-4646
in: formData
name: msg_id
type: string
- in: formData
- enum:
- 0
- 1
- 2
example: 1
in: formData
name: priority
type: integer
- in: formData
- example: example-server
in: formData
name: sender_name
type: string
- example: 1669824037
in: formData
name: timestamp
type: number
- in: formData
- example: Hello World
in: formData
name: title
type: string
- in: formData
- example: "7725"
in: formData
name: user_id
type: integer
- in: formData
name: user_key
type: string
responses:
"200":
@ -2630,52 +2687,90 @@ paths:
description: All parameter can be set via query-parameter or the json body.
Only UserID, UserKey and Title are required
parameters:
- in: query
- example: test
in: query
name: channel
type: string
- example: This is a message
in: query
name: content
type: string
- in: query
- example: P3TNH8mvv14fm
in: query
name: key
type: string
- example: db8b0e6a-a08c-4646
in: query
name: msg_id
type: string
- in: query
- enum:
- 0
- 1
- 2
example: 1
in: query
name: priority
type: integer
- in: query
- example: example-server
in: query
name: sender_name
type: string
- example: 1669824037
in: query
name: timestamp
type: number
- in: query
- example: Hello World
in: query
name: title
type: string
- in: query
- example: "7725"
in: query
name: user_id
type: integer
- in: query
name: user_key
type: string
- description: ' '
in: body
name: post_body
schema:
$ref: '#/definitions/handler.SendMessage.combined'
- in: formData
- example: test
in: formData
name: channel
type: string
- example: This is a message
in: formData
name: content
type: string
- in: formData
- example: P3TNH8mvv14fm
in: formData
name: key
type: string
- example: db8b0e6a-a08c-4646
in: formData
name: msg_id
type: string
- in: formData
- enum:
- 0
- 1
- 2
example: 1
in: formData
name: priority
type: integer
- in: formData
- example: example-server
in: formData
name: sender_name
type: string
- example: 1669824037
in: formData
name: timestamp
type: number
- in: formData
- example: Hello World
in: formData
name: title
type: string
- in: formData
- example: "7725"
in: formData
name: user_id
type: integer
- in: formData
name: user_key
type: string
responses:
"200":
@ -2708,47 +2803,85 @@ paths:
description: All parameter can be set via query-parameter or form-data body.
Only UserID, UserKey and Title are required
parameters:
- in: query
- example: test
in: query
name: channel
type: string
- example: This is a message
in: query
name: content
type: string
- in: query
- example: P3TNH8mvv14fm
in: query
name: key
type: string
- example: db8b0e6a-a08c-4646
in: query
name: msg_id
type: string
- in: query
- enum:
- 0
- 1
- 2
example: 1
in: query
name: priority
type: integer
- in: query
- example: example-server
in: query
name: sender_name
type: string
- example: 1669824037
in: query
name: timestamp
type: number
- in: query
- example: Hello World
in: query
name: title
type: string
- in: query
- example: "7725"
in: query
name: user_id
type: integer
- in: query
name: user_key
type: string
- in: formData
- example: test
in: formData
name: channel
type: string
- example: This is a message
in: formData
name: content
type: string
- in: formData
- example: P3TNH8mvv14fm
in: formData
name: key
type: string
- example: db8b0e6a-a08c-4646
in: formData
name: msg_id
type: string
- in: formData
- enum:
- 0
- 1
- 2
example: 1
in: formData
name: priority
type: integer
- in: formData
- example: example-server
in: formData
name: sender_name
type: string
- example: 1669824037
in: formData
name: timestamp
type: number
- in: formData
- example: Hello World
in: formData
name: title
type: string
- in: formData
- example: "7725"
in: formData
name: user_id
type: integer
- in: formData
name: user_key
type: string
responses:
"200":

View File

@ -124,3 +124,26 @@ func TestRequestLogSimple(t *testing.T) {
}
}
func TestRequestLogAPI(t *testing.T) {
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
data := tt.InitDefaultData(t, ws)
time.Sleep(100 * time.Millisecond)
ctx := ws.NewSimpleTransactionContext(5 * time.Second)
defer ctx.Cancel()
rl1, _, err := ws.Database.Requests.ListRequestLogs(ctx, models.RequestLogFilter{}, nil, ct.Start())
tt.TestFailIfErr(t, err)
time.Sleep(100 * time.Millisecond)
tt.RequestAuthGet[gin.H](t, data.User[0].ReadKey, baseUrl, "/api/v2/users/"+data.User[0].UID)
rl2, _, err := ws.Database.Requests.ListRequestLogs(ctx, models.RequestLogFilter{}, nil, ct.Start())
tt.TestFailIfErr(t, err)
time.Sleep(100 * time.Millisecond)
tt.AssertEqual(t, "requestlog.count", len(rl1)+1, len(rl2))
}

View File

@ -12,7 +12,6 @@ func SetBufLogger() {
buflogger = &BufferWriter{cw: createConsoleWriter()}
log.Logger = createLogger(buflogger)
gin.SetMode(gin.ReleaseMode)
ginext.SuppressGinLogs = true
}
func ClearBufLogger(dump bool) {
@ -23,7 +22,6 @@ func ClearBufLogger(dump bool) {
log.Logger = createLogger(createConsoleWriter())
buflogger = nil
gin.SetMode(gin.TestMode)
ginext.SuppressGinLogs = false
if !dump {
log.Info().Msgf("Suppressed %d logmessages / printf-statements", size)
}

View File

@ -7,6 +7,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/jobs"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/push"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"os"
"path/filepath"
@ -87,7 +88,13 @@ func StartSimpleWebserver(t *testing.T) (*logic.Application, string, func()) {
TestFailErr(t, err)
}
ginengine := ginext.NewEngine(scn.Conf)
ginengine := ginext.NewEngine(ginext.Options{
AllowCors: &scn.Conf.Cors,
GinDebug: &scn.Conf.GinDebug,
BufferBody: langext.PTrue,
Timeout: langext.Ptr(time.Duration(int64(scn.Conf.RequestTimeout) * int64(scn.Conf.RequestMaxRetry))),
BuildRequestBindError: logic.BuildGinRequestError,
})
router := api.NewRouter(app)