/
service.go
65 lines (56 loc) · 1.91 KB
/
service.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
package builder
import (
"context"
"net"
"net/http"
"time"
"github.com/aserto-dev/certs"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
type ServiceInterface interface {
Start(context.Context) error
Stop(context.Context) error
}
type GRPCRegistrations func(server *grpc.Server)
type HandlerRegistrations func(ctx context.Context, mux *runtime.ServeMux, grpcEndpoint string, opts []grpc.DialOption) error
type Server struct {
Config *API
Server *grpc.Server
Listener net.Listener
Gateway Gateway
Started chan bool
Cleanup []func()
}
type Gateway struct {
Server *http.Server
Mux *http.ServeMux
Certs *certs.TLSCredsConfig
}
type API struct {
Needs []string `json:"needs"`
GRPC struct {
FQDN string `json:"fqdn"`
ListenAddress string `json:"listen_address"`
// Default connection timeout is 120 seconds
// https://godoc.org/google.golang.org/grpc#ConnectionTimeout
ConnectionTimeoutSeconds uint32 `json:"connection_timeout_seconds"`
Certs certs.TLSCredsConfig `json:"certs"`
} `json:"grpc"`
Gateway struct {
FQDN string `json:"fqdn"`
ListenAddress string `json:"listen_address"`
AllowedOrigins []string `json:"allowed_origins"`
AllowedHeaders []string `json:"allowed_headers"`
AllowedMethods []string `json:"allowed_methods"`
Certs certs.TLSCredsConfig `json:"certs"`
HTTP bool `json:"http"`
ReadTimeout time.Duration `json:"read_timeout"`
ReadHeaderTimeout time.Duration `json:"read_header_timeout"`
WriteTimeout time.Duration `json:"write_timeout"`
IdleTimeout time.Duration `json:"idle_timeout"`
} `json:"gateway"`
}
func (g *Gateway) AddHandler(pattern string, handler http.HandlerFunc) {
g.Mux.Handle(pattern, handler)
}