-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
111 lines (104 loc) · 2.92 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
109
110
111
package main
import (
"context"
"flag"
"github.com/btcsuite/btcd/rpcclient"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"log"
"net"
"om-rpc-tool/gateway"
"om-rpc-tool/signal"
"om-rpc-tool/toolrpc"
"om-rpc-tool/toolrpc/server"
"time"
)
func main() {
var omniHost = ""
var rpcUser = ""
var rpcPasswd = ""
var netType = ""
flag.Set("alsologtostderr", "true")
flag.StringVar(&omniHost, "omni_host", "localhost:8332", "ip:port")
flag.StringVar(&rpcUser, "rpc_user", "test", "")
flag.StringVar(&rpcPasswd, "rpc_passwd", "test", "")
flag.StringVar(&netType, "net_type", "", "")
flag.Parse()
shutdownChan, err := signal.Intercept()
// Connect to local namecoin core RPC server using HTTP POST mode.
connCfg := &rpcclient.ConnConfig{
Host: omniHost,
User: rpcUser,
Pass: rpcPasswd,
HTTPPostMode: true, // Namecoin core only supports HTTP POST mode
DisableTLS: true, // Namecoin core does not provide TLS by default
}
// Notice the notification parameter is nil since notifications are
// not supported in HTTP POST mode.
client, err := rpcclient.New(connCfg, nil)
if err != nil {
log.Fatal(err)
}
defer client.Shutdown()
if netType == "" {
log.Fatalln("miss net_type")
}
// Create a listener on TCP port
lis, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalln("Failed to listen:", err)
}
rserver := server.NewRpc(client, netType)
// Create a gRPC server object
s := grpc.NewServer()
// Attach the Greeter service to the server
toolrpc.RegisterToolsServer(s, rserver)
// Serve gRPC Server
log.Println("Serving gRPCs on 0.0.0.0:8080")
go func() {
log.Fatalln(s.Serve(lis))
}()
//gw server
opts := gateway.Options{
Addr: ":8090",
GRPCServer: gateway.Endpoint{
Network: "tcp",
Addr: "0.0.0.0:8080",
},
OpenAPIDir: "swagger",
}
go func() {
gateway.Run(context.Background(), opts, []func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error{toolrpc.RegisterToolsHandler})
}()
<-shutdownChan.ShutdownChannel()
//sleep 3 second, for some go routine which depend shutdownChan to complete database op.
time.Sleep(23 * time.Second)
log.Println("done shutdown")
//simpest
// Create a client connection to the gRPC server we just started
// This is where the gRPC-Gateway proxies the requests
//conn, err := grpc.DialContext(
// context.Background(),
// "0.0.0.0:8080",
// grpc.WithBlock(),
// grpc.WithTransportCredentials(insecure.NewCredentials()),
//)
//if err != nil {
// log.Fatalln("Failed to dial server:", err)
//}
//
//gwmux := runtime.NewServeMux()
//// Register Greeter
//err = toolrpc.RegisterToolsHandler(context.Background(), gwmux, conn)
//if err != nil {
// log.Fatalln("Failed to register gateway:", err)
//}
//
//gwServer := &http.Server{
// Addr: ":8090",
// Handler: gwmux,
//}
//
//log.Println("Serving gRPC-Gateway on http://0.0.0.0:8090")
//log.Fatalln(gwServer.ListenAndServe())
}