-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (113 loc) · 3.15 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"embed"
"flag"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/happycrud/crud-grpc-gin-micro-service-example/api"
"github.com/happycrud/crud-grpc-gin-micro-service-example/crud"
"github.com/happycrud/crud-grpc-gin-micro-service-example/service"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/rs/cors"
"github.com/gin-gonic/gin"
"github.com/happycrud/crud/xsql"
"github.com/soheilhy/cmux"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
var port int
var dsn string
const (
appID = "crud-example.user"
)
//go:embed web/dist
var content embed.FS
func init() {
flag.IntVar(&port, "port", 9000, "server listen on port")
flag.StringVar(&dsn, "dsn", "root:123456@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local", "mysql dsn github.com/happycrud/crud-grpc-gin-micro-service-example(root:123456@tcp(127.0.0.1:3306)/github.com/happycrud/crud-grpc-gin-micro-service-example?parseTime=true)")
}
func main() {
flag.Parse()
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
panic(err)
}
svr := grpc.NewServer(grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(service.NoAuthFunc)))
client, err := crud.NewClient(&xsql.Config{
DSN: dsn,
ReadDSN: []string{dsn},
Active: 20,
Idle: 10,
IdleTimeout: time.Hour * 4,
QueryTimeout: time.Second,
ExecTimeout: time.Second,
})
if err != nil {
panic(err)
}
u := &service.UserServiceImpl{Client: client}
al := &service.AllTypeTableServiceImpl{Client: client}
api.RegisterAllTypeTableServiceServer(svr, al)
api.RegisterUserServiceServer(svr, u)
grpc_health_v1.RegisterHealthServer(svr, health.NewServer())
reflection.Register(svr)
m := cmux.New(l)
e := gin.Default()
var staticFS = fs.FS(content)
htmlContent, err := fs.Sub(staticFS, "web/dist")
if err != nil {
log.Fatal(err)
}
e.StaticFS("/", http.FS(htmlContent))
grpcL := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldPrefixSendSettings("content-type", "application/grpc"))
httpL := m.Match(cmux.HTTP1Fast())
hsvr := &http.Server{}
wrappedGrpc := grpcweb.WrapServer(svr)
h := http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if wrappedGrpc.IsGrpcWebRequest(req) {
wrappedGrpc.ServeHTTP(resp, req)
return
}
// Fall back to other servers.
e.ServeHTTP(resp, req)
})
corsh := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
AllowedHeaders: []string{"*"},
Debug: false,
}).Handler(h)
hsvr.Handler = corsh
go func() {
go svr.Serve(grpcL)
go hsvr.Serve(httpL)
m.Serve()
}()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
//discovery.DeleteRegister(context.Background(), instanceID)
hsvr.Shutdown(context.Background())
svr.GracefulStop()
m.Close()
return
case syscall.SIGHUP:
default:
return
}
}
}