@@ -3,14 +3,27 @@ package util
3
3
import (
4
4
"github.com/acmestack/envcd/internal/pkg/context"
5
5
"github.com/acmestack/envcd/pkg/entity/data"
6
+ "github.com/gin-gonic/gin"
7
+ "io/ioutil"
6
8
)
7
9
8
10
// BuildContext build plugin context
9
11
// @param params params
10
12
// @return *context.Context context
11
13
// @return error error
12
- func BuildContext (params interface {}) (* context.Context , error ) {
13
- // TODO build context
14
+ func BuildContext (ginCtx * gin.Context ) (* context.Context , error ) {
15
+ ctx := & context.Context {
16
+ Uri : ginCtx .Request .RequestURI ,
17
+ Method : ginCtx .Request .Method ,
18
+ Headers : buildContextHeaders (ginCtx ),
19
+ ContentType : ginCtx .ContentType (),
20
+ Cookies : buildContextCookies (ginCtx ),
21
+ Body : buildRequestBody (ginCtx ),
22
+ HttpRequest : ginCtx .Request ,
23
+ }
24
+ if ctx != nil {
25
+ return ctx , nil
26
+ }
14
27
return nil , nil
15
28
}
16
29
@@ -22,3 +35,36 @@ func ParseContext(ctx *context.Context) (*data.EnvcdData, error) {
22
35
// TODO parse context to envcd data
23
36
return nil , nil
24
37
}
38
+
39
+ // buildContextHeaders build plugin context headers
40
+ // @param ginCtx gin context
41
+ // @return map[string]interface{} ret
42
+ func buildContextHeaders (ginCtx * gin.Context ) map [string ]interface {} {
43
+ maps := make (map [string ]interface {})
44
+ for k , v := range ginCtx .Request .Header {
45
+ maps [k ] = v
46
+ }
47
+ return maps
48
+ }
49
+
50
+ // buildContextCookies build plugin context cookies
51
+ // @param ginCtx gin context
52
+ // @return map[string]interface{} ret
53
+ func buildContextCookies (ginCtx * gin.Context ) map [string ]interface {} {
54
+ maps := make (map [string ]interface {})
55
+ for k , v := range ginCtx .Request .Cookies () {
56
+ maps [string (rune (k ))] = v
57
+ }
58
+ return maps
59
+ }
60
+
61
+ // buildRequestBody build request body
62
+ // @param ginCtx gin context
63
+ // @return string request body
64
+ func buildRequestBody (ginCtx * gin.Context ) string {
65
+ all , err := ioutil .ReadAll (ginCtx .Request .Body )
66
+ if err != nil {
67
+ return ""
68
+ }
69
+ return string (all )
70
+ }
0 commit comments