-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
91 lines (75 loc) · 2.28 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
package gateway
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/gookit/color"
"github.com/goravel/framework/contracts/config"
contractsgrpc "github.com/goravel/framework/contracts/grpc"
contractshttp "github.com/goravel/framework/contracts/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
type Handler func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error
type Gateway struct {
config config.Config
grpc contractsgrpc.Grpc
}
func NewGateway(config config.Config, grpc contractsgrpc.Grpc) *Gateway {
return &Gateway{
config: config,
grpc: grpc,
}
}
func (r *Gateway) Run(serveMux ...*runtime.ServeMux) error {
host := r.config.GetString("gateway.host")
port := r.config.GetString("gateway.port")
if host == "" || port == "" {
return errors.New("please initialize GATEWAY_HOST and GATEWAY_PORT")
}
mux := runtime.NewServeMux()
if len(serveMux) > 0 {
mux = serveMux[0]
}
connections := make(map[string]*grpc.ClientConn)
clients := r.config.Get("grpc.clients").(map[string]any)
for name, params := range clients {
if name == "" {
return errors.New("gRPC client name is required")
}
if _, exist := connections[name]; !exist {
connection, err := r.grpc.Client(context.Background(), name)
if err != nil {
return fmt.Errorf("init gRPC %s client failed: %v", name, err)
}
connections[name] = connection
}
handlers, exist := params.(map[string]any)["handlers"]
if !exist {
return fmt.Errorf("gRPC %s handlers is required", name)
}
for _, handler := range handlers.([]Handler) {
if err := handler(context.Background(), mux, connections[name]); err != nil {
return fmt.Errorf("register gRPC %s handler failed: %v", name, err)
}
}
}
addr := fmt.Sprintf("%s:%s", host, port)
server := &http.Server{
Addr: addr,
Handler: mux,
}
color.Greenln("[Gateway] Listening and serving Gateway on " + addr)
if err := server.ListenAndServe(); err != nil {
return fmt.Errorf("HTTP listen failed: %v", err)
}
return nil
}
func Inject[V NumberOrString](ctx contractshttp.Context, key string, value V) {
if injectValue, exist := ctx.Value(InjectKey).(map[string]any); exist {
injectValue[key] = value
} else {
ctx.WithValue(InjectKey, map[string]any{key: value})
}
}