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) NAMESPACE=$(shell git rev-parse --abbrev-ref HEAD)
HASH=$(shell git rev-parse HEAD) HASH=$(shell git rev-parse HEAD)
TAGS="timetzdata sqlite_fts5 sqlite_foreign_keys"
.PHONY: test swagger pygmentize docker migrate dgi pygmentize lint docker .PHONY: test swagger pygmentize docker migrate dgi pygmentize lint docker
SWAGGO_VERSION=v1.8.12 SWAGGO_VERSION=v1.8.12
@ -13,7 +15,7 @@ SWAGGO=github.com/swaggo/swag/cmd/swag@$(SWAGGO_VERSION)
build: ids enums swagger pygmentize fmt build: ids enums swagger pygmentize fmt
mkdir -p _build mkdir -p _build
rm -f ./_build/scn_backend 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: enums:
go generate models/enums.go go generate models/enums.go
@ -27,7 +29,7 @@ run: build
gow: gow:
which gow || go install github.com/mitranim/gow@latest 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: dgi:
[ ! -f "DOCKER_GIT_INFO" ] || rm DOCKER_GIT_INFO [ ! -f "DOCKER_GIT_INFO" ] || rm DOCKER_GIT_INFO
@ -99,10 +101,10 @@ fmt: swagger-setup
test: test:
which gotestsum || go install gotest.tools/gotestsum@latest 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: 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 ./_build/scn_migrate
lint: lint:

View File

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

View File

@ -8,18 +8,19 @@ const (
NO_ERROR APIError = 0000 NO_ERROR APIError = 0000
MISSING_UID APIError = 1101 MISSING_UID APIError = 1101
MISSING_TOK APIError = 1102 MISSING_TOK APIError = 1102
MISSING_TITLE APIError = 1103 MISSING_TITLE APIError = 1103
INVALID_PRIO APIError = 1104 INVALID_PRIO APIError = 1104
REQ_METHOD APIError = 1105 REQ_METHOD APIError = 1105
INVALID_CLIENTTYPE APIError = 1106 INVALID_CLIENTTYPE APIError = 1106
PAGETOKEN_ERROR APIError = 1121 PAGETOKEN_ERROR APIError = 1121
BINDFAIL_QUERY_PARAM APIError = 1151 BINDFAIL_QUERY_PARAM APIError = 1151
BINDFAIL_BODY_PARAM APIError = 1152 BINDFAIL_BODY_PARAM APIError = 1152
BINDFAIL_URI_PARAM APIError = 1153 BINDFAIL_URI_PARAM APIError = 1153
INVALID_BODY_PARAM APIError = 1161 BINDFAIL_HEADER_PARAM APIError = 1152
INVALID_ENUM_VALUE APIError = 1171 INVALID_BODY_PARAM APIError = 1161
INVALID_ENUM_VALUE APIError = 1171
NO_TITLE APIError = 1201 NO_TITLE APIError = 1201
TITLE_TOO_LONG APIError = 1202 TITLE_TOO_LONG APIError = 1202

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) 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 { func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) ginext.HTTPResponse {
reqUri := "" reqUri := ""
if g != nil && g.Request != nil { if g != nil && g.Request != nil {

View File

@ -4,6 +4,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/api/apierr" "blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp" "blackforestbytes.com/simplecloudnotifier/api/ginresp"
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken" ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"errors" "errors"
@ -56,67 +57,65 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
} }
defer ctx.Cancel() defer ctx.Cancel()
ctx, errResp := h.app.StartRequest(g, &u, &q, nil, nil) return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp return *permResp
}
sel := strings.ToLower(langext.Coalesce(q.Selector, "owned"))
var res []models.ChannelWithSubscriptionJSON
if sel == "owned" {
channels, err := h.database.ListChannelsByOwner(ctx, u.UserID, u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
} }
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(true) })
} else if sel == "subscribed_any" { sel := strings.ToLower(langext.Coalesce(q.Selector, "owned"))
var res []models.ChannelWithSubscriptionJSON
if sel == "owned" {
channels, err := h.database.ListChannelsByOwner(ctx, u.UserID, u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(true) })
} else if sel == "subscribed_any" {
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, nil)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else if sel == "all_any" {
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, nil)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else if sel == "subscribed" {
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, langext.Ptr(true))
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else if sel == "all" {
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, langext.Ptr(true))
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else {
return ginresp.APIError(g, 400, apierr.INVALID_ENUM_VALUE, "Invalid value for the [selector] parameter", nil)
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, nil)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
} }
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else if sel == "all_any" { return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, nil) })
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else if sel == "subscribed" {
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, langext.Ptr(true))
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else if sel == "all" {
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, langext.Ptr(true))
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
} else {
return ginresp.APIError(g, 400, apierr.INVALID_ENUM_VALUE, "Invalid value for the [selector] parameter", nil)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
} }
// GetChannel swaggerdoc // GetChannel swaggerdoc
@ -142,25 +141,29 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true) if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true))) channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
})
} }
// CreateChannel swaggerdoc // CreateChannel swaggerdoc
@ -192,75 +195,78 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
if b.Name == "" { if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Missing parameter: name", nil) return *permResp
}
channelDisplayName := h.app.NormalizeChannelDisplayName(b.Name)
channelInternalName := h.app.NormalizeChannelInternalName(b.Name)
channelExisting, err := h.database.GetChannelByName(ctx, u.UserID, channelInternalName)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
user, err := h.database.GetUser(ctx, u.UserID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
if len(channelDisplayName) > user.MaxChannelNameLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
}
if len(strings.TrimSpace(channelDisplayName)) == 0 {
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil)
}
if len(channelInternalName) > user.MaxChannelNameLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
}
if len(strings.TrimSpace(channelInternalName)) == 0 {
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel internalname cannot be empty"), nil)
}
if channelExisting != nil {
return ginresp.APIError(g, 409, apierr.CHANNEL_ALREADY_EXISTS, "Channel with this name already exists", nil)
}
subscribeKey := h.app.GenerateRandomAuthKey()
channel, err := h.database.CreateChannel(ctx, u.UserID, channelDisplayName, channelInternalName, subscribeKey, b.Description)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create channel", err)
}
if langext.Coalesce(b.Subscribe, true) {
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, true)
if err != nil {
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))) if b.Name == "" {
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Missing parameter: name", nil)
}
} else { channelDisplayName := h.app.NormalizeChannelDisplayName(b.Name)
channelInternalName := h.app.NormalizeChannelInternalName(b.Name)
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true))) channelExisting, err := h.database.GetChannelByName(ctx, u.UserID, channelInternalName)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
} user, err := h.database.GetUser(ctx, u.UserID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
if len(channelDisplayName) > user.MaxChannelNameLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
}
if len(strings.TrimSpace(channelDisplayName)) == 0 {
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil)
}
if len(channelInternalName) > user.MaxChannelNameLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
}
if len(strings.TrimSpace(channelInternalName)) == 0 {
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel internalname cannot be empty"), nil)
}
if channelExisting != nil {
return ginresp.APIError(g, 409, apierr.CHANNEL_ALREADY_EXISTS, "Channel with this name already exists", nil)
}
subscribeKey := h.app.GenerateRandomAuthKey()
channel, err := h.database.CreateChannel(ctx, u.UserID, channelDisplayName, channelInternalName, subscribeKey, b.Description)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create channel", err)
}
if langext.Coalesce(b.Subscribe, true) {
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, true)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true)))
} else {
return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
}
})
} }
// UpdateChannel swaggerdoc // UpdateChannel swaggerdoc
@ -296,84 +302,88 @@ func (h APIHandler) UpdateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
user, err := h.database.GetUser(ctx, u.UserID) _, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil) return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
} }
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
if langext.Coalesce(b.RefreshSubscribeKey, false) {
newkey := h.app.GenerateRandomAuthKey()
err := h.database.UpdateChannelSubscribeKey(ctx, u.ChannelID, newkey)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
}
if b.DisplayName != nil {
newDisplayName := h.app.NormalizeChannelDisplayName(*b.DisplayName)
if len(newDisplayName) > user.MaxChannelNameLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
} }
if len(strings.TrimSpace(newDisplayName)) == 0 { user, err := h.database.GetUser(ctx, u.UserID)
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil) if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil)
} }
err := h.database.UpdateChannelDisplayName(ctx, u.ChannelID, newDisplayName)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
} }
} if langext.Coalesce(b.RefreshSubscribeKey, false) {
newkey := h.app.GenerateRandomAuthKey()
if b.DescriptionName != nil { err := h.database.UpdateChannelSubscribeKey(ctx, u.ChannelID, newkey)
if err != nil {
var descName *string = nil return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
if strings.TrimSpace(*b.DescriptionName) != "" { }
descName = langext.Ptr(strings.TrimSpace(*b.DescriptionName))
} }
if descName != nil && len(*descName) > user.MaxChannelDescriptionLength() { if b.DisplayName != nil {
return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelDescriptionLength()), nil)
newDisplayName := h.app.NormalizeChannelDisplayName(*b.DisplayName)
if len(newDisplayName) > user.MaxChannelNameLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
}
if len(strings.TrimSpace(newDisplayName)) == 0 {
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil)
}
err := h.database.UpdateChannelDisplayName(ctx, u.ChannelID, newDisplayName)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
}
} }
err := h.database.UpdateChannelDescriptionName(ctx, u.ChannelID, descName) if b.DescriptionName != nil {
var descName *string = nil
if strings.TrimSpace(*b.DescriptionName) != "" {
descName = langext.Ptr(strings.TrimSpace(*b.DescriptionName))
}
if descName != nil && len(*descName) > user.MaxChannelDescriptionLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelDescriptionLength()), nil)
}
err := h.database.UpdateChannelDescriptionName(ctx, u.ChannelID, descName)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
}
}
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err)
} }
} return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true) })
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
} }
// ListChannelMessages swaggerdoc // ListChannelMessages swaggerdoc
@ -416,50 +426,54 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo
var u uri var u uri
var q query 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
trimmed := langext.Coalesce(q.Trimmed, true) return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
maxPageSize := langext.Conditional(trimmed, 16, 256) trimmed := langext.Coalesce(q.Trimmed, true)
pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize) maxPageSize := langext.Conditional(trimmed, 16, 256)
channel, err := h.database.GetChannel(ctx, u.ChannelUserID, u.ChannelID, false) pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
if permResp := ctx.CheckPermissionChanMessagesRead(channel.Channel); permResp != nil { channel, err := h.database.GetChannel(ctx, u.ChannelUserID, u.ChannelID, false)
return *permResp if errors.Is(err, sql.ErrNoRows) {
} return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, "")) if permResp := ctx.CheckPermissionChanMessagesRead(channel.Channel); permResp != nil {
if err != nil { return *permResp
return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err) }
}
filter := models.MessageFilter{ tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, ""))
ChannelID: langext.Ptr([]models.ChannelID{channel.ChannelID}), if err != nil {
} return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
}
messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok) filter := models.MessageFilter{
if err != nil { ChannelID: langext.Ptr([]models.ChannelID{channel.ChannelID}),
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err) }
}
var res []models.MessageJSON messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok)
if trimmed { if err != nil {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() }) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
} else { }
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})) var res []models.MessageJSON
if trimmed {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() })
} else {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
}
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
})
} }

