11package api
22
33import (
4- "errors"
54 "github.com/0xJacky/Nginx-UI/model"
65 "github.com/gin-gonic/gin"
7- "github.com/go-playground/validator/v10"
86 "github.com/uozi-tech/cosy/logger"
97 "net/http"
10- "reflect"
11- "regexp"
12- "strings"
138)
149
1510func CurrentUser (c * gin.Context ) * model.User {
@@ -23,105 +18,10 @@ func ErrHandler(c *gin.Context, err error) {
2318 })
2419}
2520
26- type ValidError struct {
27- Key string
28- Message string
29- }
30-
31- func BindAndValid (c * gin.Context , target interface {}) bool {
32- err := c .ShouldBindJSON (target )
33- if err != nil {
34- logger .Error ("bind err" , err )
35-
36- var verrs validator.ValidationErrors
37- ok := errors .As (err , & verrs )
38-
39- if ! ok {
40- c .JSON (http .StatusNotAcceptable , gin.H {
41- "message" : "Requested with wrong parameters" ,
42- "code" : http .StatusNotAcceptable ,
43- })
44- return false
45- }
46-
47- t := reflect .TypeOf (target ).Elem ()
48- errorsMap := make (map [string ]interface {})
49- for _ , value := range verrs {
50- var path []string
51-
52- namespace := strings .Split (value .StructNamespace (), "." )
53- // logger.Debug(t.Name(), namespace)
54- if t .Name () != "" && len (namespace ) > 1 {
55- namespace = namespace [1 :]
56- }
57-
58- getJsonPath (t , namespace , & path )
59- insertError (errorsMap , path , value .Tag ())
60- }
61-
62- c .JSON (http .StatusNotAcceptable , gin.H {
63- "errors" : errorsMap ,
64- "message" : "Requested with wrong parameters" ,
65- "code" : http .StatusNotAcceptable ,
66- })
67-
68- return false
69- }
70-
71- return true
72- }
73-
74- // findField recursively finds the field in a nested struct
75- func getJsonPath (t reflect.Type , fields []string , path * []string ) {
76- field := fields [0 ]
77- // used in case of array
78- var index string
79- if field [len (field )- 1 ] == ']' {
80- re := regexp .MustCompile (`(\w+)\[(\d+)\]` )
81- matches := re .FindStringSubmatch (field )
82-
83- if len (matches ) > 2 {
84- field = matches [1 ]
85- index = matches [2 ]
86- }
87- }
88-
89- f , ok := t .FieldByName (field )
90- if ! ok {
91- return
92- }
93-
94- * path = append (* path , f .Tag .Get ("json" ))
95-
96- if index != "" {
97- * path = append (* path , index )
98- }
99-
100- if len (fields ) > 1 {
101- subFields := fields [1 :]
102- getJsonPath (f .Type , subFields , path )
103- }
104- }
105-
106- // insertError inserts an error into the errors map
107- func insertError (errorsMap map [string ]interface {}, path []string , errorTag string ) {
108- if len (path ) == 0 {
109- return
110- }
111-
112- jsonTag := path [0 ]
113- if len (path ) == 1 {
114- // Last element in the path, set the error
115- errorsMap [jsonTag ] = errorTag
116- return
117- }
118-
119- // Create a new map if necessary
120- if _ , ok := errorsMap [jsonTag ]; ! ok {
121- errorsMap [jsonTag ] = make (map [string ]interface {})
122- }
123-
124- // Recursively insert into the nested map
125- subMap , _ := errorsMap [jsonTag ].(map [string ]interface {})
126- insertError (subMap , path [1 :], errorTag )
21+ func SetSSEHeaders (c * gin.Context ) {
22+ c .Header ("Content-Type" , "text/event-stream" )
23+ c .Header ("Cache-Control" , "no-cache" )
24+ c .Header ("Connection" , "keep-alive" )
25+ // https://stackoverflow.com/questions/27898622/server-sent-events-stopped-work-after-enabling-ssl-on-proxy/27960243#27960243
26+ c .Header ("X-Accel-Buffering" , "no" )
12727}
0 commit comments