-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
79 lines (64 loc) · 1.7 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
package main
import (
"flag"
// "/dist/swaggerui"
"log"
"net/http"
"path"
"strings"
"github.com/elazarl/go-bindata-assetfs"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var (
realSrvAddr = flag.String("real_srv_addr", "localhost:8000", "endpoint of real server")
srvAddr = flag.String("srv_addr", ":8001", "endpoint of reverse proxy server")
)
func run() error {
var ctx context.Context
var cc context.CancelFunc
ctx = context.Background()
ctx, cc = context.WithCancel(ctx)
defer cc()
mux := http.NewServeMux()
mux.HandleFunc("/swagger/", serveSwaggerFile)
serveSwaggerUI(mux)
gwMux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
// todo gRpc server register
// ctx gwMux, *realSrvAddr opts
_ = opts
mux.Handle("/", gwMux)
log.Println("realSrvAddr:", *realSrvAddr, "srvAddr:", *srvAddr)
return http.ListenAndServe(*srvAddr, mux)
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}
func serveSwaggerFile(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, "swagger.json") {
log.Printf("Not Found: %s", r.URL.Path)
http.NotFound(w, r)
return
}
p := strings.TrimPrefix(r.URL.Path, "/swagger/")
p = path.Join("../proto", p)
log.Printf("Serving swagger-file: %s", p)
http.ServeFile(w, r, p)
}
func serveSwaggerUI(mux *http.ServeMux) {
fileServer := http.FileServer(&assetfs.AssetFS{
// from dist/swaggerui
Asset: nil, //swaggerui.Asset,
AssetDir: nil, //swaggerui.AssetDir,
Prefix: "path/swaggerui",
})
prefix := "/swagger-ui/"
mux.Handle(prefix, http.StripPrefix(prefix, fileServer))
}