View File

@ -3,6 +3,7 @@ package handler
import ( import (
"blackforestbytes.com/simplecloudnotifier/api/apierr" "blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp" "blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"errors" "errors"
@ -34,24 +35,28 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
clients, err := h.database.ListClients(ctx, u.UserID) if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
if err != nil { return *permResp
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err) }
}
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() }) clients, err := h.database.ListClients(ctx, u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Clients: res})) res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
})
} }
// GetClient swaggerdoc // GetClient swaggerdoc
@ -77,25 +82,29 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID) if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON())) client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
} }
// AddClient swaggerdoc // AddClient swaggerdoc
@ -128,32 +137,36 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if !b.ClientType.Valid() { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
}
clientType := b.ClientType
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { if !b.ClientType.Valid() {
return *permResp return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
} }
clientType := b.ClientType
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if err != nil { return *permResp
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err) }
}
client, err := h.database.CreateClient(ctx, u.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.Name) err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
} }
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON())) client, err := h.database.CreateClient(ctx, u.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.Name)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
} }
// DeleteClient swaggerdoc // DeleteClient swaggerdoc
@ -179,30 +192,34 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
err = h.database.DeleteClient(ctx, u.ClientID) client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
if err != nil { if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err) return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
} }
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON())) err = h.database.DeleteClient(ctx, u.ClientID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
} }
// UpdateClient swaggerdoc // UpdateClient swaggerdoc
@ -239,69 +256,73 @@ func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
if b.FCMToken != nil && *b.FCMToken != client.FCMToken {
err = h.database.DeleteClientsByFCM(ctx, *b.FCMToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
} }
err = h.database.UpdateClientFCMToken(ctx, u.ClientID, *b.FCMToken) client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
if err != nil { if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err) return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
} }
}
if b.AgentModel != nil {
err = h.database.UpdateClientAgentModel(ctx, u.ClientID, *b.AgentModel)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
} }
}
if b.AgentVersion != nil { if b.FCMToken != nil && *b.FCMToken != client.FCMToken {
err = h.database.UpdateClientAgentVersion(ctx, u.ClientID, *b.AgentVersion)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
}
}
if b.Name != nil { err = h.database.DeleteClientsByFCM(ctx, *b.FCMToken)
if *b.Name == "" {
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, nil)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
} }
} else {
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, langext.Ptr(*b.Name)) err = h.database.UpdateClientFCMToken(ctx, u.ClientID, *b.FCMToken)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
} }
} }
}
client, err = h.database.GetClient(ctx, u.UserID, u.ClientID) if b.AgentModel != nil {
if err != nil { err = h.database.UpdateClientAgentModel(ctx, u.ClientID, *b.AgentModel)
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err) if err != nil {
} return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
}
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON())) if b.AgentVersion != nil {
err = h.database.UpdateClientAgentVersion(ctx, u.ClientID, *b.AgentVersion)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
}
}
if b.Name != nil {
if *b.Name == "" {
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, nil)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
}
} else {
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, langext.Ptr(*b.Name))
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
}
}
}
client, err = h.database.GetClient(ctx, u.UserID, u.ClientID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
} }

View File

@ -3,6 +3,7 @@ package handler
import ( import (
"blackforestbytes.com/simplecloudnotifier/api/apierr" "blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp" "blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"errors" "errors"
@ -36,24 +37,28 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
toks, err := h.database.ListKeyTokens(ctx, u.UserID) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if err != nil { return *permResp
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err) }
}
res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() }) toks, err := h.database.ListKeyTokens(ctx, u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Keys: res})) res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Keys: res}))
})
} }
// GetCurrentUserKey swaggerdoc // GetCurrentUserKey swaggerdoc
@ -79,30 +84,34 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionAny(); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
tokid := ctx.GetPermissionKeyTokenID() if permResp := ctx.CheckPermissionAny(); permResp != nil {
if tokid == nil { return *permResp
return ginresp.APIError(g, 400, apierr.USER_AUTH_FAILED, "Missing KeyTokenID in context", nil) }
}
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, *tokid) tokid := ctx.GetPermissionKeyTokenID()
if errors.Is(err, sql.ErrNoRows) { if tokid == nil {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err) return ginresp.APIError(g, 400, apierr.USER_AUTH_FAILED, "Missing KeyTokenID in context", nil)
} }
if err != nil {
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))) keytoken, err := h.database.GetKeyToken(ctx, u.UserID, *tokid)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
})
} }
// GetUserKey swaggerdoc // GetUserKey swaggerdoc
@ -129,25 +138,29 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON())) keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
})
} }
// UpdateUserKey swaggerdoc // UpdateUserKey swaggerdoc
@ -182,70 +195,74 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
if b.Name != nil { keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
err := h.database.UpdateKeyTokenName(ctx, u.KeyID, *b.Name) if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update name", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
keytoken.Name = *b.Name
}
if b.Permissions != nil {
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
} }
permlist := models.ParseTokenPermissionList(*b.Permissions) if b.Name != nil {
err := h.database.UpdateKeyTokenPermissions(ctx, u.KeyID, permlist) err := h.database.UpdateKeyTokenName(ctx, u.KeyID, *b.Name)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update permissions", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update name", err)
} }
keytoken.Permissions = permlist keytoken.Name = *b.Name
}
if b.AllChannels != nil {
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
} }
err := h.database.UpdateKeyTokenAllChannels(ctx, u.KeyID, *b.AllChannels) if b.Permissions != nil {
if err != nil { if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update all_channels", err) return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
} }
keytoken.AllChannels = *b.AllChannels
}
if b.Channels != nil { permlist := models.ParseTokenPermissionList(*b.Permissions)
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() { err := h.database.UpdateKeyTokenPermissions(ctx, u.KeyID, permlist)
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err) if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update permissions", err)
}
keytoken.Permissions = permlist
} }
err := h.database.UpdateKeyTokenChannels(ctx, u.KeyID, *b.Channels) if b.AllChannels != nil {
if err != nil { if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channels", err) return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
} }
keytoken.Channels = *b.Channels
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON())) err := h.database.UpdateKeyTokenAllChannels(ctx, u.KeyID, *b.AllChannels)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update all_channels", err)
}
keytoken.AllChannels = *b.AllChannels
}
if b.Channels != nil {
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
}
err := h.database.UpdateKeyTokenChannels(ctx, u.KeyID, *b.Channels)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channels", err)
}
keytoken.Channels = *b.Channels
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
})
} }
// CreateUserKey swaggerdoc // CreateUserKey swaggerdoc
@ -278,43 +295,47 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
channels := langext.Coalesce(b.Channels, make([]models.ChannelID, 0)) return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
var allChan bool channels := langext.Coalesce(b.Channels, make([]models.ChannelID, 0))
if b.AllChannels == nil && b.Channels != nil {
allChan = false
} else if b.AllChannels == nil && b.Channels == nil {
allChan = true
} else {
allChan = *b.AllChannels
}
for _, c := range channels { var allChan bool
if err := c.Valid(); err != nil { if b.AllChannels == nil && b.Channels != nil {
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Invalid ChannelID", err) allChan = false
} else if b.AllChannels == nil && b.Channels == nil {
allChan = true
} else {
allChan = *b.AllChannels
} }
}
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { for _, c := range channels {
return *permResp if err := c.Valid(); err != nil {
} return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Invalid ChannelID", err)
}
}
token := h.app.GenerateRandomAuthKey() if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
perms := models.ParseTokenPermissionList(b.Permissions) token := h.app.GenerateRandomAuthKey()
keytok, err := h.database.CreateKeyToken(ctx, b.Name, *ctx.GetPermissionUserID(), allChan, channels, perms, token) perms := models.ParseTokenPermissionList(b.Permissions)
if err != nil {
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))) keytok, err := h.database.CreateKeyToken(ctx, b.Name, *ctx.GetPermissionUserID(), allChan, channels, perms, token)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create keytoken in db", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytok.JSON().WithToken(token)))
})
} }
// DeleteUserKey swaggerdoc // DeleteUserKey swaggerdoc
@ -341,32 +362,36 @@ func (h APIHandler) DeleteUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
client, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
if u.KeyID == *ctx.GetPermissionKeyTokenID() { client, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
return ginresp.APIError(g, 400, apierr.CANNOT_SELFDELETE_KEY, "Cannot delete the currently used key", err) if errors.Is(err, sql.ErrNoRows) {
} return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
err = h.database.DeleteKeyToken(ctx, u.KeyID) if u.KeyID == *ctx.GetPermissionKeyTokenID() {
if err != nil { return ginresp.APIError(g, 400, apierr.CANNOT_SELFDELETE_KEY, "Cannot delete the currently used key", err)
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err) }
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON())) err = h.database.DeleteKeyToken(ctx, u.KeyID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
})
} }

