-
Notifications
You must be signed in to change notification settings - Fork 202
/
http-handler.go
84 lines (70 loc) · 2.06 KB
/
http-handler.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
83
84
package http_router
import (
"net/http"
"time"
"github.com/eolinker/apinto/entries/ctx_key"
http_service "github.com/eolinker/apinto/node/http-context"
http_complete "github.com/eolinker/apinto/drivers/router/http-router/http-complete"
"github.com/eolinker/apinto/service"
"github.com/eolinker/eosc/eocontext"
http_context "github.com/eolinker/eosc/eocontext/http-context"
)
var completeCaller = http_complete.NewHttpCompleteCaller()
type httpHandler struct {
completeHandler eocontext.CompleteHandler
routerName string
routerId string
serviceName string
finisher eocontext.FinishHandler
service service.IService
filters eocontext.IChainPro
disable bool
websocket bool
labels map[string]string
retry int
timeout time.Duration
}
func (h *httpHandler) Serve(ctx eocontext.EoContext) {
httpContext, err := http_context.Assert(ctx)
if err != nil {
return
}
if h.disable {
httpContext.Response().SetStatus(http.StatusNotFound, "")
httpContext.Response().SetBody([]byte("router disable"))
httpContext.FastFinish()
return
}
if h.websocket {
wsCtx, err := http_service.NewWebsocketContext(httpContext)
if err != nil {
httpContext.Response().SetStatus(http.StatusInternalServerError, "")
httpContext.Response().SetBody([]byte(err.Error()))
httpContext.FastFinish()
return
}
ctx = wsCtx
}
for key, value := range h.labels {
// 设置标签
ctx.SetLabel(key, value)
}
//set retry timeout
ctx.WithValue(ctx_key.CtxKeyRetry, h.retry)
ctx.WithValue(ctx_key.CtxKeyTimeout, h.timeout)
//Set Label
ctx.SetLabel("api", h.routerName)
ctx.SetLabel("api_id", h.routerId)
ctx.SetLabel("service", h.serviceName)
if h.service != nil {
ctx.SetLabel("service_id", h.service.Id())
}
ctx.SetLabel("method", httpContext.Request().Method())
ctx.SetLabel("path", httpContext.Request().URI().RequestURI())
ctx.SetLabel("ip", httpContext.Request().RealIp())
ctx.SetCompleteHandler(h.completeHandler)
ctx.SetBalance(h.service)
ctx.SetUpstreamHostHandler(h.service)
ctx.SetFinish(h.finisher)
h.filters.Chain(ctx, completeCaller)
}