forked from gtfierro/wave
-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpserve.go
74 lines (63 loc) · 1.82 KB
/
httpserve.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
package eapi
import (
"context"
"fmt"
"net/http"
"path"
"strings"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/immesys/wave/eapi/pb"
"google.golang.org/grpc"
)
func allowCORS(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
preflightHandler(w, r)
return
}
}
h.ServeHTTP(w, r)
})
}
func preflightHandler(w http.ResponseWriter, r *http.Request) {
headers := []string{"Content-Type", "Accept"}
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
glog.Infof("preflight request for %s", r.URL.Path)
return
}
func serveSwagger(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, ".swagger.json") {
http.NotFound(w, r)
return
}
p := strings.TrimPrefix(r.URL.Path, "/swagger/")
p = path.Join("swagger", p)
http.ServeFile(w, r, p)
}
func runHTTPserver(dialaddr string, listenaddr string) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := http.NewServeMux()
gw := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := pb.RegisterWAVEHandlerFromEndpoint(ctx, gw, dialaddr, opts)
if err != nil {
panic(err)
}
mux.HandleFunc("/swagger/", serveSwagger)
mux.Handle("/", gw)
s := &http.Server{
Addr: listenaddr,
Handler: allowCORS(mux),
}
if err := s.ListenAndServe(); err != http.ErrServerClosed {
fmt.Printf("HTTP server failed to listen: %v\n", err)
panic(err)
}
}