forked from micro/micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
340 lines (291 loc) · 7.5 KB
/
web.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package web
import (
"crypto/tls"
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/http/httputil"
"regexp"
"sort"
"strings"
"time"
log "github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/micro/cli"
"github.com/micro/go-micro"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"github.com/micro/micro/internal/handler"
"github.com/micro/micro/internal/server"
"github.com/serenize/snaker"
)
var (
re = regexp.MustCompile("^[a-zA-Z0-9]+$")
// Default address to bind to
Address = ":8082"
// The namespace to serve
// Example:
// Namespace + /[Service]/foo/bar
// Host: Namespace.Service Endpoint: /foo/bar
Namespace = "go.micro.web"
// Base path sent to web service.
// This is stripped from the request path
// Allows the web service to define absolute paths
BasePathHeader = "X-Micro-Web-Base-Path"
CORS = map[string]bool{"*": true}
)
type srv struct {
*mux.Router
}
func (s *srv) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); CORS[origin] {
w.Header().Set("Access-Control-Allow-Origin", origin)
} else if len(origin) > 0 && CORS["*"] {
w.Header().Set("Access-Control-Allow-Origin", origin)
}
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
w.Header().Set("Access-Control-Allow-Credentials", "true")
if r.Method == "OPTIONS" {
return
}
s.Router.ServeHTTP(w, r)
}
func (s *srv) proxy() http.Handler {
sel := selector.NewSelector(
selector.Registry((*cmd.DefaultOptions().Registry)),
)
director := func(r *http.Request) {
kill := func() {
r.URL.Host = ""
r.URL.Path = ""
r.URL.Scheme = ""
r.Host = ""
r.RequestURI = ""
}
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 2 {
kill()
return
}
if !re.MatchString(parts[1]) {
kill()
return
}
next, err := sel.Select(Namespace + "." + parts[1])
if err != nil {
kill()
return
}
s, err := next()
if err != nil {
kill()
return
}
r.Header.Set(BasePathHeader, "/"+parts[1])
r.URL.Host = fmt.Sprintf("%s:%d", s.Address, s.Port)
r.URL.Path = "/" + strings.Join(parts[2:], "/")
r.URL.Scheme = "http"
}
return &proxy{
Default: &httputil.ReverseProxy{Director: director},
Director: director,
}
/*
return &httputil.ReverseProxy{
Director: director,
}
*/
}
func format(v *registry.Value) string {
if v == nil || len(v.Values) == 0 {
return "{}"
}
var f []string
for _, k := range v.Values {
f = append(f, formatEndpoint(k, 0))
}
return fmt.Sprintf("{\n%s}", strings.Join(f, ""))
}
func formatEndpoint(v *registry.Value, r int) string {
// default format is tabbed plus the value plus new line
fparts := []string{"", "%s %s", "\n"}
for i := 0; i < r+1; i++ {
fparts[0] += "\t"
}
// its just a primitive of sorts so return
if len(v.Values) == 0 {
return fmt.Sprintf(strings.Join(fparts, ""), snaker.CamelToSnake(v.Name), v.Type)
}
// this thing has more things, it's complex
fparts[1] += " {"
vals := []interface{}{snaker.CamelToSnake(v.Name), v.Type}
for _, val := range v.Values {
fparts = append(fparts, "%s")
vals = append(vals, formatEndpoint(val, r+1))
}
// at the end
l := len(fparts) - 1
for i := 0; i < r+1; i++ {
fparts[l] += "\t"
}
fparts = append(fparts, "}\n")
return fmt.Sprintf(strings.Join(fparts, ""), vals...)
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
return
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
services, err := (*cmd.DefaultOptions().Registry).ListServices()
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
var webServices []string
for _, s := range services {
if strings.Index(s.Name, Namespace) == 0 && len(strings.TrimPrefix(s.Name, Namespace)) > 0 {
webServices = append(webServices, strings.Replace(s.Name, Namespace+".", "", 1))
}
}
sort.Strings(webServices)
type templateData struct {
HasWebServices bool
WebServices []string
}
data := templateData{len(webServices) > 0, webServices}
render(w, r, indexTemplate, data)
}
func registryHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
svc := r.Form.Get("service")
if len(svc) > 0 {
s, err := (*cmd.DefaultOptions().Registry).GetService(svc)
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
if len(s) == 0 {
http.Error(w, "Not found", 404)
return
}
if r.Header.Get("Content-Type") == "application/json" {
b, err := json.Marshal(map[string]interface{}{
"services": s,
})
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
render(w, r, serviceTemplate, s)
return
}
services, err := (*cmd.DefaultOptions().Registry).ListServices()
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
sort.Sort(sortedServices{services})
if r.Header.Get("Content-Type") == "application/json" {
b, err := json.Marshal(map[string]interface{}{
"services": services,
})
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
render(w, r, registryTemplate, services)
}
func queryHandler(w http.ResponseWriter, r *http.Request) {
render(w, r, queryTemplate, nil)
}
func render(w http.ResponseWriter, r *http.Request, tmpl string, data interface{}) {
t, err := template.New("template").Funcs(template.FuncMap{
"format": format,
}).Parse(layoutTemplate)
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
t, err = t.Parse(tmpl)
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
if err := t.ExecuteTemplate(w, "layout", data); err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
}
}
func run(ctx *cli.Context) {
r := mux.NewRouter()
s := &srv{r}
s.HandleFunc("/registry", registryHandler)
s.HandleFunc("/rpc", handler.RPC)
s.HandleFunc("/query", queryHandler)
s.HandleFunc("/favicon.ico", faviconHandler)
s.PathPrefix("/{service:[a-zA-Z0-9]+}").Handler(s.proxy())
s.HandleFunc("/", indexHandler)
var opts []server.Option
if ctx.GlobalBool("enable_tls") {
cert := ctx.GlobalString("tls_cert_file")
key := ctx.GlobalString("tls_key_file")
if len(cert) > 0 && len(key) > 0 {
certs, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
fmt.Println(err.Error())
return
}
config := &tls.Config{
Certificates: []tls.Certificate{certs},
}
opts = append(opts, server.EnableTLS(true))
opts = append(opts, server.TLSConfig(config))
} else {
fmt.Println("Enable TLS specified without certificate and key files")
return
}
}
srv := server.NewServer(Address)
srv.Init(opts...)
srv.Handle("/", s)
// Initialise Server
service := micro.NewService(
micro.Name("go.micro.web"),
micro.RegisterTTL(
time.Duration(ctx.GlobalInt("register_ttl"))*time.Second,
),
micro.RegisterInterval(
time.Duration(ctx.GlobalInt("register_interval"))*time.Second,
),
)
if err := srv.Start(); err != nil {
log.Fatal(err)
}
// Run server
if err := service.Run(); err != nil {
log.Fatal(err)
}
if err := srv.Stop(); err != nil {
log.Fatal(err)
}
}
func Commands() []cli.Command {
return []cli.Command{
{
Name: "web",
Usage: "Run the micro web app",
Action: func(c *cli.Context) {
run(c)
},
},
}
}