Skip to content

Commit 2607630

Browse files
authored
[type:feat] build context (#84)
1 parent 3d1e5b6 commit 2607630

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

internal/core/openapi/auth.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package openapi
1919

2020
import (
2121
"fmt"
22-
2322
"github.com/acmestack/envcd/internal/core/plugin"
2423
"github.com/acmestack/envcd/internal/core/storage/dao"
2524
"github.com/acmestack/envcd/internal/pkg/context"
2625
"github.com/acmestack/envcd/internal/pkg/entity"
26+
"github.com/acmestack/envcd/internal/util"
2727
"github.com/acmestack/envcd/pkg/entity/data"
2828
"github.com/acmestack/godkits/gox/errorsx"
2929
"github.com/acmestack/godkits/gox/stringsx"
@@ -42,12 +42,13 @@ func (openapi *Openapi) login(ctx *gin.Context) {
4242
}
4343

4444
func (openapi *Openapi) logout(ctx *gin.Context) {
45-
c := &context.Context{Action: func() (*data.EnvcdResult, error) {
45+
c, _ := util.BuildContext(ctx)
46+
c.Action = func() (*data.EnvcdResult, error) {
4647
fmt.Println("hello world")
4748
// UserDao.save(),
4849
// LogDao.save()
4950
return nil, errorsx.Err("test error")
50-
}}
51+
}
5152
if ret, err := plugin.NewChain(openapi.executors).Execute(c); err != nil {
5253
fmt.Printf("ret = %v, error = %v", ret, err)
5354
}

internal/util/contextutil.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ package util
33
import (
44
"github.com/acmestack/envcd/internal/pkg/context"
55
"github.com/acmestack/envcd/pkg/entity/data"
6+
"github.com/gin-gonic/gin"
7+
"io/ioutil"
68
)
79

810
// BuildContext build plugin context
911
// @param params params
1012
// @return *context.Context context
1113
// @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+
}
1427
return nil, nil
1528
}
1629

@@ -22,3 +35,36 @@ func ParseContext(ctx *context.Context) (*data.EnvcdData, error) {
2235
// TODO parse context to envcd data
2336
return nil, nil
2437
}
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

Comments
 (0)