-
Notifications
You must be signed in to change notification settings - Fork 26
/
console.go
125 lines (106 loc) · 3.39 KB
/
console.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
package app
import (
"context"
"fmt"
"net/http"
"strings"
builder "github.com/aserto-dev/service-host"
"github.com/aserto-dev/topaz/pkg/app/handlers"
"github.com/aserto-dev/topaz/pkg/cc/config"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
type ConsoleService struct{}
const (
consoleService = "console"
)
func NewConsole() ServiceTypes {
return &ConsoleService{}
}
func (e *ConsoleService) AvailableServices() []string {
return []string{"console"}
}
func (e *ConsoleService) GetGRPCRegistrations(services ...string) builder.GRPCRegistrations {
return func(server *grpc.Server) {
}
}
func (e *ConsoleService) GetGatewayRegistration(services ...string) builder.HandlerRegistrations {
return func(ctx context.Context, mux *runtime.ServeMux, grpcEndpoint string, opts []grpc.DialOption) error {
return mux.HandlePath("GET", "/", runtime.HandlerFunc(func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
http.Redirect(w, r, "/ui/directory/model", http.StatusSeeOther)
}))
}
}
func (e *ConsoleService) Cleanups() []func() {
return nil
}
func (e *ConsoleService) PrepareConfig(cfg *config.Config) *handlers.TopazCfg {
authorizerURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[authorizerService]; ok {
authorizerURL = getGatewayAddress(serviceConfig)
}
readerURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[readerService]; ok {
readerURL = getGatewayAddress(serviceConfig)
}
writerURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[writerService]; ok {
writerURL = getGatewayAddress(serviceConfig)
}
importerURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[importerService]; ok {
importerURL = getGatewayAddress(serviceConfig)
}
exporterURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[exporterService]; ok {
exporterURL = getGatewayAddress(serviceConfig)
}
modelURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[modelService]; ok {
modelURL = getGatewayAddress(serviceConfig)
}
consoleURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[consoleService]; ok {
consoleURL = getGatewayAddress(serviceConfig)
}
authorizerAPIKey := ""
if _, ok := cfg.APIConfig.Services[authorizerService]; ok {
for key := range cfg.Auth.APIKeys {
// we only need a key
authorizerAPIKey = key
break
}
}
directoryAPIKey := ""
if _, ok := cfg.APIConfig.Services[readerService]; ok {
for key := range cfg.Auth.APIKeys {
// we only need a key
directoryAPIKey = key
break
}
}
return &handlers.TopazCfg{
AuthorizerServiceURL: authorizerURL,
AuthorizerAPIKey: authorizerAPIKey,
DirectoryServiceURL: readerURL,
DirectoryAPIKey: directoryAPIKey,
DirectoryTenantID: cfg.DirectoryResolver.TenantID,
DirectoryReaderServiceURL: readerURL,
DirectoryWriterServiceURL: writerURL,
DirectoryImporterServiceURL: importerURL,
DirectoryExporterServiceURL: exporterURL,
DirectoryModelServiceURL: modelURL,
ConsoleURL: consoleURL,
}
}
func getGatewayAddress(serviceConfig *builder.API) string {
addr := serviceAddress(serviceConfig.Gateway.ListenAddress)
if serviceConfig.Gateway.HTTP {
return fmt.Sprintf("http://%s", addr)
} else {
return fmt.Sprintf("https://%s", addr)
}
}
func serviceAddress(listenAddress string) string {
return strings.Replace(listenAddress, "0.0.0.0", "localhost", 1)
}