View File

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"blackforestbytes.com/simplecloudnotifier/logic"
"database/sql" "database/sql"
"errors" "errors"
"gogs.mikescher.com/BlackForestBytes/goext/ginext" "gogs.mikescher.com/BlackForestBytes/goext/ginext"
@ -55,107 +56,111 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var q query var q query
ctx, errResp := h.app.StartRequest(g, nil, &q, nil, nil) ctx, g, errResp := pctx.Query(&q).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
trimmed := langext.Coalesce(q.Trimmed, true) return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
maxPageSize := langext.Conditional(trimmed, 16, 256) trimmed := langext.Coalesce(q.Trimmed, true)
pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize) maxPageSize := langext.Conditional(trimmed, 16, 256)
if permResp := ctx.CheckPermissionSelfAllMessagesRead(); permResp != nil { pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize)
return *permResp
}
userid := *ctx.GetPermissionUserID() if permResp := ctx.CheckPermissionSelfAllMessagesRead(); permResp != nil {
return *permResp
tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, ""))
if err != nil {
return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
}
err = h.database.UpdateUserLastRead(ctx, userid)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update last-read", err)
}
filter := models.MessageFilter{
ConfirmedSubscriptionBy: langext.Ptr(userid),
}
if q.Filter != nil && strings.TrimSpace(*q.Filter) != "" {
filter.SearchString = langext.Ptr([]string{strings.TrimSpace(*q.Filter)})
}
if len(q.Channels) != 0 {
filter.ChannelNameCS = langext.Ptr(q.Channels)
}
if len(q.ChannelIDs) != 0 {
cids := make([]models.ChannelID, 0, len(q.ChannelIDs))
for _, v := range q.ChannelIDs {
cid := models.ChannelID(v)
if err = cid.Valid(); err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid channel-id", err)
}
cids = append(cids, cid)
} }
filter.ChannelID = &cids
}
if len(q.Senders) != 0 { userid := *ctx.GetPermissionUserID()
filter.SenderNameCS = langext.Ptr(q.Senders)
}
if q.TimeBefore != nil { tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, ""))
t0, err := time.Parse(time.RFC3339, *q.TimeBefore)
if err != nil { if err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid before-time", err) return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
} }
filter.TimestampCoalesceBefore = &t0
}
if q.TimeAfter != nil { err = h.database.UpdateUserLastRead(ctx, userid)
t0, err := time.Parse(time.RFC3339, *q.TimeAfter)
if err != nil { if err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid after-time", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update last-read", err)
} }
filter.TimestampCoalesceAfter = &t0
}
if len(q.Priority) != 0 { filter := models.MessageFilter{
filter.Priority = langext.Ptr(q.Priority) ConfirmedSubscriptionBy: langext.Ptr(userid),
} }
if len(q.KeyTokens) != 0 { if q.Filter != nil && strings.TrimSpace(*q.Filter) != "" {
tids := make([]models.KeyTokenID, 0, len(q.KeyTokens)) filter.SearchString = langext.Ptr([]string{strings.TrimSpace(*q.Filter)})
for _, v := range q.KeyTokens { }
tid := models.KeyTokenID(v)
if err = tid.Valid(); err != nil { if len(q.Channels) != 0 {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid keytoken-id", err) filter.ChannelNameCS = langext.Ptr(q.Channels)
}
if len(q.ChannelIDs) != 0 {
cids := make([]models.ChannelID, 0, len(q.ChannelIDs))
for _, v := range q.ChannelIDs {
cid := models.ChannelID(v)
if err = cid.Valid(); err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid channel-id", err)
}
cids = append(cids, cid)
} }
tids = append(tids, tid) filter.ChannelID = &cids
} }
filter.UsedKeyID = &tids
}
messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok) if len(q.Senders) != 0 {
if err != nil { filter.SenderNameCS = langext.Ptr(q.Senders)
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err) }
}
var res []models.MessageJSON if q.TimeBefore != nil {
if trimmed { t0, err := time.Parse(time.RFC3339, *q.TimeBefore)
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() }) if err != nil {
} else { return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid before-time", err)
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() }) }
} filter.TimestampCoalesceBefore = &t0
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize})) if q.TimeAfter != nil {
t0, err := time.Parse(time.RFC3339, *q.TimeAfter)
if err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid after-time", err)
}
filter.TimestampCoalesceAfter = &t0
}
if len(q.Priority) != 0 {
filter.Priority = langext.Ptr(q.Priority)
}
if len(q.KeyTokens) != 0 {
tids := make([]models.KeyTokenID, 0, len(q.KeyTokens))
for _, v := range q.KeyTokens {
tid := models.KeyTokenID(v)
if err = tid.Valid(); err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid keytoken-id", err)
}
tids = append(tids, tid)
}
filter.UsedKeyID = &tids
}
messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
}
var res []models.MessageJSON
if trimmed {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() })
} else {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
}
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
})
} }
// GetMessage swaggerdoc // GetMessage swaggerdoc
@ -182,50 +187,54 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionAny(); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
msg, err := h.database.GetMessage(ctx, u.MessageID, false) if permResp := ctx.CheckPermissionAny(); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
}
// either we have direct read permissions (it is our message + read/admin key) msg, err := h.database.GetMessage(ctx, u.MessageID, false)
// or we subscribe (+confirmed) to the channel and have read/admin key if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
if ctx.CheckPermissionMessageRead(msg) { }
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
}
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil {
sub, err := h.database.GetSubscriptionBySubscriber(ctx, *uid, msg.ChannelID)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
}
if sub == nil {
// not subbed
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
}
if !sub.Confirmed {
// sub not confirmed
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
} }
// => perm okay // either we have direct read permissions (it is our message + read/admin key)
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON())) // or we subscribe (+confirmed) to the channel and have read/admin key
}
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil) if ctx.CheckPermissionMessageRead(msg) {
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
}
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil {
sub, err := h.database.GetSubscriptionBySubscriber(ctx, *uid, msg.ChannelID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
if sub == nil {
// not subbed
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
}
if !sub.Confirmed {
// sub not confirmed
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
}
// => perm okay
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 // DeleteMessage swaggerdoc
@ -250,37 +259,41 @@ func (h APIHandler) DeleteMessage(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionAny(); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
msg, err := h.database.GetMessage(ctx, u.MessageID, false) if permResp := ctx.CheckPermissionAny(); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
}
if !ctx.CheckPermissionMessageDelete(msg) { msg, err := h.database.GetMessage(ctx, u.MessageID, false)
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil) if errors.Is(err, sql.ErrNoRows) {
} return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
}
err = h.database.DeleteMessage(ctx, msg.MessageID) if !ctx.CheckPermissionMessageDelete(msg) {
if err != nil { return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete message", err) }
}
err = h.database.CancelPendingDeliveries(ctx, msg.MessageID) err = h.database.DeleteMessage(ctx, msg.MessageID)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete message", err)
} }
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON())) err = h.database.CancelPendingDeliveries(ctx, msg.MessageID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
})
} }

View File

@ -3,6 +3,7 @@ package handler
import ( import (
"blackforestbytes.com/simplecloudnotifier/api/apierr" "blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp" "blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"errors" "errors"
@ -31,25 +32,29 @@ func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionAny(); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
user, err := h.database.GetUser(ctx, u.UserID) if permResp := ctx.CheckPermissionAny(); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview())) user, err := h.database.GetUser(ctx, u.UserID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview()))
})
} }
// GetChannelPreview swaggerdoc // GetChannelPreview swaggerdoc
@ -73,25 +78,29 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionAny(); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
channel, err := h.database.GetChannelByID(ctx, u.ChannelID) if permResp := ctx.CheckPermissionAny(); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview())) channel, err := h.database.GetChannelByID(ctx, u.ChannelID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview()))
})
} }
// GetUserKeyPreview swaggerdoc // GetUserKeyPreview swaggerdoc
@ -115,23 +124,27 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionAny(); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
keytoken, err := h.database.GetKeyTokenByID(ctx, u.KeyID) if permResp := ctx.CheckPermissionAny(); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview())) keytoken, err := h.database.GetKeyTokenByID(ctx, u.KeyID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview()))
})
} }

View File

