forked from tronprotocol/grpc-gateway
/
main.go
88 lines (70 loc) · 2.19 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
package main
import (
"flag"
"net/http"
"strconv"
"strings"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"golang.org/x/net/context"
"google.golang.org/grpc"
gw "github.com/OpenStars/grpc-gateway/api"
"fmt"
)
var (
port = flag.Int("port",50051, "port of your tron grpc service" )
host = flag.String("host", "localhost", "host of your tron grpc service")
listen = flag.Int("listen", 18890, "the port that http server listen")
)
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", "*")
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","X-Requested-With","token"}
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE","TRACE","OPTIONS","PATCH"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
orgin:=[]string{"*"}
w.Header().Set("Access-Control-Allow-Origin", strings.Join(orgin, ","));
glog.Infof("preflight request for %s", r.URL.Path)
return
}
func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
grpcEndpoint := *host + ":" + strconv.Itoa(*port)
opts := []grpc.DialOption{grpc.WithInsecure()}
fmt.Printf("grpc server: %s\n", grpcEndpoint)
fmt.Printf("http port : %d\n", *listen)
err := gw.RegisterWalletHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
err = gw.RegisterWalletSolidityHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
err = gw.RegisterWalletExtensionHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
return http.ListenAndServe(":" + strconv.Itoa(*listen), allowCORS(mux))
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}