-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
102 lines (86 loc) · 2.62 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
/*
Copyright (c) 2023 Schubert Anselme <schubert@anselm.es>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package internal
import (
"context"
"fmt"
"log"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
api "github.com/sanselme/helloworld/api/v1alpha1"
"github.com/sanselme/helloworld/pkg/handler"
"github.com/spf13/cobra"
"github.com/anselmes/util/pkg/host"
"github.com/anselmes/util/pkg/util"
"google.golang.org/grpc"
)
type (
GatewayOption func(*gateway)
gateway struct {
Endpoint host.Endpoint
Service host.Endpoint
OpenAPIDir string
}
)
func (gw *gateway) RunGateway(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
svc := fmt.Sprintf("%s:%d", gw.Service.Address, gw.Service.Port)
// Create a client connection to the gRPC server we just started
// This is where the gRPC-Gateway proxies the requests
conn, err := Dial(ctx, svc, grpc.WithBlock())
if err != nil {
return err
}
go func() {
<-ctx.Done()
if err := conn.Close(); err != nil {
util.CheckErr(err)
}
}()
// Register generated routes
rt := runtime.NewServeMux()
err = api.RegisterGreeterServiceHandler(ctx, rt, conn)
if err != nil {
return err
}
// Register custom routes
mux := http.NewServeMux()
mux.Handle("/", handler.AllowCORS(rt))
mux.HandleFunc("/openapiv2/", handler.OpenAPIServer(gw.OpenAPIDir))
mux.HandleFunc("/healthz", handler.HealthServer(conn))
// Start HTTP server (and proxy calls to gRPC server endpoint)
uri := fmt.Sprintf("%s:%d", gw.Endpoint.Address, gw.Endpoint.Port)
s := &http.Server{Addr: uri, Handler: mux}
go func() {
<-ctx.Done()
log.Println("shutting down...")
if err := s.Shutdown(context.Background()); err != nil {
util.CheckErr(err)
}
}()
// Start HTTP server (and proxy calls to gRPC server endpoint)
log.Println("listening on", uri)
if err := s.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
}
func NewGateway(opts ...GatewayOption) *gateway {
gw := &gateway{}
for _, op := range opts {
op(gw)
}
return gw
}