-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
102 lines (91 loc) · 2.54 KB
/
gateway.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
package gateway
import (
"context"
"fmt"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rakyll/statik/fs"
"github.com/shijting/web/inits/config"
"github.com/shijting/web/protos"
_ "github.com/shijting/web/statik"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"log"
"mime"
"net/http"
"strings"
)
func RunGrpcGwServer(quit chan error) {
errorOption := runtime.WithErrorHandler(gatewayErrorHandler)
mux := runtime.NewServeMux(errorOption)
//mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
grpcPort := config.Conf.GrpcServerConfig.Port
ctx := context.Background()
conn, err := grpc.DialContext(
ctx,
fmt.Sprintf(":%d", grpcPort),
opts...,
)
if err != nil {
quit <- fmt.Errorf("failed to dial server: %w", err)
}
err = protos.RegisterUserServiceHandler(ctx, mux, conn)
if err != nil {
quit <- err
}
oa := getOpenAPIHandler(quit)
grpcGwPort := config.Conf.GrpcGwServerConfig.Port
if grpcGwPort == 0 {
grpcGwPort = 8001
}
m := NewMiddleware()
m.Use(timeMiddleware)
m.Use(logMiddleware)
// start timeMiddleware -> start logMiddleware ->content -> end logMiddleware ->end timeMiddleware
gwServer := &http.Server{
Addr: fmt.Sprintf(":%d", grpcGwPort),
Handler: m.middleWareHandler(handler(mux, oa)),
}
log.Println("Serving gRPC-gateway on port:",grpcGwPort)
err = gwServer.ListenAndServe()
if err !=nil {
quit <- err
}
}
func handler(mux *runtime.ServeMux, oa http.Handler ) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 所有的对外接口都以/api开头
if strings.HasPrefix(r.URL.Path, "/api") {
fmt.Println("content")
mux.ServeHTTP(w, r)
return
}
// swagger OpenAPI文档服务
oa.ServeHTTP(w, r)
})
}
func (r *Middleware)middleWareHandler(h http.Handler) http.Handler {
var mergedHandler = h
for i := len(r.middlewareChain) - 1; i >= 0; i-- {
mergedHandler = r.middlewareChain[i](mergedHandler)
}
return mergedHandler
}
func getOpenAPIHandler(quit chan error) http.Handler {
err := mime.AddExtensionType(".svg", "image/svg+xml")
if err != nil {
quit <- err
}
statikFS, err := fs.New()
if err != nil {
quit <- err
}
return http.FileServer(statikFS)
}
func gatewayErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
st := status.Convert(err)
httpStatus := runtime.HTTPStatusFromCode(st.Code())
w.WriteHeader(httpStatus)
//w.Header().Set("Content-Type", "application/json")
w.Write([]byte(st.Message()))
}