-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv_core.go
123 lines (117 loc) · 3.76 KB
/
srv_core.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
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"flag"
"github.com/bootapp/rest-grpc-oauth2/auth"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"net"
"net/http"
"os"
"os/signal"
"srv-core/oauth"
pb "srv-core/proto"
"srv-core/server"
"srv-core/settings"
"srv-core/utils"
"strings"
"time"
)
var (
grpcEndpoint = flag.String("grpc_endpoint", ":9090", "The endpoint of the core grpc service")
httpEndpoint = flag.String("http_endpoint", ":8090", "The endpoint of the core restful service")
)
func main() {
_ = flag.Set("alsologtostderr", "true")
flag.Parse()
defer glog.Flush()
ctx, cancel := context.WithCancel(context.Background())
//====== initialize auth client
authenticator :=auth.GetInstance()
//====== initialize oauth server
oauthServer := oauth.NewPassOAuthServer()
//====== read configs and listen changes from apollo
server.ApolloConfig(ctx, false, oauthServer, authenticator)
// "redis://:qwerty@localhost:6379/1"
utils.InitRedis(settings.RedisAddr)
go func() {
defer cancel()
_ = gwRun(ctx, *httpEndpoint, *grpcEndpoint)
}()
_ = grpcRun(ctx, *grpcEndpoint)
}
func grpcRun(ctx context.Context, grpcEndpoint string) error {
l, err := net.Listen("tcp", grpcEndpoint)
if err != nil {
return err
}
grpcServer := grpc.NewServer()
srvCoreUserSrv := server.NewSrvCoreUserServiceServer()
srvCoreSecuritySrv := server.NewSecurityServer()
srvCoreSysSrv := server.NewSysServer()
srvDataStoreSrv := server.NewDataStoreServer()
pb.RegisterSrvCoreUserServiceServer(grpcServer, srvCoreUserSrv)
pb.RegisterSrvSecurityServiceServer(grpcServer, srvCoreSecuritySrv)
pb.RegisterSrvCoreSysServiceServer(grpcServer, srvCoreSysSrv)
pb.RegisterSrvDataStoreServiceServer(grpcServer, srvDataStoreSrv)
go func() {
defer grpcServer.GracefulStop()
<-ctx.Done()
glog.Info("grpc server shutting down...")
}()
glog.Info("grpc server running...")
return grpcServer.Serve(l)
}
func routedGatewayHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.RequestURI, "/api") {
oauth.ServeOAuthHTTP(w, r)
} else if strings.HasPrefix(r.RequestURI, "/rpc") {
h.ServeHTTP(w, r)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
})
}
func gwRun(ctx context.Context, httpEndpoint string, grpcEndpoint string) error {
//ctx, cancel := context.WithCancel(context.Background())
mux := runtime.NewServeMux(
runtime.WithForwardResponseOption(auth.GatewayResponseCookieAnnotator),
runtime.WithMetadata(auth.GatewayRequestCookieParser))
opts := []grpc.DialOption{grpc.WithInsecure()}
if err := pb.RegisterSrvCoreUserServiceHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts); err != nil {
glog.Fatal("failed to start rest gateway: %v", err)
return err
}
if err := pb.RegisterSrvSecurityServiceHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts); err != nil {
glog.Fatal("failed to start rest gateway: %v", err)
return err
}
if err := pb.RegisterSrvCoreSysServiceHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts); err != nil {
glog.Fatal("failed to start rest gateway: %v", err)
return err
}
if err := pb.RegisterSrvDataStoreServiceHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts); err != nil {
glog.Fatal("failed to start rest gateway: %v", err)
return err
}
srv := &http.Server {
Addr: httpEndpoint,
Handler: routedGatewayHandler(mux),
}
// graceful shutdown
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<- c
glog.Info("rest gateway shutting down...")
_, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err:= srv.Shutdown(ctx); err != nil {
glog.Fatalf("failed to shutdown rest gateway %v", err)
}
}()
glog.Info("restful gateway running...")
return srv.ListenAndServe()
}