/
gkcontext.go
79 lines (72 loc) · 1.75 KB
/
gkcontext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package goktrl
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/abiosoft/ishell/v2"
"github.com/gin-gonic/gin"
)
type ContextType int
const (
ContextClient ContextType = 1 // 客户端
ContextServer ContextType = 2 // 服务端
ArgsFormatStr string = "args%sargs"
)
type Context struct {
*gin.Context
ShellContext *ishell.Context
Type ContextType
Options KtrlOpt
Args []string
Parser *ParserPlus
Table *KtrlTable
KtrlPath string
Client *KtrlClient
DefaultSocket string
ShellCmdName string
Result []byte
}
func (that *Context) GetResult(sockName ...string) ([]byte, error) {
sName := that.DefaultSocket
if len(sockName) > 0 && len(sockName[0]) > 0 {
sName = sockName[0]
}
params := make(map[string]string)
if that.Type == ContextClient {
params = that.Parser.Params
params[fmt.Sprintf(ArgsFormatStr, that.ShellCmdName)] = strings.Join(that.Args, ",")
} else {
// 服务端继续转发请求
for k := range that.Request.URL.Query() {
params[k] = that.Query(k)
}
that.KtrlPath = that.Request.URL.Path
}
if that.Client == nil {
that.Client = NewKtrlClient()
}
return that.Client.GetResult(that.KtrlPath, params, sName)
}
func (that *Context) Send(content interface{}, code ...int) {
if that.Context != nil {
statusCode := http.StatusOK
if len(code) > 0 {
statusCode = code[0]
}
switch r := content.(type) {
case string:
that.Context.String(statusCode, r)
case []byte:
that.Context.String(statusCode, string(r))
default:
res, err := json.Marshal(content)
if err != nil {
fmt.Println(err)
that.Context.String(http.StatusInternalServerError, err.Error())
return
}
that.Context.String(statusCode, string(res))
}
}
}