@ -3,6 +3,7 @@ package handler
import ( import (
"blackforestbytes.com/simplecloudnotifier/api/apierr" "blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp" "blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"errors" "errors"
@ -64,71 +65,75 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes
var u uri var u uri
var q query 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
filter := models.SubscriptionFilter{} if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
filter.AnyUserID = langext.Ptr(u.UserID) return *permResp
if q.Direction != nil {
if strings.EqualFold(*q.Direction, "incoming") {
filter.ChannelOwnerUserID = langext.Ptr([]models.UserID{u.UserID})
} else if strings.EqualFold(*q.Direction, "outgoing") {
filter.SubscriberUserID = langext.Ptr([]models.UserID{u.UserID})
} else if strings.EqualFold(*q.Direction, "both") {
// both
} else {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'direction'", nil)
} }
}
if q.Confirmation != nil { filter := models.SubscriptionFilter{}
if strings.EqualFold(*q.Confirmation, "confirmed") { filter.AnyUserID = langext.Ptr(u.UserID)
filter.Confirmed = langext.PTrue
} else if strings.EqualFold(*q.Confirmation, "unconfirmed") { if q.Direction != nil {
filter.Confirmed = langext.PFalse if strings.EqualFold(*q.Direction, "incoming") {
} else if strings.EqualFold(*q.Confirmation, "all") { filter.ChannelOwnerUserID = langext.Ptr([]models.UserID{u.UserID})
// both } else if strings.EqualFold(*q.Direction, "outgoing") {
} else { filter.SubscriberUserID = langext.Ptr([]models.UserID{u.UserID})
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'confirmation'", nil) } else if strings.EqualFold(*q.Direction, "both") {
// both
} else {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'direction'", nil)
}
} }
}
if q.External != nil { if q.Confirmation != nil {
if strings.EqualFold(*q.External, "true") { if strings.EqualFold(*q.Confirmation, "confirmed") {
filter.SubscriberIsChannelOwner = langext.PFalse filter.Confirmed = langext.PTrue
} else if strings.EqualFold(*q.External, "false") { } else if strings.EqualFold(*q.Confirmation, "unconfirmed") {
filter.SubscriberIsChannelOwner = langext.PTrue filter.Confirmed = langext.PFalse
} else if strings.EqualFold(*q.External, "all") { } else if strings.EqualFold(*q.Confirmation, "all") {
// both // both
} else { } else {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'external'", nil) return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'confirmation'", nil)
}
} }
}
if q.SubscriberUserID != nil { if q.External != nil {
filter.SubscriberUserID2 = langext.Ptr([]models.UserID{*q.SubscriberUserID}) if strings.EqualFold(*q.External, "true") {
} filter.SubscriberIsChannelOwner = langext.PFalse
} else if strings.EqualFold(*q.External, "false") {
filter.SubscriberIsChannelOwner = langext.PTrue
} else if strings.EqualFold(*q.External, "all") {
// both
} else {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'external'", nil)
}
}
if q.ChannelOwnerUserID != nil { if q.SubscriberUserID != nil {
filter.ChannelOwnerUserID2 = langext.Ptr([]models.UserID{*q.ChannelOwnerUserID}) filter.SubscriberUserID2 = langext.Ptr([]models.UserID{*q.SubscriberUserID})
} }
res, err := h.database.ListSubscriptions(ctx, filter) if q.ChannelOwnerUserID != nil {
if err != nil { filter.ChannelOwnerUserID2 = langext.Ptr([]models.UserID{*q.ChannelOwnerUserID})
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err) }
}
jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() }) res, err := h.database.ListSubscriptions(ctx, filter)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres})) jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres}))
})
} }
// ListChannelSubscriptions swaggerdoc // ListChannelSubscriptions swaggerdoc
@ -157,32 +162,36 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true) if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
clients, err := h.database.ListSubscriptions(ctx, models.SubscriptionFilter{AnyUserID: langext.Ptr(u.UserID), ChannelID: langext.Ptr([]models.ChannelID{u.ChannelID})}) _, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
if err != nil { if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err) return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
} }
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() }) clients, err := h.database.ListSubscriptions(ctx, models.SubscriptionFilter{AnyUserID: langext.Ptr(u.UserID), ChannelID: langext.Ptr([]models.ChannelID{u.ChannelID})})
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res})) res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
})
} }
// GetSubscription swaggerdoc // GetSubscription swaggerdoc
@ -208,28 +217,32 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID) if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON())) subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
})
} }
// CancelSubscription swaggerdoc // CancelSubscription swaggerdoc
@ -255,33 +268,37 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID) if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err) }
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
err = h.database.DeleteSubscription(ctx, u.SubscriptionID) subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
if err != nil { if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err) return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
} }
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON())) err = h.database.DeleteSubscription(ctx, u.SubscriptionID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
})
} }
// CreateSubscription swaggerdoc // CreateSubscription swaggerdoc
@ -317,76 +334,80 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
var u uri var u uri
var q query var q query
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
var channel models.Channel if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
if b.ChannelOwnerUserID != nil && b.ChannelInternalName != nil && b.ChannelID == nil {
channelInternalName := h.app.NormalizeChannelInternalName(*b.ChannelInternalName)
outchannel, err := h.database.GetChannelByName(ctx, *b.ChannelOwnerUserID, channelInternalName)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
if outchannel == nil {
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
} }
channel = *outchannel var channel models.Channel
} else if b.ChannelOwnerUserID == nil && b.ChannelInternalName == nil && b.ChannelID != nil { if b.ChannelOwnerUserID != nil && b.ChannelInternalName != nil && b.ChannelID == nil {
outchannel, err := h.database.GetChannelByID(ctx, *b.ChannelID) channelInternalName := h.app.NormalizeChannelInternalName(*b.ChannelInternalName)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
if outchannel == nil {
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
channel = *outchannel outchannel, err := h.database.GetChannelByName(ctx, *b.ChannelOwnerUserID, channelInternalName)
} else {
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Must either supply [channel_owner_user_id, channel_internal_name] or [channel_id]", nil)
}
if channel.OwnerUserID != u.UserID && (q.ChanSubscribeKey == nil || *q.ChanSubscribeKey != channel.SubscribeKey) {
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
}
existingSub, err := h.database.GetSubscriptionBySubscriber(ctx, u.UserID, channel.ChannelID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query existing subscription", err)
}
if existingSub != nil {
if !existingSub.Confirmed && channel.OwnerUserID == u.UserID {
err = h.database.UpdateSubscriptionConfirmed(ctx, existingSub.SubscriptionID, true)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
} }
existingSub.Confirmed = true if outchannel == nil {
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
channel = *outchannel
} else if b.ChannelOwnerUserID == nil && b.ChannelInternalName == nil && b.ChannelID != nil {
outchannel, err := h.database.GetChannelByID(ctx, *b.ChannelID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
if outchannel == nil {
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
channel = *outchannel
} else {
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Must either supply [channel_owner_user_id, channel_internal_name] or [channel_id]", nil)
} }
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON())) if channel.OwnerUserID != u.UserID && (q.ChanSubscribeKey == nil || *q.ChanSubscribeKey != channel.SubscribeKey) {
} return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
}
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID) existingSub, err := h.database.GetSubscriptionBySubscriber(ctx, u.UserID, channel.ChannelID)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query existing subscription", err)
} }
if existingSub != nil {
if !existingSub.Confirmed && channel.OwnerUserID == u.UserID {
err = h.database.UpdateSubscriptionConfirmed(ctx, existingSub.SubscriptionID, true)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
}
existingSub.Confirmed = true
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, sub.JSON())) return finishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON()))
}
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, sub.JSON()))
})
} }
// UpdateSubscription swaggerdoc // UpdateSubscription swaggerdoc
@ -417,43 +438,47 @@ func (h APIHandler) UpdateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
userid := *ctx.GetPermissionUserID() if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID) }
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err) userid := *ctx.GetPermissionUserID()
}
if err != nil { subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err) if errors.Is(err, sql.ErrNoRows) {
} return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
if b.Confirmed != nil {
if subscription.ChannelOwnerUserID != userid {
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
} }
err = h.database.UpdateSubscriptionConfirmed(ctx, u.SubscriptionID, *b.Confirmed)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
} }
}
subscription, err = h.database.GetSubscription(ctx, u.SubscriptionID) if b.Confirmed != nil {
if err != nil { if subscription.ChannelOwnerUserID != userid {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err) return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
} }
err = h.database.UpdateSubscriptionConfirmed(ctx, u.SubscriptionID, *b.Confirmed)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
}
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON())) subscription, err = h.database.GetSubscription(ctx, u.SubscriptionID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
})
} }

View File

@ -3,6 +3,7 @@ package handler
import ( import (
"blackforestbytes.com/simplecloudnotifier/api/apierr" "blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp" "blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"errors" "errors"
@ -39,99 +40,101 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var b body var b body
ctx, g, errResp := h.app.StartRequest(pctx.Body(&b).Start()) ctx, g, errResp := pctx.Body(&b).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
var clientType models.ClientType return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if !b.NoClient {
if b.FCMToken == "" {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing FCMToken", nil)
}
if b.AgentVersion == "" {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing AgentVersion", nil)
}
if b.ClientType == "" {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing ClientType", nil)
}
if !b.ClientType.Valid() {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Invalid ClientType", nil)
}
clientType = b.ClientType
}
if b.ProToken != nil { var clientType models.ClientType
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken) if !b.NoClient {
if b.FCMToken == "" {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing FCMToken", nil)
}
if b.AgentVersion == "" {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing AgentVersion", nil)
}
if b.ClientType == "" {
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing ClientType", nil)
}
if !b.ClientType.Valid() {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Invalid ClientType", nil)
}
clientType = b.ClientType
}
if b.ProToken != nil {
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
}
if !ptok {
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
}
}
readKey := h.app.GenerateRandomAuthKey()
sendKey := h.app.GenerateRandomAuthKey()
adminKey := h.app.GenerateRandomAuthKey()
err := h.database.ClearFCMTokens(ctx, b.FCMToken)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
} }
if !ptok { if b.ProToken != nil {
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil) err := h.database.ClearProTokens(ctx, *b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing pro tokens", err)
}
} }
}
readKey := h.app.GenerateRandomAuthKey() username := b.Username
sendKey := h.app.GenerateRandomAuthKey() if username != nil {
adminKey := h.app.GenerateRandomAuthKey() username = langext.Ptr(h.app.NormalizeUsername(*username))
}
err := h.database.ClearFCMTokens(ctx, b.FCMToken) userobj, err := h.database.CreateUser(ctx, b.ProToken, username)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
}
if b.ProToken != nil {
err := h.database.ClearProTokens(ctx, *b.ProToken)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing pro tokens", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
} }
}
username := b.Username _, err = h.database.CreateKeyToken(ctx, "AdminKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermAdmin}, adminKey)
if username != nil {
username = langext.Ptr(h.app.NormalizeUsername(*username))
}
userobj, err := h.database.CreateUser(ctx, b.ProToken, username)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
}
_, err = h.database.CreateKeyToken(ctx, "AdminKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermAdmin}, adminKey)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create admin-key in db", err)
}
_, err = h.database.CreateKeyToken(ctx, "SendKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermChannelSend}, sendKey)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create send-key in db", err)
}
_, err = h.database.CreateKeyToken(ctx, "ReadKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermUserRead, models.PermChannelRead}, readKey)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create read-key in db", err)
}
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)))
} else {
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create admin-key in db", err)
} }
client, err := h.database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.ClientName) _, err = h.database.CreateKeyToken(ctx, "SendKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermChannelSend}, sendKey)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create send-key in db", err)
} }
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey))) _, err = h.database.CreateKeyToken(ctx, "ReadKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermUserRead, models.PermChannelRead}, readKey)
} if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create read-key in db", err)
}
log.Info().Msg(fmt.Sprintf("Sucessfully created new user %s (client: %v)", userobj.UserID, b.NoClient))
if b.NoClient {
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 {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
}
client, err := h.database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.ClientName)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey)))
}
})
} }
// GetUser swaggerdoc // GetUser swaggerdoc
@ -155,25 +158,30 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
} }
var u uri var u uri
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start()) ctx, g, errResp := pctx.URI(&u).Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
user, err := h.database.GetUser(ctx, u.UserID) if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
if errors.Is(err, sql.ErrNoRows) { return *permResp
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err) }
}
if err != nil { user, err := h.database.GetUser(ctx, u.UserID)
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err) if errors.Is(err, sql.ErrNoRows) {
} return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
})
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
} }
// UpdateUser swaggerdoc // UpdateUser swaggerdoc
@ -206,60 +214,63 @@ func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse {
var u uri var u uri
var b body 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
return *permResp
}
if b.Username != nil { if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
username := langext.Ptr(h.app.NormalizeUsername(*b.Username)) return *permResp
if *username == "" {
username = nil
} }
err := h.database.UpdateUserUsername(ctx, u.UserID, username) if b.Username != nil {
username := langext.Ptr(h.app.NormalizeUsername(*b.Username))
if *username == "" {
username = nil
}
err := h.database.UpdateUserUsername(ctx, u.UserID, username)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
}
}
if b.ProToken != nil {
if *b.ProToken == "" {
err := h.database.UpdateUserProToken(ctx, u.UserID, nil)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
}
} else {
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
}
if !ptok {
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
}
err = h.database.ClearProTokens(ctx, *b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
}
err = h.database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
}
}
}
user, err := h.database.GetUser(ctx, u.UserID)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
} }
}
if b.ProToken != nil { return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
if *b.ProToken == "" { })
err := h.database.UpdateUserProToken(ctx, u.UserID, nil)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
}
} else {
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
}
if !ptok {
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
}
err = h.database.ClearProTokens(ctx, *b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
}
err = h.database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
}
}
}
user, err := h.database.GetUser(ctx, u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
} }

