-
Notifications
You must be signed in to change notification settings - Fork 26
/
edgedir.go
155 lines (138 loc) · 4.6 KB
/
edgedir.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package app
import (
"context"
"net/http"
"strconv"
dse2 "github.com/aserto-dev/go-directory/aserto/directory/exporter/v2"
dse3 "github.com/aserto-dev/go-directory/aserto/directory/exporter/v3"
dsi2 "github.com/aserto-dev/go-directory/aserto/directory/importer/v2"
dsi3 "github.com/aserto-dev/go-directory/aserto/directory/importer/v3"
dsm3 "github.com/aserto-dev/go-directory/aserto/directory/model/v3"
dsr2 "github.com/aserto-dev/go-directory/aserto/directory/reader/v2"
dsr3 "github.com/aserto-dev/go-directory/aserto/directory/reader/v3"
dsw2 "github.com/aserto-dev/go-directory/aserto/directory/writer/v2"
dsw3 "github.com/aserto-dev/go-directory/aserto/directory/writer/v3"
dsm3stream "github.com/aserto-dev/go-directory/pkg/gateway/model/v3"
"github.com/aserto-dev/go-edge-ds/pkg/directory"
dsOpenAPI "github.com/aserto-dev/openapi-directory/publish/directory"
builder "github.com/aserto-dev/service-host"
"github.com/aserto-dev/topaz/pkg/rapidoc"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
type EdgeDir struct {
dir *directory.Directory
}
const (
modelService = "model"
readerService = "reader"
writerService = "writer"
exporterService = "exporter"
importerService = "importer"
)
func NewEdgeDir(edge *directory.Directory) (ServiceTypes, error) {
return &EdgeDir{
dir: edge,
}, nil
}
func (e *EdgeDir) Cleanups() []func() {
if e.dir != nil {
return []func(){e.dir.Close}
}
return nil
}
func (e *EdgeDir) AvailableServices() []string {
return []string{modelService, readerService, writerService, exporterService, importerService}
}
func (e *EdgeDir) GetGRPCRegistrations(services ...string) builder.GRPCRegistrations {
return func(server *grpc.Server) {
if contains(services, modelService) {
dsm3.RegisterModelServer(server, e.dir.Model3())
}
if contains(services, readerService) {
dsr2.RegisterReaderServer(server, e.dir.Reader2())
dsr3.RegisterReaderServer(server, e.dir.Reader3())
}
if contains(services, writerService) {
dsw2.RegisterWriterServer(server, e.dir.Writer2())
dsw3.RegisterWriterServer(server, e.dir.Writer3())
}
if contains(services, importerService) {
dsi2.RegisterImporterServer(server, e.dir.Importer2())
dsi3.RegisterImporterServer(server, e.dir.Importer3())
}
if contains(services, exporterService) {
dse2.RegisterExporterServer(server, e.dir.Exporter2())
dse3.RegisterExporterServer(server, e.dir.Exporter3())
}
}
}
func (e *EdgeDir) GetGatewayRegistration(services ...string) builder.HandlerRegistrations {
return func(ctx context.Context, mux *runtime.ServeMux, grpcEndpoint string, opts []grpc.DialOption) error {
if contains(services, modelService) {
err := dsm3.RegisterModelHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
if err := dsm3stream.RegisterModelStreamHandlersFromEndpoint(ctx, mux, grpcEndpoint, opts); err != nil {
return err
}
}
if contains(services, readerService) {
err := dsr3.RegisterReaderHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
}
if contains(services, writerService) {
err := dsw3.RegisterWriterHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
}
if contains(services, importerService) {
err := dsi3.RegisterImporterHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
}
if contains(services, exporterService) {
err := dse3.RegisterExporterHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
}
if len(services) > 0 {
if err := mux.HandlePath(http.MethodGet, directoryOpenAPISpec, dsOpenAPIHandler); err != nil {
return err
}
if err := mux.HandlePath(http.MethodGet, directoryOpenAPIDocs, dsOpenAPIDocsHandler()); err != nil {
return err
}
}
return nil
}
}
const (
directoryOpenAPISpec string = "/directory/openapi.json"
directoryOpenAPIDocs string = "/directory/docs"
)
func dsOpenAPIHandler(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
buf, err := dsOpenAPI.Static().ReadFile("openapi.json")
if err != nil {
w.WriteHeader(http.StatusNotFound)
}
w.Header().Add("Content-Type", "application/json")
w.Header().Add("Content-Length", strconv.FormatInt(int64(len(buf)), 10))
_, _ = w.Write(buf)
}
func dsOpenAPIDocsHandler() runtime.HandlerFunc {
h := rapidoc.Handler(&rapidoc.Opts{
Path: directoryOpenAPIDocs,
SpecURL: directoryOpenAPISpec,
Title: "Aserto Directory HTTP API",
}, nil)
return func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
h.ServeHTTP(w, r)
}
}