working commit

This commit is contained in:
2026-06-05 18:25:03 +02:00
commit 7d8abba003
82 changed files with 21863 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
package auxgin
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func CorsMiddleware() gin.HandlerFunc {
headers := []string{"Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}
headerList := strings.Join(headers, ",")
methods := []string{"POST", "GET", "OPTIONS", "PUT", "DELETE", "UPDATE"}
methodList := strings.Join(methods, ",")
return func(gctx *gin.Context) {
gctx.Writer.Header().Set("Access-Control-Allow-Origin", "*")
gctx.Writer.Header().Set("Access-Control-Max-Age", "86400")
gctx.Writer.Header().Set("Access-Control-Allow-Methods", methodList)
gctx.Writer.Header().Set("Access-Control-Allow-Headers", headerList)
gctx.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if gctx.Request.Method == "OPTIONS" {
gctx.AbortWithStatus(http.StatusOK)
} else {
gctx.Next()
}
}
}
+60
View File
@@ -0,0 +1,60 @@
package auxgin
import (
"bytes"
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func LogMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
start := time.Now()
ctx.Next()
var reqSize int64
var method string
var reqURI string
var remAddr string
if ctx.Request != nil {
reqSize = ctx.Request.ContentLength
method = ctx.Request.Method
reqURI = ctx.Request.RequestURI
remAddr = ctx.RemoteIP()
}
duration := time.Since(start).Microseconds()
var resCode int
var resSize int
if ctx.Writer != nil {
resCode = ctx.Writer.Status()
resSize = ctx.Writer.Size()
}
logString := fmt.Sprintf("%s %s %s in=%d out=%d res=%d %dms",
remAddr, method, reqURI, reqSize, resSize, resCode, duration)
logger := logrus.WithField("object", "accesslog")
logger.Infoln(logString)
}
}
type LogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (lw LogWriter) Write(data []byte) (int, error) {
lw.body.Write(data)
return lw.ResponseWriter.Write(data)
}
func (lw LogWriter) WriteString(data string) (int, error) {
lw.body.WriteString(data)
return lw.ResponseWriter.WriteString(data)
}
+34
View File
@@ -0,0 +1,34 @@
package auxgin
import (
"bytes"
"encoding/json"
"io/ioutil"
"strings"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func RequestLogMiddleware() gin.HandlerFunc {
return func(context *gin.Context) {
contentType := context.GetHeader("Content-Type")
contentType = strings.ToLower(contentType)
var requestBody []byte
if context.Request.Body != nil {
requestBody, _ = ioutil.ReadAll(context.Request.Body)
}
if strings.Contains(contentType, "application/json") && context.Request.Method == "POST" {
buffer := bytes.NewBuffer(nil)
json.Indent(buffer, requestBody, "", " ")
logger := logrus.WithField("object", "requestlog")
logger.Infoln("request:\n", buffer.String())
}
context.Request.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
context.Next()
}
}
+32
View File
@@ -0,0 +1,32 @@
package auxgin
import (
"bytes"
"encoding/json"
"strings"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func ResponseLogMiddleware() gin.HandlerFunc {
return func(context *gin.Context) {
contentType := context.GetHeader("Content-Type")
contentType = strings.ToLower(contentType)
writer := &LogWriter{
body: bytes.NewBuffer(nil),
ResponseWriter: context.Writer,
}
context.Writer = writer
context.Next()
if strings.Contains(contentType, "application/json") {
buffer := bytes.NewBuffer(nil)
json.Indent(buffer, writer.body.Bytes(), "", " ")
logger := logrus.WithField("object", "responselog")
logger.Infoln("request:\n", buffer.String())
}
}
}