View File

@ -58,19 +58,23 @@ func (h CommonHandler) Ping(pctx ginext.PreContext) ginext.HTTPResponse {
} }
defer ctx.Cancel() defer ctx.Cancel()
buf := new(bytes.Buffer) return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
_, _ = buf.ReadFrom(g.Request.Body)
resuestBody := buf.String() buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(g.Request.Body)
resuestBody := buf.String()
return ginext.JSON(http.StatusOK, pingResponse{
Message: "Pong",
Info: pingResponseInfo{
Method: g.Request.Method,
Request: resuestBody,
Headers: g.Request.Header,
URI: g.Request.RequestURI,
Address: g.Request.RemoteAddr,
},
})
return ginext.JSON(http.StatusOK, pingResponse{
Message: "Pong",
Info: pingResponseInfo{
Method: g.Request.Method,
Request: resuestBody,
Headers: g.Request.Header,
URI: g.Request.RequestURI,
Address: g.Request.RemoteAddr,
},
}) })
} }
@ -92,24 +96,28 @@ func (h CommonHandler) DatabaseTest(pctx ginext.PreContext) ginext.HTTPResponse
SourceID string `json:"sourceID"` SourceID string `json:"sourceID"`
} }
ctx, _, errResp := pctx.Start() ctx, g, errResp := pctx.Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
libVersion, libVersionNumber, sourceID := sqlite3.Version() return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
err := h.app.Database.Ping(ctx) libVersion, libVersionNumber, sourceID := sqlite3.Version()
if err != nil {
return ginresp.InternalError(err) err := h.app.Database.Ping(ctx)
} if err != nil {
return ginresp.InternalError(err)
}
return ginext.JSON(http.StatusOK, response{
Success: true,
LibVersion: libVersion,
LibVersionNumber: libVersionNumber,
SourceID: sourceID,
})
return ginext.JSON(http.StatusOK, response{
Success: true,
LibVersion: libVersion,
LibVersionNumber: libVersionNumber,
SourceID: sourceID,
}) })
} }
@ -128,52 +136,56 @@ func (h CommonHandler) Health(pctx ginext.PreContext) ginext.HTTPResponse {
Status string `json:"status"` Status string `json:"status"`
} }
ctx, _, errResp := pctx.Start() ctx, g, errResp := pctx.Start()
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
_, libVersionNumber, _ := sqlite3.Version() return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
if libVersionNumber < 3039000 { _, libVersionNumber, _ := sqlite3.Version()
return ginresp.InternalError(errors.New("sqlite version too low"))
}
tctx := simplectx.CreateSimpleContext(ctx, nil) if libVersionNumber < 3039000 {
return ginresp.InternalError(errors.New("sqlite version too low"))
}
err := h.app.Database.Ping(tctx) tctx := simplectx.CreateSimpleContext(ctx, nil)
if err != nil {
return ginresp.InternalError(err)
}
for _, subdb := range h.app.Database.List() { err := h.app.Database.Ping(tctx)
uuidKey, _ := langext.NewHexUUID()
uuidWrite, _ := langext.NewHexUUID()
err = subdb.WriteMetaString(tctx, uuidKey, uuidWrite)
if err != nil { if err != nil {
return ginresp.InternalError(err) return ginresp.InternalError(err)
} }
uuidRead, err := subdb.ReadMetaString(tctx, uuidKey) for _, subdb := range h.app.Database.List() {
if err != nil {
return ginresp.InternalError(err) uuidKey, _ := langext.NewHexUUID()
uuidWrite, _ := langext.NewHexUUID()
err = subdb.WriteMetaString(tctx, uuidKey, uuidWrite)
if err != nil {
return ginresp.InternalError(err)
}
uuidRead, err := subdb.ReadMetaString(tctx, uuidKey)
if err != nil {
return ginresp.InternalError(err)
}
if uuidRead == nil || uuidWrite != *uuidRead {
return ginresp.InternalError(errors.New("writing into DB was not consistent"))
}
err = subdb.DeleteMeta(tctx, uuidKey)
if err != nil {
return ginresp.InternalError(err)
}
} }
if uuidRead == nil || uuidWrite != *uuidRead { return ginext.JSON(http.StatusOK, response{Status: "ok"})
return ginresp.InternalError(errors.New("writing into DB was not consistent"))
}
err = subdb.DeleteMeta(tctx, uuidKey) })
if err != nil {
return ginresp.InternalError(err)
}
}
return ginext.JSON(http.StatusOK, response{Status: "ok"})
} }
// Sleep swaggerdoc // Sleep swaggerdoc
@ -205,21 +217,25 @@ func (h CommonHandler) Sleep(pctx ginext.PreContext) ginext.HTTPResponse {
} }
defer ctx.Cancel() defer ctx.Cancel()
t0 := time.Now().Format(time.RFC3339Nano) return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
var u uri t0 := time.Now().Format(time.RFC3339Nano)
if err := g.ShouldBindUri(&u); err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_URI_PARAM, "Failed to read uri", err)
}
time.Sleep(timeext.FromSeconds(u.Seconds)) var u uri
if err := g.ShouldBindUri(&u); err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_URI_PARAM, "Failed to read uri", err)
}
t1 := time.Now().Format(time.RFC3339Nano) time.Sleep(timeext.FromSeconds(u.Seconds))
t1 := time.Now().Format(time.RFC3339Nano)
return ginext.JSON(http.StatusOK, response{
Start: t0,
End: t1,
Duration: u.Seconds,
})
return ginext.JSON(http.StatusOK, response{
Start: t0,
End: t1,
Duration: u.Seconds,
}) })
} }
@ -230,14 +246,18 @@ func (h CommonHandler) NoRoute(pctx ginext.PreContext) ginext.HTTPResponse {
} }
defer ctx.Cancel() defer ctx.Cancel()
return ginext.JSON(http.StatusNotFound, gin.H{ return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
"": "================ ROUTE NOT FOUND ================",
"FullPath": g.FullPath(), return ginext.JSON(http.StatusNotFound, gin.H{
"Method": g.Request.Method, "": "================ ROUTE NOT FOUND ================",
"URL": g.Request.URL.String(), "FullPath": g.FullPath(),
"RequestURI": g.Request.RequestURI, "Method": g.Request.Method,
"Proto": g.Request.Proto, "URL": g.Request.URL.String(),
"Header": g.Request.Header, "RequestURI": g.Request.RequestURI,
"~": "================ ROUTE NOT FOUND ================", "Proto": g.Request.Proto,
"Header": g.Request.Header,
"~": "================ ROUTE NOT FOUND ================",
})
}) })
} }

File diff suppressed because it is too large Load Diff

View File

@ -74,61 +74,65 @@ func (h ExternalHandler) UptimeKuma(pctx ginext.PreContext) ginext.HTTPResponse
var b body var b body
var q query var q query
ctx, httpErr := h.app.StartRequest(g, nil, &q, &b, nil) ctx, g, errResp := pctx.Query(&q).Body(&b).Start()
if httpErr != nil {
return *httpErr
}
defer ctx.Cancel()
if b.Heartbeat == nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'heartbeat' in request body", nil)
}
if b.Monitor == nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'monitor' in request body", nil)
}
if b.Msg == nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'msg' in request body", nil)
}
title := langext.Conditional(b.Heartbeat.Status == 1, fmt.Sprintf("Monitor %v is back online", b.Monitor.Name), fmt.Sprintf("Monitor %v went down!", b.Monitor.Name))
content := b.Heartbeat.Msg
var timestamp *float64 = nil
if tz, err := time.LoadLocation(b.Heartbeat.Timezone); err == nil {
if ts, err := time.ParseInLocation("2006-01-02 15:04:05", b.Heartbeat.LocalDateTime, tz); err == nil {
timestamp = langext.Ptr(float64(ts.Unix()))
}
}
var channel *string = nil
if q.Channel != nil {
channel = q.Channel
}
if q.ChannelUp != nil && b.Heartbeat.Status == 1 {
channel = q.ChannelUp
}
if q.ChannelDown != nil && b.Heartbeat.Status != 1 {
channel = q.ChannelDown
}
var priority *int = nil
if q.Priority != nil {
priority = q.Priority
}
if q.PriorityUp != nil && b.Heartbeat.Status == 1 {
priority = q.PriorityUp
}
if q.PriorityDown != nil && b.Heartbeat.Status != 1 {
priority = q.PriorityDown
}
okResp, errResp := h.app.SendMessage(g, ctx, q.UserID, q.KeyToken, channel, &title, &content, priority, nil, timestamp, q.SenderName)
if errResp != nil { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel()
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{ return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
MessageID: okResp.Message.MessageID,
})) if b.Heartbeat == nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'heartbeat' in request body", nil)
}
if b.Monitor == nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'monitor' in request body", nil)
}
if b.Msg == nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'msg' in request body", nil)
}
title := langext.Conditional(b.Heartbeat.Status == 1, fmt.Sprintf("Monitor %v is back online", b.Monitor.Name), fmt.Sprintf("Monitor %v went down!", b.Monitor.Name))
content := b.Heartbeat.Msg
var timestamp *float64 = nil
if tz, err := time.LoadLocation(b.Heartbeat.Timezone); err == nil {
if ts, err := time.ParseInLocation("2006-01-02 15:04:05", b.Heartbeat.LocalDateTime, tz); err == nil {
timestamp = langext.Ptr(float64(ts.Unix()))
}
}
var channel *string = nil
if q.Channel != nil {
channel = q.Channel
}
if q.ChannelUp != nil && b.Heartbeat.Status == 1 {
channel = q.ChannelUp
}
if q.ChannelDown != nil && b.Heartbeat.Status != 1 {
channel = q.ChannelDown
}
var priority *int = nil
if q.Priority != nil {
priority = q.Priority
}
if q.PriorityUp != nil && b.Heartbeat.Status == 1 {
priority = q.PriorityUp
}
if q.PriorityDown != nil && b.Heartbeat.Status != 1 {
priority = q.PriorityDown
}
okResp, errResp := h.app.SendMessage(g, ctx, q.UserID, q.KeyToken, channel, &title, &content, priority, nil, timestamp, q.SenderName)
if errResp != nil {
return *errResp
}
return finishSuccess(ginext.JSON(http.StatusOK, response{
MessageID: okResp.Message.MessageID,
}))
})
} }

