Refactor server to go-sqlite and ginext [WIP]
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
||||
"time"
|
||||
)
|
||||
@@ -81,7 +82,7 @@ func (ac *AppContext) RequestURI() string {
|
||||
}
|
||||
}
|
||||
|
||||
func (ac *AppContext) FinishSuccess(res ginresp.HTTPResponse) ginresp.HTTPResponse {
|
||||
func (ac *AppContext) FinishSuccess(res ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
if ac.cancelled {
|
||||
panic("Cannot finish a cancelled request")
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
scn "blackforestbytes.com/simplecloudnotifier"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/db"
|
||||
"blackforestbytes.com/simplecloudnotifier/db/simplectx"
|
||||
"blackforestbytes.com/simplecloudnotifier/google"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
@@ -11,13 +12,12 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/rext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/syncext"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"regexp"
|
||||
@@ -33,7 +33,7 @@ var rexCompatTitleChannel = rext.W(regexp.MustCompile("^\\[(?P<channel>[A-Za-z\\
|
||||
|
||||
type Application struct {
|
||||
Config scn.Config
|
||||
Gin *gin.Engine
|
||||
Gin *ginext.GinWrapper
|
||||
Database *DBPool
|
||||
Pusher push.NotificationClient
|
||||
AndroidPublisher google.AndroidPublisherClient
|
||||
@@ -53,7 +53,7 @@ func NewApp(db *DBPool) *Application {
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Application) Init(cfg scn.Config, g *gin.Engine, fb push.NotificationClient, apc google.AndroidPublisherClient, jobs []Job) {
|
||||
func (app *Application) Init(cfg scn.Config, g *ginext.GinWrapper, fb push.NotificationClient, apc google.AndroidPublisherClient, jobs []Job) {
|
||||
app.Config = cfg
|
||||
app.Gin = g
|
||||
app.Pusher = fb
|
||||
@@ -69,38 +69,17 @@ func (app *Application) Stop() {
|
||||
}
|
||||
|
||||
func (app *Application) Run() {
|
||||
httpserver := &http.Server{
|
||||
Addr: net.JoinHostPort(app.Config.ServerIP, app.Config.ServerPort),
|
||||
Handler: app.Gin,
|
||||
}
|
||||
|
||||
errChan := make(chan error)
|
||||
// ================== START HTTP ==================
|
||||
|
||||
go func() {
|
||||
|
||||
ln, err := net.Listen("tcp", httpserver.Addr)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
_, port, err := net.SplitHostPort(ln.Addr().String())
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Str("address", httpserver.Addr).Msg("HTTP-Server started on http://localhost:" + port)
|
||||
addr := net.JoinHostPort(app.Config.ServerIP, app.Config.ServerPort)
|
||||
|
||||
errChan, httpserver := app.Gin.ListenAndServeHTTP(addr, func(port string) {
|
||||
app.Port = port
|
||||
app.IsRunning.Set(true)
|
||||
})
|
||||
|
||||
app.IsRunning.Set(true) // the net.Listener a few lines above is at this point actually already buffering requests
|
||||
|
||||
errChan <- httpserver.Serve(ln)
|
||||
}()
|
||||
|
||||
sigstop := make(chan os.Signal, 1)
|
||||
signal.Notify(sigstop, os.Interrupt, syscall.SIGTERM)
|
||||
// ================== START JOBS ==================
|
||||
|
||||
for _, job := range app.Jobs {
|
||||
err := job.Start()
|
||||
@@ -109,6 +88,11 @@ func (app *Application) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
// ================== LISTEN FOR SIGNALS ==================
|
||||
|
||||
sigstop := make(chan os.Signal, 1)
|
||||
signal.Notify(sigstop, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
select {
|
||||
case <-sigstop:
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
@@ -127,7 +111,7 @@ func (app *Application) Run() {
|
||||
case err := <-errChan:
|
||||
log.Error().Err(err).Msg("HTTP-Server failed")
|
||||
|
||||
case _ = <-app.stopChan:
|
||||
case <-app.stopChan:
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -142,20 +126,25 @@ func (app *Application) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
// ================== STOP JOBS ==================
|
||||
|
||||
for _, job := range app.Jobs {
|
||||
job.Stop()
|
||||
}
|
||||
|
||||
log.Info().Msg("Manually stopped Jobs")
|
||||
// ================== STOP DB ==================
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
err := app.Database.Stop(ctx)
|
||||
if err != nil {
|
||||
log.Info().Err(err).Msg("Error while stopping the database")
|
||||
{
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
err := app.Database.Stop(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to stop database")
|
||||
}
|
||||
}
|
||||
log.Info().Msg("Stopped Databases")
|
||||
|
||||
log.Info().Msg("Manually closed database connection")
|
||||
// ================== FINISH ==================
|
||||
|
||||
app.IsRunning.Set(false)
|
||||
}
|
||||
@@ -223,65 +212,26 @@ type RequestOptions struct {
|
||||
IgnoreWrongContentType bool
|
||||
}
|
||||
|
||||
func (app *Application) StartRequest(g *gin.Context, uri any, query any, body any, form any, opts ...RequestOptions) (*AppContext, *ginresp.HTTPResponse) {
|
||||
func (app *Application) StartRequest(gectx *ginext.AppContext, g *gin.Context, r *ginext.HTTPResponse) (*AppContext, *gin.Context, *ginext.HTTPResponse) {
|
||||
|
||||
ignoreWrongContentType := langext.ArrAny(opts, func(o RequestOptions) bool { return o.IgnoreWrongContentType })
|
||||
|
||||
if uri != nil {
|
||||
if err := g.ShouldBindUri(uri); err != nil {
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.BINDFAIL_URI_PARAM, "Failed to read uri", err))
|
||||
}
|
||||
if r != nil {
|
||||
return nil, g, r
|
||||
}
|
||||
|
||||
if query != nil {
|
||||
if err := g.ShouldBindQuery(query); err != nil {
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Failed to read query", err))
|
||||
}
|
||||
}
|
||||
|
||||
if body != nil {
|
||||
if g.ContentType() == "application/json" {
|
||||
if err := g.ShouldBindJSON(body); err != nil {
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Failed to read body", err))
|
||||
}
|
||||
} else {
|
||||
if !ignoreWrongContentType {
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing JSON body", nil))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if form != nil {
|
||||
if g.ContentType() == "multipart/form-data" {
|
||||
if err := g.ShouldBindWith(form, binding.Form); err != nil {
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Failed to read multipart-form", err))
|
||||
}
|
||||
} else if g.ContentType() == "application/x-www-form-urlencoded" {
|
||||
if err := g.ShouldBindWith(form, binding.Form); err != nil {
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Failed to read urlencoded-form", err))
|
||||
}
|
||||
} else {
|
||||
if !ignoreWrongContentType {
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing form body", nil))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ictx, cancel := context.WithTimeout(context.Background(), app.Config.RequestTimeout)
|
||||
actx := CreateAppContext(app, g, ictx, cancel)
|
||||
actx := CreateAppContext(app, g, gectx, gectx.Cancel)
|
||||
|
||||
authheader := g.GetHeader("Authorization")
|
||||
|
||||
perm, err := app.getPermissions(actx, authheader)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, langext.Ptr(ginresp.APIError(g, 400, apierr.PERM_QUERY_FAIL, "Failed to determine permissions", err))
|
||||
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, nil
|
||||
return actx, g, nil
|
||||
}
|
||||
|
||||
func (app *Application) NewSimpleTransactionContext(timeout time.Duration) *simplectx.SimpleContext {
|
||||
@@ -289,7 +239,7 @@ func (app *Application) NewSimpleTransactionContext(timeout time.Duration) *simp
|
||||
return simplectx.CreateSimpleContext(ictx, cancel)
|
||||
}
|
||||
|
||||
func (app *Application) getPermissions(ctx *AppContext, hdr string) (models.PermissionSet, error) {
|
||||
func (app *Application) getPermissions(ctx db.TxContext, hdr string) (models.PermissionSet, error) {
|
||||
if hdr == "" {
|
||||
return models.NewEmptyPermissions(), nil
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ type SendMessageResponse struct {
|
||||
CompatMessageID int64
|
||||
}
|
||||
|
||||
func (app *Application) SendMessage(g *gin.Context, ctx *AppContext, UserID *models.UserID, Key *string, Channel *string, Title *string, Content *string, Priority *int, UserMessageID *string, SendTimestamp *float64, SenderName *string) (*SendMessageResponse, *ginresp.HTTPResponse) {
|
||||
func (app *Application) SendMessage(g *gin.Context, ctx *AppContext, UserID *models.UserID, Key *string, Channel *string, Title *string, Content *string, Priority *int, UserMessageID *string, SendTimestamp *float64, SenderName *string) (*SendMessageResponse, *ginext.HTTPResponse) {
|
||||
if Title != nil {
|
||||
Title = langext.Ptr(strings.TrimSpace(*Title))
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@ import (
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
)
|
||||
|
||||
func (ac *AppContext) CheckPermissionUserRead(userid models.UserID) *ginresp.HTTPResponse {
|
||||
func (ac *AppContext) CheckPermissionUserRead(userid models.UserID) *ginext.HTTPResponse {
|
||||
p := ac.permissions
|
||||
if p.Token != nil && p.Token.IsUserRead(userid) {
|
||||
return nil
|
||||
@@ -18,7 +18,7 @@ func (ac *AppContext) CheckPermissionUserRead(userid models.UserID) *ginresp.HTT
|
||||
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
||||
}
|
||||
|
||||
func (ac *AppContext) CheckPermissionSelfAllMessagesRead() *ginresp.HTTPResponse {
|
||||
func (ac *AppContext) CheckPermissionSelfAllMessagesRead() *ginext.HTTPResponse {
|
||||
p := ac.permissions
|
||||
if p.Token != nil && p.Token.IsAllMessagesRead(p.Token.OwnerUserID) {
|
||||
return nil
|
||||
@@ -27,7 +27,7 @@ func (ac *AppContext) CheckPermissionSelfAllMessagesRead() *ginresp.HTTPResponse
|
||||
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
||||
}
|
||||
|
||||
func (ac *AppContext) CheckPermissionAllMessagesRead(userid models.UserID) *ginresp.HTTPResponse {
|
||||
func (ac *AppContext) CheckPermissionAllMessagesRead(userid models.UserID) *ginext.HTTPResponse {
|
||||
p := ac.permissions
|
||||
if p.Token != nil && p.Token.IsAllMessagesRead(userid) {
|
||||
return nil
|
||||
@@ -36,7 +36,7 @@ func (ac *AppContext) CheckPermissionAllMessagesRead(userid models.UserID) *ginr
|
||||
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
||||
}
|
||||
|
||||
func (ac *AppContext) CheckPermissionChanMessagesRead(channel models.Channel) *ginresp.HTTPResponse {
|
||||
func (ac *AppContext) CheckPermissionChanMessagesRead(channel models.Channel) *ginext.HTTPResponse {
|
||||
p := ac.permissions
|
||||
if p.Token != nil && p.Token.IsChannelMessagesRead(channel.ChannelID) {
|
||||
|
||||
@@ -63,7 +63,7 @@ func (ac *AppContext) CheckPermissionChanMessagesRead(channel models.Channel) *g
|
||||
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
||||
}
|
||||
|
||||
func (ac *AppContext) CheckPermissionUserAdmin(userid models.UserID) *ginresp.HTTPResponse {
|
||||
func (ac *AppContext) CheckPermissionUserAdmin(userid models.UserID) *ginext.HTTPResponse {
|
||||
p := ac.permissions
|
||||
if p.Token != nil && p.Token.IsAdmin(userid) {
|
||||
return nil
|
||||
@@ -72,7 +72,7 @@ func (ac *AppContext) CheckPermissionUserAdmin(userid models.UserID) *ginresp.HT
|
||||
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
||||
}
|
||||
|
||||
func (ac *AppContext) CheckPermissionSend(channel models.Channel, key string) (*models.KeyToken, *ginresp.HTTPResponse) {
|
||||
func (ac *AppContext) CheckPermissionSend(channel models.Channel, key string) (*models.KeyToken, *ginext.HTTPResponse) {
|
||||
|
||||
keytok, err := ac.app.Database.Primary.GetKeyTokenByToken(ac, key)
|
||||
if err != nil {
|
||||
@@ -107,7 +107,7 @@ func (ac *AppContext) CheckPermissionMessageDelete(msg models.Message) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (ac *AppContext) CheckPermissionAny() *ginresp.HTTPResponse {
|
||||
func (ac *AppContext) CheckPermissionAny() *ginext.HTTPResponse {
|
||||
p := ac.permissions
|
||||
if p.Token == nil {
|
||||
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
||||
|
Reference in New Issue
Block a user