-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
108 lines (87 loc) · 2.72 KB
/
main.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
package main
import (
"flag"
"net/http"
"path"
"strings"
"google.golang.org/grpc"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
gw "github.com/rephus/grpc-gateway-example/template"
"golang.org/x/net/context"
)
const (
grpcPort = "10000"
)
var (
getEndpoint = flag.String("get", "localhost:"+grpcPort, "endpoint of YourService")
postEndpoint = flag.String("post", "localhost:"+grpcPort, "endpoint of YourService")
swaggerDir = flag.String("swagger_dir", "template", "path to the directory which contains swagger definitions")
)
func newGateway(ctx context.Context, opts ...runtime.ServeMuxOption) (http.Handler, error) {
mux := runtime.NewServeMux(opts...)
dialOpts := []grpc.DialOption{grpc.WithInsecure()}
err := gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, *getEndpoint, dialOpts)
if err != nil {
return nil, err
}
err = gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, *postEndpoint, dialOpts)
if err != nil {
return nil, err
}
return mux, nil
}
func serveSwagger(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, ".swagger.json") {
glog.Errorf("Swagger JSON not Found: %s", r.URL.Path)
http.NotFound(w, r)
return
}
glog.Infof("Serving %s", r.URL.Path)
p := strings.TrimPrefix(r.URL.Path, "/swagger/")
p = path.Join(*swaggerDir, p)
http.ServeFile(w, r, p)
}
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
}
// allowCORS allows Cross Origin Resoruce Sharing from any origin.
// Don't do this without consideration in production systems.
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 Run(address string, opts ...runtime.ServeMuxOption) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
//mux := runtime.NewServeMux()
mux := http.NewServeMux()
mux.HandleFunc("/swagger/", serveSwagger)
gw, err := newGateway(ctx, opts...)
if err != nil {
return err
}
mux.Handle("/", gw)
return http.ListenAndServe(address, allowCORS(mux))
}
func main() {
flag.Parse()
defer glog.Flush()
if err := Run(":8080"); err != nil {
glog.Fatal(err)
}
}