mirror of
https://github.com/veops/oneterm.git
synced 2025-10-06 15:57:04 +08:00
fix: publickey
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user