move server/* to scnserver/*

This commit is contained in:
2022-12-21 12:35:56 +01:00
parent 2b4d77bab4
commit bbf7962e29
123 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package ginresp
type apiError struct {
Success bool `json:"success"`
Error int `json:"error"`
ErrorHighlight int `json:"errhighlight"`
Message string `json:"message"`
RawError *string `json:"errorObj,omitempty"`
Trace string `json:"traceObj,omitempty"`
}
type compatAPIError struct {
Success bool `json:"success"`
ErrorID int `json:"errid,omitempty"`
Message string `json:"message"`
}

View File

@@ -0,0 +1,139 @@
package ginresp
import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/apihighlight"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug"
)
type HTTPResponse interface {
Write(g *gin.Context)
}
type jsonHTTPResponse struct {
statusCode int
data any
}
func (j jsonHTTPResponse) Write(g *gin.Context) {
g.JSON(j.statusCode, j.data)
}
type emptyHTTPResponse struct {
statusCode int
}
func (j emptyHTTPResponse) Write(g *gin.Context) {
g.Status(j.statusCode)
}
type textHTTPResponse struct {
statusCode int
data string
}
func (j textHTTPResponse) Write(g *gin.Context) {
g.String(j.statusCode, "%s", j.data)
}
type dataHTTPResponse struct {
statusCode int
data []byte
contentType string
}
func (j dataHTTPResponse) Write(g *gin.Context) {
g.Data(j.statusCode, j.contentType, j.data)
}
type errorHTTPResponse struct {
statusCode int
data any
error error
}
func (j errorHTTPResponse) Write(g *gin.Context) {
g.JSON(j.statusCode, j.data)
}
func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc}
}
func JSON(sc int, data any) HTTPResponse {
return &jsonHTTPResponse{statusCode: sc, data: data}
}
func Data(sc int, contentType string, data []byte) HTTPResponse {
return &dataHTTPResponse{statusCode: sc, contentType: contentType, data: data}
}
func Text(sc int, data string) HTTPResponse {
return &textHTTPResponse{statusCode: sc, data: data}
}
func InternalError(e error) HTTPResponse {
return createApiError(nil, "InternalError", 500, apierr.INTERNAL_EXCEPTION, 0, e.Error(), e)
}
func APIError(g *gin.Context, status int, errorid apierr.APIError, msg string, e error) HTTPResponse {
return createApiError(g, "APIError", status, errorid, 0, msg, e)
}
func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
return createApiError(g, "SendAPIError", status, errorid, highlight, msg, e)
}
func NotImplemented(g *gin.Context) HTTPResponse {
return createApiError(g, "NotImplemented", 500, apierr.UNDEFINED, 0, "Not Implemented", nil)
}
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
reqUri := ""
if g != nil && g.Request != nil {
reqUri = g.Request.Method + " :: " + g.Request.RequestURI
}
log.Error().
Int("errorid", int(errorid)).
Int("highlight", int(highlight)).
Str("uri", reqUri).
AnErr("err", e).
Stack().
Msg(fmt.Sprintf("[%s] %s", ident, msg))
if scn.Conf.ReturnRawErrors {
return &errorHTTPResponse{
statusCode: status,
data: apiError{
Success: false,
Error: int(errorid),
ErrorHighlight: int(highlight),
Message: msg,
RawError: langext.Ptr(langext.Conditional(e == nil, "", fmt.Sprintf("%+v", e))),
Trace: string(debug.Stack()),
},
error: e,
}
} else {
return &errorHTTPResponse{
statusCode: status,
data: apiError{
Success: false,
Error: int(errorid),
ErrorHighlight: int(highlight),
Message: msg,
},
error: e,
}
}
}
func CompatAPIError(errid int, msg string) HTTPResponse {
return &jsonHTTPResponse{statusCode: 200, data: compatAPIError{Success: false, ErrorID: errid, Message: msg}}
}

View File

@@ -0,0 +1,80 @@
package ginresp
import (
scn "blackforestbytes.com/simplecloudnotifier"
"github.com/gin-gonic/gin"
"github.com/mattn/go-sqlite3"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"time"
)
type WHandlerFunc func(*gin.Context) HTTPResponse
func Wrap(fn WHandlerFunc) gin.HandlerFunc {
maxRetry := scn.Conf.RequestMaxRetry
retrySleep := scn.Conf.RequestRetrySleep
return func(g *gin.Context) {
reqctx := g.Request.Context()
if g.Request.Body != nil {
g.Request.Body = dataext.NewBufferedReadCloser(g.Request.Body)
}
for ctr := 1; ; ctr++ {
wrap := fn(g)
if g.Writer.Written() {
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(retrySleep)
continue
}
if reqctx.Err() == nil {
wrap.Write(g)
}
return
}
}
}
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 HTTPResponse) bool {
if errwrap, ok := r.(*errorHTTPResponse); ok && errwrap != nil {
if s3err, ok := (errwrap.error).(sqlite3.Error); ok {
if s3err.Code == sqlite3.ErrBusy {
return true
}
}
}
return false
}