/
dispatch.go
82 lines (64 loc) · 1.62 KB
/
dispatch.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
80
81
82
package console
import (
"encoding/json"
"fmt"
"github.com/aosfather/myway/runtime"
"github.com/valyala/fasthttp"
)
/**
控制台服务入口
*/
const (
_DEFAULT_PORT = 9990
)
type ConsoleDispatch struct {
port int
server *fasthttp.Server
dispatch *runtime.SimpleDispatch
}
func (this *ConsoleDispatch) Init(p int) {
this.port = p
this.dispatch = &runtime.SimpleDispatch{}
this.dispatch.Init()
}
func (this *ConsoleDispatch) RegisterHandle(url string, h ConsoleHandler) {
if url != "" && h != nil {
this.dispatch.AddUrl(url, h)
}
}
func (this *ConsoleDispatch) Start() {
this.server = &fasthttp.Server{Handler: this.ServeHTTP}
if this.port == 0 {
this.port = _DEFAULT_PORT
}
addr := fmt.Sprintf("0.0.0.0:%d", this.port)
this.server.ListenAndServe(addr)
}
func (this *ConsoleDispatch) ServeHTTP(ctx *fasthttp.RequestCtx) {
//获取访问的url
url := string(ctx.Request.URI().RequestURI())
//通过dispatch,获取api的定义
api := this.dispatch.GetUrl(url)
if api == nil { //不存在的时候的处理
ctx.Response.SetBodyString("the url not found!")
return
}
//检查访问token
//执行对应请求处理
h := api.(ConsoleHandler)
response := h(nil)
ctx.WriteString(fmt.Sprintf("%d,%s,%s", response.Status, response.Code, response.Msg))
}
//控制请求
type ConsoleRequest struct {
Token string `json:"access_token"`
Data json.RawMessage `json:"data"`
}
//控制结果
type ConsoleResponse struct {
Status byte `json:"status"`
Code string `json:"code"`
Msg string `json:"message"`
}
//处理的hanler
type ConsoleHandler func(request *ConsoleRequest) ConsoleResponse