fix: publickey

This commit is contained in:
ttk
2024-09-02 18:51:26 +08:00
parent 5caf481b68
commit c09565d673
11 changed files with 1038 additions and 54 deletions

View File

@@ -1,13 +1,19 @@
package api
import (
"bytes"
"encoding/json"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
"go.uber.org/zap"
"github.com/veops/oneterm/acl"
"github.com/veops/oneterm/api/controller"
myi18n "github.com/veops/oneterm/i18n"
"github.com/veops/oneterm/logger"
)
@@ -63,3 +69,53 @@ func auth() gin.HandlerFunc {
ctx.Next()
}
}
type bodyWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodyWriter) Write(b []byte) (int, error) {
return w.body.Write(b)
}
func Error2Resp() gin.HandlerFunc {
return func(ctx *gin.Context) {
if strings.Contains(ctx.Request.URL.String(), "session/replay") {
ctx.Next()
return
}
wb := &bodyWriter{
body: &bytes.Buffer{},
ResponseWriter: ctx.Writer,
}
ctx.Writer = wb
ctx.Next()
obj := make(map[string]any)
json.Unmarshal(wb.body.Bytes(), &obj)
if len(ctx.Errors) > 0 {
if v, ok := obj["code"]; !ok || v == 0 {
obj["code"] = ctx.Writer.Status()
}
if v, ok := obj["message"]; !ok || v == "" {
e := ctx.Errors.Last().Err
obj["message"] = e.Error()
ae, ok := e.(*controller.ApiError)
if ok {
lang := ctx.PostForm("lang")
accept := ctx.GetHeader("Accept-Language")
localizer := i18n.NewLocalizer(myi18n.Bundle, lang, accept)
obj["message"] = ae.Message(localizer)
}
}
}
bs, _ := json.Marshal(obj)
wb.ResponseWriter.Write(bs)
}
}