-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhttp_server.go
133 lines (118 loc) · 3.86 KB
/
http_server.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package zebra
import (
"github.com/rcrowley/go-metrics"
log "github.com/sirupsen/logrus"
"github.com/tietang/props/kvs"
"github.com/tietang/zebra/health"
"github.com/tietang/zebra/router"
"net/http"
"strconv"
"time"
)
const (
Version = "0.1"
Name = "Zebra"
)
//server
const (
//key
KEY_SERVER_DEBUG = "server.debug"
KEY_SERVER_PORT = "server.port"
KEY_SERVER_CONTEXT_PATH = "server.contextPath"
KEY_SERVER_MODE = "server.mode"
KEY_SERVER_FAVICON_ICO_PATH = "server.favicon.ico.path"
KEY_SERVER_GMS_ENABLED = "server.gms.enabled"
KEY_SERVER_GMS_DOMAIN = "server.gms.domain"
KEY_SERVER_GMS_PORT = "server.gms.port"
KEY_SERVER_GMS_DEBUG = "server.gms.debug"
//default value
DEFAULT_SERVER_DEBUG = "true"
DEFAULT_SERVER_PORT = 19001
DEFAULT_SERVER_MODE = "client"
DEFAULT_SERVER_FAVICON_ICO_PATH = "favicon.ico"
DEFAULT_SERVER_GMS_ENABLED = "true"
DEFAULT_SERVER_GMS_DOMAIN = ""
DEFAULT_SERVER_GMS_PORT = 17980
DEFAULT_SERVER_GMS_DEBUG = "true"
)
type HttpProxyServer struct {
dir string
configFile string
port int
conf kvs.ConfigSource
health *health.RootHealth
middleware Handlers
endpoints []Endpoint
contextPath string
//
stats *Stats
}
func NewHttpProxyServer(conf kvs.ConfigSource) *HttpProxyServer {
port := conf.GetIntDefault(KEY_SERVER_PORT, DEFAULT_SERVER_PORT)
rootHealth := &health.RootHealth{}
rootHealth.Status = health.STATUS_UP
rootHealth.Desc = "Gateway zebra"
rootHealth.Healths = make(map[string]*health.Health)
contextPath := conf.GetDefault(KEY_SERVER_CONTEXT_PATH, "/")
h := &HttpProxyServer{
conf: conf,
health: rootHealth,
port: port,
contextPath: contextPath,
}
h.Use(func(context *Context) error {
return nil
})
h.initEndpoints()
return h
}
func (h *HttpProxyServer) Run(addr string) {
server := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Addr: addr,
Handler: h,
}
go h.StartStatsServer()
// http.ListenAndServe(addr, h.proxyHandler)
log.Info("http proxy server address: ", addr)
server.ListenAndServe()
}
func (h *HttpProxyServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := NewContext(w, req, h.middleware)
ctx.handlers = h.middleware
// time.Sleep(time.Millisecond * 100)
url := req.URL
t := metrics.GetOrRegisterTimer(url.Path, router.UrlRegistry)
t.Time(func() {
if h.endpointExec(ctx.Method(), ctx.Path(), ctx) {
return
}
ctx.Next()
})
}
func (h *HttpProxyServer) DefaultRun() {
if h.port == 0 {
h.port = DEFAULT_SERVER_PORT
}
h.RunByPort(h.port)
}
func (h *HttpProxyServer) RunByPort(port int) {
h.Run(":" + strconv.Itoa(port))
}
// Use appends Handler(s) to the current Party's routes and child routes.
// If the current Party is the root, then it registers the middleware to all child Parties' routes too.
func (r *HttpProxyServer) Use(handlers ...Handler) {
r.middleware = append(r.middleware, handlers...)
}
//
//// UseGlobal registers Handler middleware to the beginning, prepends them instead of append
////
//// Use it when you want to add a global middleware to all parties, to all routes in all subdomains
//// It should be called right before Listen functions
//func (r *HttpProxyServer) UseGlobal(handlers ...Handler) {
// //for _, route := range r.Routes {
// // route.Handlers = append(handlers, route.Handlers...) // prepend the handlers
// //}
// r.middleware = append(handlers, r.middleware...) // set as middleware on the next routes too
//}