View File

@ -77,30 +77,34 @@ func (h MessageHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse
var b combined var b combined
var q combined var q combined
var f 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 { if errResp != nil {
return *errResp return *errResp
} }
defer ctx.Cancel() defer ctx.Cancel()
// query has highest prio, then form, then json return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
data := dataext.ObjectMerge(dataext.ObjectMerge(b, f), q)
okResp, errResp := h.app.SendMessage(g, ctx, data.UserID, data.KeyToken, data.Channel, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, data.SenderName) // query has highest prio, then form, then json
if errResp != nil { data := dataext.ObjectMerge(dataext.ObjectMerge(b, f), q)
return *errResp
} else { okResp, errResp := h.app.SendMessage(g, ctx, data.UserID, data.KeyToken, data.Channel, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, data.SenderName)
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{ if errResp != nil {
Success: true, return *errResp
ErrorID: apierr.NO_ERROR, } else {
ErrorHighlight: -1, return finishSuccess(ginext.JSON(http.StatusOK, response{
Message: langext.Conditional(okResp.MessageIsOld, "Message already sent", "Message sent"), Success: true,
SuppressSend: okResp.MessageIsOld, ErrorID: apierr.NO_ERROR,
MessageCount: okResp.User.MessagesSent, ErrorHighlight: -1,
Quota: okResp.User.QuotaUsedToday(), Message: langext.Conditional(okResp.MessageIsOld, "Message already sent", "Message sent"),
IsPro: okResp.User.IsPro, SuppressSend: okResp.MessageIsOld,
QuotaMax: okResp.User.QuotaPerDay(), MessageCount: okResp.User.MessagesSent,
SCNMessageID: okResp.Message.MessageID, Quota: okResp.User.QuotaUsedToday(),
})) IsPro: okResp.User.IsPro,
} QuotaMax: okResp.User.QuotaPerDay(),
SCNMessageID: okResp.Message.MessageID,
}))
}
})
} }

View File

@ -35,7 +35,9 @@ func (h WebsiteHandler) Index(pctx ginext.PreContext) ginext.HTTPResponse {
} }
defer ctx.Cancel() defer ctx.Cancel()
return h.serveAsset(g, "index.html", true) 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 { 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() defer ctx.Cancel()
return h.serveAsset(g, "api.html", true) 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 { 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() defer ctx.Cancel()
return h.serveAsset(g, "api_more.html", true) 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 { 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() defer ctx.Cancel()
return h.serveAsset(g, "message_sent.html", true) 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 { 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() defer ctx.Cancel()
return h.serveAsset(g, "favicon.ico", false) 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 { 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() defer ctx.Cancel()
return h.serveAsset(g, "favicon.png", false) 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 { func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
@ -95,16 +107,19 @@ func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
} }
defer ctx.Cancel() defer ctx.Cancel()
type uri struct { return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
Filename string `uri:"fn"`
}
var u uri type uri struct {
if err := g.ShouldBindUri(&u); err != nil { Filename string `uri:"fn"`
return ginext.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) }
}
return h.serveAsset(g, "js/"+u.Filename, false) var u uri
if err := g.ShouldBindUri(&u); err != nil {
return ginext.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
return h.serveAsset(g, "js/"+u.Filename, false)
})
} }
func (h WebsiteHandler) CSS(pctx ginext.PreContext) ginext.HTTPResponse { 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() defer ctx.Cancel()
return h.serveAsset(g, "css/"+u.Filename, false) 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 { func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginext.HTTPResponse {

View File

@ -1,13 +1,11 @@
package api package api
import ( import (
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/api/handler" "blackforestbytes.com/simplecloudnotifier/api/handler"
"blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"blackforestbytes.com/simplecloudnotifier/swagger" "blackforestbytes.com/simplecloudnotifier/swagger"
"errors" "errors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"gogs.mikescher.com/BlackForestBytes/goext/ginext" "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") return errors.New("failed to add validators - wrong engine")
} }
wrap := func(fn ginext.WHandlerFunc) ginext.WHandlerFunc{return Wrap(r.app, fn)}
// ================ General (unversioned) ================ // ================ General (unversioned) ================
commonAPI := e.Routes().Group("/api") commonAPI := e.Routes().Group("/api")
{ {
commonAPI.Any("/ping").Handle(wrap(r.commonHandler.Ping)) commonAPI.Any("/ping").Handle(r.commonHandler.Ping)
commonAPI.POST("/db-test").Handle(wrap(r.commonHandler.DatabaseTest)) commonAPI.POST("/db-test").Handle(r.commonHandler.DatabaseTest)
commonAPI.GET("/health").Handle(wrap(r.commonHandler.Health)) commonAPI.GET("/health").Handle(r.commonHandler.Health)
commonAPI.POST("/sleep/:secs").Handle(wrap(r.commonHandler.Sleep)) commonAPI.POST("/sleep/:secs").Handle(r.commonHandler.Sleep)
} }
// ================ Swagger ================ // ================ Swagger ================
docs := e.Routes().Group("/documentation") docs := e.Routes().Group("/documentation")
{ {
docs.GET("/swagger").Handle(wrap(ginext.RedirectTemporary("/documentation/swagger/"))) docs.GET("/swagger").Handle(ginext.RedirectTemporary("/documentation/swagger/"))
docs.GET("/swagger/*sub").Handle(wrap(swagger.Handle)) docs.GET("/swagger/*sub").Handle(swagger.Handle)
} }
// ================ Website ================ // ================ Website ================
frontend := e.Routes().Group("") frontend := e.Routes().Group("")
{ {
frontend.GET("/").Handle(wrap(r.websiteHandler.Index)) frontend.GET("/").Handle(r.websiteHandler.Index)
frontend.GET("/index.php").Handle(wrap(r.websiteHandler.Index)) frontend.GET("/index.php").Handle(r.websiteHandler.Index)
frontend.GET("/index.html").Handle(wrap(r.websiteHandler.Index)) frontend.GET("/index.html").Handle(r.websiteHandler.Index)
frontend.GET("/index").Handle(wrap(r.websiteHandler.Index)) frontend.GET("/index").Handle(r.websiteHandler.Index)
frontend.GET("/api").Handle(wrap(r.websiteHandler.APIDocs)) frontend.GET("/api").Handle(r.websiteHandler.APIDocs)
frontend.GET("/api.php").Handle(wrap(r.websiteHandler.APIDocs)) frontend.GET("/api.php").Handle(r.websiteHandler.APIDocs)
frontend.GET("/api.html").Handle(wrap(r.websiteHandler.APIDocs)) frontend.GET("/api.html").Handle(r.websiteHandler.APIDocs)
frontend.GET("/api_more").Handle(wrap(r.websiteHandler.APIDocsMore)) frontend.GET("/api_more").Handle(r.websiteHandler.APIDocsMore)
frontend.GET("/api_more.php").Handle(wrap(r.websiteHandler.APIDocsMore)) frontend.GET("/api_more.php").Handle(r.websiteHandler.APIDocsMore)
frontend.GET("/api_more.html").Handle(wrap(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").Handle(r.websiteHandler.MessageSent)
frontend.GET("/message_sent.php").Handle(wrap(r.websiteHandler.MessageSent)) frontend.GET("/message_sent.php").Handle(r.websiteHandler.MessageSent)
frontend.GET("/message_sent.html").Handle(wrap(r.websiteHandler.MessageSent)) frontend.GET("/message_sent.html").Handle(r.websiteHandler.MessageSent)
frontend.GET("/favicon.ico").Handle(wrap(r.websiteHandler.FaviconIco)) frontend.GET("/favicon.ico").Handle(r.websiteHandler.FaviconIco)
frontend.GET("/favicon.png").Handle(wrap(r.websiteHandler.FaviconPNG)) frontend.GET("/favicon.png").Handle(r.websiteHandler.FaviconPNG)
frontend.GET("/js/:fn").Handle(wrap(r.websiteHandler.Javascript)) frontend.GET("/js/:fn").Handle(r.websiteHandler.Javascript)
frontend.GET("/css/:fn").Handle(wrap(r.websiteHandler.CSS)) frontend.GET("/css/:fn").Handle(r.websiteHandler.CSS)
} }
// ================ Compat (v1) ================ // ================ Compat (v1) ================
compat := e.Routes().Group("/api") compat := e.Routes().Group("/api")
{ {
compat.GET("/register.php").Handle(wrap(r.compatHandler.Register)) compat.GET("/register.php").Handle(r.compatHandler.Register)
compat.GET("/info.php").Handle(wrap(r.compatHandler.Info)) compat.GET("/info.php").Handle(r.compatHandler.Info)
compat.GET("/ack.php").Handle(wrap(r.compatHandler.Ack)) compat.GET("/ack.php").Handle(r.compatHandler.Ack)
compat.GET("/requery.php").Handle(wrap(r.compatHandler.Requery)) compat.GET("/requery.php").Handle(r.compatHandler.Requery)
compat.GET("/update.php").Handle(wrap(r.compatHandler.Update)) compat.GET("/update.php").Handle(r.compatHandler.Update)
compat.GET("/expand.php").Handle(wrap(r.compatHandler.Expand)) compat.GET("/expand.php").Handle(r.compatHandler.Expand)
compat.GET("/upgrade.php").Handle(wrap(r.compatHandler.Upgrade)) compat.GET("/upgrade.php").Handle(r.compatHandler.Upgrade)
} }
// ================ Manage API (v2) ================ // ================ Manage API (v2) ================
apiv2 := e.Routes().Group("/api/v2/") apiv2 := e.Routes().Group("/api/v2/")
{ {
apiv2.POST("/users").Handle(wrap(r.apiHandler.CreateUser)) apiv2.POST("/users").Handle(r.apiHandler.CreateUser)
apiv2.GET("/users/:uid").Handle(wrap(r.apiHandler.GetUser)) apiv2.GET("/users/:uid").Handle(r.apiHandler.GetUser)
apiv2.PATCH("/users/:uid").Handle(wrap(r.apiHandler.UpdateUser)) apiv2.PATCH("/users/:uid").Handle(r.apiHandler.UpdateUser)
apiv2.GET("/users/:uid/keys").Handle(wrap(r.apiHandler.ListUserKeys)) apiv2.GET("/users/:uid/keys").Handle(r.apiHandler.ListUserKeys)
apiv2.POST("/users/:uid/keys").Handle(wrap(r.apiHandler.CreateUserKey)) apiv2.POST("/users/:uid/keys").Handle(r.apiHandler.CreateUserKey)
apiv2.GET("/users/:uid/keys/current").Handle(wrap(r.apiHandler.GetCurrentUserKey)) apiv2.GET("/users/:uid/keys/current").Handle(r.apiHandler.GetCurrentUserKey)
apiv2.GET("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.GetUserKey)) apiv2.GET("/users/:uid/keys/:kid").Handle(r.apiHandler.GetUserKey)
apiv2.PATCH("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.UpdateUserKey)) apiv2.PATCH("/users/:uid/keys/:kid").Handle(r.apiHandler.UpdateUserKey)
apiv2.DELETE("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.DeleteUserKey)) 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").Handle(r.apiHandler.ListClients)
apiv2.GET("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.GetClient)) apiv2.GET("/users/:uid/clients/:cid").Handle(r.apiHandler.GetClient)
apiv2.PATCH("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.UpdateClient)) apiv2.PATCH("/users/:uid/clients/:cid").Handle(r.apiHandler.UpdateClient)
apiv2.POST("/users/:uid/clients").Handle(wrap(r.apiHandler.AddClient)) apiv2.POST("/users/:uid/clients").Handle(r.apiHandler.AddClient)
apiv2.DELETE("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.DeleteClient)) apiv2.DELETE("/users/:uid/clients/:cid").Handle(r.apiHandler.DeleteClient)
apiv2.GET("/users/:uid/channels").Handle(wrap(r.apiHandler.ListChannels)) apiv2.GET("/users/:uid/channels").Handle(r.apiHandler.ListChannels)
apiv2.POST("/users/:uid/channels").Handle(wrap(r.apiHandler.CreateChannel)) apiv2.POST("/users/:uid/channels").Handle(r.apiHandler.CreateChannel)
apiv2.GET("/users/:uid/channels/:cid").Handle(wrap(r.apiHandler.GetChannel)) apiv2.GET("/users/:uid/channels/:cid").Handle(r.apiHandler.GetChannel)
apiv2.PATCH("/users/:uid/channels/:cid").Handle(wrap(r.apiHandler.UpdateChannel)) apiv2.PATCH("/users/:uid/channels/:cid").Handle(r.apiHandler.UpdateChannel)
apiv2.GET("/users/:uid/channels/:cid/messages").Handle(wrap(r.apiHandler.ListChannelMessages)) apiv2.GET("/users/:uid/channels/:cid/messages").Handle(r.apiHandler.ListChannelMessages)
apiv2.GET("/users/:uid/channels/:cid/subscriptions").Handle(wrap(r.apiHandler.ListChannelSubscriptions)) apiv2.GET("/users/:uid/channels/:cid/subscriptions").Handle(r.apiHandler.ListChannelSubscriptions)
apiv2.GET("/users/:uid/subscriptions").Handle(wrap(r.apiHandler.ListUserSubscriptions)) apiv2.GET("/users/:uid/subscriptions").Handle(r.apiHandler.ListUserSubscriptions)
apiv2.POST("/users/:uid/subscriptions").Handle(wrap(r.apiHandler.CreateSubscription)) apiv2.POST("/users/:uid/subscriptions").Handle(r.apiHandler.CreateSubscription)
apiv2.GET("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.GetSubscription)) apiv2.GET("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.GetSubscription)
apiv2.DELETE("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.CancelSubscription)) apiv2.DELETE("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.CancelSubscription)
apiv2.PATCH("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.UpdateSubscription)) apiv2.PATCH("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.UpdateSubscription)
apiv2.GET("/messages").Handle(wrap(r.apiHandler.ListMessages)) apiv2.GET("/messages").Handle(r.apiHandler.ListMessages)
apiv2.GET("/messages/:mid").Handle(wrap(r.apiHandler.GetMessage)) apiv2.GET("/messages/:mid").Handle(r.apiHandler.GetMessage)
apiv2.DELETE("/messages/:mid").Handle(wrap(r.apiHandler.DeleteMessage)) apiv2.DELETE("/messages/:mid").Handle(r.apiHandler.DeleteMessage)
apiv2.GET("/preview/users/:uid").Handle(wrap(r.apiHandler.GetUserPreview)) apiv2.GET("/preview/users/:uid").Handle(r.apiHandler.GetUserPreview)
apiv2.GET("/preview/keys/:kid").Handle(wrap(r.apiHandler.GetUserKeyPreview)) apiv2.GET("/preview/keys/:kid").Handle(r.apiHandler.GetUserKeyPreview)
apiv2.GET("/preview/channels/:cid").Handle(wrap(r.apiHandler.GetChannelPreview)) apiv2.GET("/preview/channels/:cid").Handle(r.apiHandler.GetChannelPreview)
} }
// ================ Send API (unversioned) ================ // ================ Send API (unversioned) ================
sendAPI := e.Routes().Group("") sendAPI := e.Routes().Group("")
{ {
sendAPI.POST("/").Handle(wrap(r.messageHandler.SendMessage) sendAPI.POST("/").Handle(r.messageHandler.SendMessage)
sendAPI.POST("/send").Handle(wrap(r.messageHandler.SendMessage) sendAPI.POST("/send").Handle(r.messageHandler.SendMessage)
sendAPI.POST("/send.php").Handle(wrap(r.compatHandler.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 ( import (
"blackforestbytes.com/simplecloudnotifier/db/schema" "blackforestbytes.com/simplecloudnotifier/db/schema"
"context" "context"
"database/sql"
"fmt" "fmt"
"github.com/glebarez/go-sqlite"
"gogs.mikescher.com/BlackForestBytes/goext/exerr" "gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq" "gogs.mikescher.com/BlackForestBytes/goext/sq"
"time" "time"
) )
@ -15,7 +18,9 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
sqlite3.Version() // ensure slite3 loaded if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()
}
fmt.Println() fmt.Println()

View File

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

View File

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

View File

@ -26,7 +26,12 @@ type Database struct {
func NewLogsDatabase(cfg server.Config) (*Database, error) { func NewLogsDatabase(cfg server.Config) (*Database, error) {
conf := cfg.DBLogs 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()) { if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3() sqlite.RegisterAsSQLITE3()

View File

@ -26,7 +26,12 @@ type Database struct {
func NewPrimaryDatabase(cfg server.Config) (*Database, error) { func NewPrimaryDatabase(cfg server.Config) (*Database, error) {
conf := cfg.DBMain 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()) { if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3() sqlite.RegisterAsSQLITE3()

View File

@ -26,7 +26,12 @@ type Database struct {
func NewRequestsDatabase(cfg server.Config) (*Database, error) { func NewRequestsDatabase(cfg server.Config) (*Database, error) {
conf := cfg.DBRequests 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()) { if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3() sqlite.RegisterAsSQLITE3()

View File

@ -12,7 +12,7 @@ require (
github.com/jmoiron/sqlx v1.4.0 github.com/jmoiron/sqlx v1.4.0
github.com/mattn/go-sqlite3 v1.14.22 github.com/mattn/go-sqlite3 v1.14.22
github.com/rs/zerolog v1.33.0 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 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= 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 h1:veU8oJdGZ9rjLB8sluagBduiBs3BbEDf60sGmEEv8lk=
gogs.mikescher.com/BlackForestBytes/goext v0.0.482/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM= 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.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 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=

View File

@ -71,7 +71,10 @@ func (ac *AppContext) Cancel() {
} }
ac.transaction = nil ac.transaction = nil
} }
ac.cancelFunc()
if ac.cancelFunc != nil {
ac.cancelFunc()
}
} }
func (ac *AppContext) RequestURI() string { func (ac *AppContext) RequestURI() string {
@ -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 { if ac.cancelled {
panic("Cannot finish a cancelled request") panic("Cannot finish a cancelled request")
} }

View File

@ -2,8 +2,6 @@ package logic
import ( import (
scn "blackforestbytes.com/simplecloudnotifier" scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/db" "blackforestbytes.com/simplecloudnotifier/db"
"blackforestbytes.com/simplecloudnotifier/db/simplectx" "blackforestbytes.com/simplecloudnotifier/db/simplectx"
"blackforestbytes.com/simplecloudnotifier/google" "blackforestbytes.com/simplecloudnotifier/google"
@ -11,10 +9,8 @@ import (
"blackforestbytes.com/simplecloudnotifier/push" "blackforestbytes.com/simplecloudnotifier/push"
"context" "context"
"errors" "errors"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext" "gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rext" "gogs.mikescher.com/BlackForestBytes/goext/rext"
"gogs.mikescher.com/BlackForestBytes/goext/syncext" "gogs.mikescher.com/BlackForestBytes/goext/syncext"
"net" "net"
@ -208,32 +204,6 @@ func (app *Application) Migrate() error {
return app.Database.Migrate(ctx) 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 { func (app *Application) NewSimpleTransactionContext(timeout time.Duration) *simplectx.SimpleContext {
ictx, cancel := context.WithTimeout(context.Background(), timeout) ictx, cancel := context.WithTimeout(context.Background(), timeout)
return simplectx.CreateSimpleContext(ictx, cancel) return simplectx.CreateSimpleContext(ictx, cancel)

View File

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

View File

@ -6,6 +6,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"errors" "errors"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "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/langext"
import "gogs.mikescher.com/BlackForestBytes/goext/enums" import "gogs.mikescher.com/BlackForestBytes/goext/enums"
const ChecksumEnumGenerator = "fd2e0463f7720d853f7a3394352c084ac7d086e9e012caa3d3d70a6e83749970" // GoExtVersion: 0.0.482 const ChecksumEnumGenerator = "ba14f2f5d0b0357f248dcbd12933de102c80f1e61be697a37ebb723609fc0c59" // GoExtVersion: 0.0.485
// ================================ ClientType ================================ // ================================ ClientType ================================
// //

View File

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

View File

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

View File

@ -14,6 +14,7 @@ definitions:
- 1151 - 1151
- 1152 - 1152
- 1153 - 1153
- 1152
- 1161 - 1161
- 1171 - 1171
- 1201 - 1201
@ -59,6 +60,7 @@ definitions:
- BINDFAIL_QUERY_PARAM - BINDFAIL_QUERY_PARAM
- BINDFAIL_BODY_PARAM - BINDFAIL_BODY_PARAM
- BINDFAIL_URI_PARAM - BINDFAIL_URI_PARAM
- BINDFAIL_HEADER_PARAM
- INVALID_BODY_PARAM - INVALID_BODY_PARAM
- INVALID_ENUM_VALUE - INVALID_ENUM_VALUE
- NO_TITLE - NO_TITLE
@ -327,19 +329,36 @@ definitions:
type: object type: object
handler.SendMessage.combined: handler.SendMessage.combined:
properties: properties:
channel:
example: test
type: string
content: content:
example: This is a message
type: string
key:
example: P3TNH8mvv14fm
type: string type: string
msg_id: msg_id:
example: db8b0e6a-a08c-4646
type: string type: string
priority: priority:
enum:
- 0
- 1
- 2
example: 1
type: integer type: integer
sender_name:
example: example-server
type: string
timestamp: timestamp:
example: 1669824037
type: number type: number
title: title:
example: Hello World
type: string type: string
user_id: user_id:
type: integer example: "7725"
user_key:
type: string type: string
type: object type: object
handler.SendMessage.response: handler.SendMessage.response:
@ -359,7 +378,7 @@ definitions:
quota_max: quota_max:
type: integer type: integer
scn_msg_id: scn_msg_id:
type: integer type: string
success: success:
type: boolean type: boolean
suppress_send: suppress_send:
@ -785,52 +804,90 @@ paths:
description: All parameter can be set via query-parameter or the json body. description: All parameter can be set via query-parameter or the json body.
Only UserID, UserKey and Title are required Only UserID, UserKey and Title are required
parameters: parameters:
- in: query - example: test
in: query
name: channel
type: string
- example: This is a message
in: query
name: content name: content
type: string type: string
- in: query - example: P3TNH8mvv14fm
in: query
name: key
type: string
- example: db8b0e6a-a08c-4646
in: query
name: msg_id name: msg_id
type: string type: string
- in: query - enum:
- 0
- 1
- 2
example: 1
in: query
name: priority name: priority
type: integer type: integer
- in: query - example: example-server
in: query
name: sender_name
type: string
- example: 1669824037
in: query
name: timestamp name: timestamp
type: number type: number
- in: query - example: Hello World
in: query
name: title name: title
type: string type: string
- in: query - example: "7725"
in: query
name: user_id name: user_id
type: integer
- in: query
name: user_key
type: string type: string
- description: ' ' - description: ' '
in: body in: body
name: post_body name: post_body
schema: schema:
$ref: '#/definitions/handler.SendMessage.combined' $ref: '#/definitions/handler.SendMessage.combined'
- in: formData - example: test
in: formData
name: channel
type: string
- example: This is a message
in: formData
name: content name: content
type: string type: string
- in: formData - example: P3TNH8mvv14fm
in: formData
name: key
type: string
- example: db8b0e6a-a08c-4646
in: formData
name: msg_id name: msg_id
type: string type: string
- in: formData - enum:
- 0
- 1
- 2
example: 1
in: formData
name: priority name: priority
type: integer type: integer
- in: formData - example: example-server
in: formData
name: sender_name
type: string
- example: 1669824037
in: formData
name: timestamp name: timestamp
type: number type: number
- in: formData - example: Hello World
in: formData
name: title name: title
type: string type: string
- in: formData - example: "7725"
in: formData
name: user_id name: user_id
type: integer
- in: formData
name: user_key
type: string type: string
responses: responses:
"200": "200":
@ -2630,52 +2687,90 @@ paths:
description: All parameter can be set via query-parameter or the json body. description: All parameter can be set via query-parameter or the json body.
Only UserID, UserKey and Title are required Only UserID, UserKey and Title are required
parameters: parameters:
- in: query - example: test
in: query
name: channel
type: string
- example: This is a message
in: query
name: content name: content
type: string type: string
- in: query - example: P3TNH8mvv14fm
in: query
name: key
type: string
- example: db8b0e6a-a08c-4646
in: query
name: msg_id name: msg_id
type: string type: string
- in: query - enum:
- 0
- 1
- 2
example: 1
in: query
name: priority name: priority
type: integer type: integer
- in: query - example: example-server
in: query
name: sender_name
type: string
- example: 1669824037
in: query
name: timestamp name: timestamp
type: number type: number
- in: query - example: Hello World
in: query
name: title name: title
type: string type: string
- in: query - example: "7725"
in: query
name: user_id name: user_id
type: integer
- in: query
name: user_key
type: string type: string
- description: ' ' - description: ' '
in: body in: body
name: post_body name: post_body
schema: schema:
$ref: '#/definitions/handler.SendMessage.combined' $ref: '#/definitions/handler.SendMessage.combined'
- in: formData - example: test
in: formData
name: channel
type: string
- example: This is a message
in: formData
name: content name: content
type: string type: string
- in: formData - example: P3TNH8mvv14fm
in: formData
name: key
type: string
- example: db8b0e6a-a08c-4646
in: formData
name: msg_id name: msg_id
type: string type: string
- in: formData - enum:
- 0
- 1
- 2
example: 1
in: formData
name: priority name: priority
type: integer type: integer
- in: formData - example: example-server
in: formData
name: sender_name
type: string
- example: 1669824037
in: formData
name: timestamp name: timestamp
type: number type: number
- in: formData - example: Hello World
in: formData
name: title name: title
type: string type: string
- in: formData - example: "7725"
in: formData
name: user_id name: user_id
type: integer
- in: formData
name: user_key
type: string type: string
responses: responses:
"200": "200":
@ -2708,47 +2803,85 @@ paths:
description: All parameter can be set via query-parameter or form-data body. description: All parameter can be set via query-parameter or form-data body.
Only UserID, UserKey and Title are required Only UserID, UserKey and Title are required
parameters: parameters:
- in: query - example: test
in: query
name: channel
type: string
- example: This is a message
in: query
name: content name: content
type: string type: string
- in: query - example: P3TNH8mvv14fm
in: query
name: key
type: string
- example: db8b0e6a-a08c-4646
in: query
name: msg_id name: msg_id
type: string type: string
- in: query - enum:
- 0
- 1
- 2
example: 1
in: query
name: priority name: priority
type: integer type: integer
- in: query - example: example-server
in: query
name: sender_name
type: string
- example: 1669824037
in: query
name: timestamp name: timestamp
type: number type: number
- in: query - example: Hello World
in: query
name: title name: title
type: string type: string
- in: query - example: "7725"
in: query
name: user_id name: user_id
type: integer
- in: query
name: user_key
type: string type: string
- in: formData - example: test
in: formData
name: channel
type: string
- example: This is a message
in: formData
name: content name: content
type: string type: string
- in: formData - example: P3TNH8mvv14fm
in: formData
name: key
type: string
- example: db8b0e6a-a08c-4646
in: formData
name: msg_id name: msg_id
type: string type: string
- in: formData - enum:
- 0
- 1
- 2
example: 1
in: formData
name: priority name: priority
type: integer type: integer
- in: formData - example: example-server
in: formData
name: sender_name
type: string
- example: 1669824037
in: formData
name: timestamp name: timestamp
type: number type: number
- in: formData - example: Hello World
in: formData
name: title name: title
type: string type: string
- in: formData - example: "7725"
in: formData
name: user_id name: user_id
type: integer
- in: formData
name: user_key
type: string type: string
responses: responses:
"200": "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()} buflogger = &BufferWriter{cw: createConsoleWriter()}
log.Logger = createLogger(buflogger) log.Logger = createLogger(buflogger)
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
ginext.SuppressGinLogs = true
} }
func ClearBufLogger(dump bool) { func ClearBufLogger(dump bool) {
@ -23,7 +22,6 @@ func ClearBufLogger(dump bool) {
log.Logger = createLogger(createConsoleWriter()) log.Logger = createLogger(createConsoleWriter())
buflogger = nil buflogger = nil
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
ginext.SuppressGinLogs = false
if !dump { if !dump {
log.Info().Msgf("Suppressed %d logmessages / printf-statements", size) log.Info().Msgf("Suppressed %d logmessages / printf-statements", size)
} }

View File

@ -7,6 +7,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/jobs" "blackforestbytes.com/simplecloudnotifier/jobs"
"blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/push" "blackforestbytes.com/simplecloudnotifier/push"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"os" "os"
"path/filepath" "path/filepath"
@ -87,7 +88,13 @@ func StartSimpleWebserver(t *testing.T) (*logic.Application, string, func()) {
TestFailErr(t, err) 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) router := api.NewRouter(app)