Skip to content

Commit

Permalink
Prepare topaz to use the go-topaz-ui package
Browse files Browse the repository at this point in the history
  • Loading branch information
carabasdaniel committed Nov 13, 2023
1 parent 3b919c1 commit 9677d2a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 190 deletions.
5 changes: 0 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ env:
- ORG=aserto-dev
- REPO=topaz

before:
# https://goreleaser.com/customization/hooks/
hooks:
- ./pre-build.sh {{ .Env.CONSOLE_VERSION }}

builds:
# https://goreleaser.com/customization/build/
- id: topazd
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module github.com/aserto-dev/topaz

go 1.20

// replace github.com/aserto-dev/azm => ../azm
// replace github.com/aserto-dev/go-directory => ../go-directory
// replace github.com/aserto-dev/go-directory-cli => ../go-directory-cli
// replace github.com/aserto-dev/go-edge-ds => ../go-edge-ds

// replace github.com/aserto-dev/go-topaz-ui => ../go-topaz-ui

// replace github.com/aserto-dev/service-host => ../service-host
// replace github.com/aserto-dev/runtime => ../runtime

Expand All @@ -22,6 +23,7 @@ require (
github.com/aserto-dev/go-directory-cli v0.30.1
github.com/aserto-dev/go-edge-ds v0.30.2
github.com/aserto-dev/go-grpc v0.8.59
github.com/aserto-dev/go-topaz-ui v0.0.0-20231113091804-10da5d3a0946
github.com/aserto-dev/header v0.0.5
github.com/aserto-dev/logger v0.0.4
github.com/aserto-dev/openapi-authorizer v0.20.3
Expand Down
33 changes: 33 additions & 0 deletions go.sum

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pkg/app/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type Authorizer struct {

const (
authorizerService = "authorizer"
consoleService = "console"
)

func NewAuthorizer(cfg *builder.API, commonConfig *config.Common, authorizerOpts []grpc.ServerOption, logger *zerolog.Logger) (ServiceTypes, error) {
Expand Down
53 changes: 48 additions & 5 deletions pkg/app/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package app

import (
"context"
"embed"
"fmt"
"strings"

"github.com/aserto-dev/go-topaz-ui/ui"
builder "github.com/aserto-dev/service-host"
"github.com/aserto-dev/topaz/pkg/cc/config"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)

//nolint:all
//go:embed console
var console embed.FS

type ConsoleService struct{}

const (
consoleService = "console"
)

func NewConsole() ServiceTypes {
return &ConsoleService{}
}
Expand All @@ -37,3 +40,43 @@ func (e *ConsoleService) GetGatewayRegistration(services ...string) builder.Hand
func (e *ConsoleService) Cleanups() []func() {
return nil
}

func (e *ConsoleService) PrepareConfig(cfg *config.Config) *ui.ConsoleCfg {
authorizerURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[authorizerService]; ok {
authorizerURL = getGatewayAddress(serviceConfig)
}
readerURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[readerService]; ok {
readerURL = getGatewayAddress(serviceConfig)
}
if readerURL == "" {
host := strings.Split(cfg.DirectoryResolver.Address, ":")[0]
readerURL = fmt.Sprintf("https://%s", host)
}
writerURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[writerService]; ok {
readerURL = getGatewayAddress(serviceConfig)
}
modelURL := ""
if serviceConfig, ok := cfg.APIConfig.Services[modelService]; ok {
readerURL = getGatewayAddress(serviceConfig)
}
return &ui.ConsoleCfg{
AsertoDirectoryURL: readerURL,
DirectoryAPIKey: cfg.DirectoryResolver.APIKey,
DirectoryTenantID: cfg.DirectoryResolver.TenantID,
AuthorizerServiceURL: authorizerURL,
AsertoDirectoryReaderURL: &readerURL,
AsertoDirectoryWriterURL: &writerURL,
AsertoDirectoryModelURL: &modelURL,
}
}

func getGatewayAddress(serviceConfig *builder.API) string {
if serviceConfig.Gateway.HTTP {
return fmt.Sprintf("http://%s", serviceConfig.Gateway.ListenAddress)
} else {
return fmt.Sprintf("https://%s", serviceConfig.Gateway.ListenAddress)
}
}
23 changes: 15 additions & 8 deletions pkg/app/topaz.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/aserto-dev/topaz/decision_log/logger/file"
"github.com/aserto-dev/topaz/decision_log/logger/nop"
"github.com/aserto-dev/topaz/pkg/app/middlewares"
"github.com/aserto-dev/topaz/pkg/app/ui"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"

Expand All @@ -24,6 +23,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"

console "github.com/aserto-dev/go-topaz-ui"
"github.com/aserto-dev/go-topaz-ui/ui"
builder "github.com/aserto-dev/service-host"
)

Expand Down Expand Up @@ -145,11 +146,15 @@ func (e *Topaz) ConfigServices() error {
return err
}

if contains(serviceConfig.registeredServices, "console") {
server.Gateway.Mux.Handle("/ui/", ui.UIHandler(http.FS(console)))
server.Gateway.Mux.Handle("/public/", ui.UIHandler(http.FS(console)))
server.Gateway.Mux.HandleFunc("/api/v1/config", ui.ConfigHandler(e.Configuration))
server.Gateway.Mux.HandleFunc("/api/v1/authorizers", ui.AuthorizersHandler(e.Configuration))
if con, ok := e.Services[consoleService]; ok {
consoleConfig := con.(*ConsoleService).PrepareConfig(e.Configuration)

if contains(serviceConfig.registeredServices, "console") {
server.Gateway.Mux.Handle("/ui/", ui.UIHandler(http.FS(console.FS)))
server.Gateway.Mux.Handle("/public/", ui.UIHandler(http.FS(console.FS)))
server.Gateway.Mux.HandleFunc("/api/v1/config", ui.ConfigHandler(consoleConfig))
server.Gateway.Mux.HandleFunc("/api/v1/authorizers", ui.AuthorizersHandler(consoleConfig))
}
}

err = e.Manager.AddGRPCServer(server)
Expand Down Expand Up @@ -299,8 +304,10 @@ func (e *Topaz) validateConfig() error {
}

if _, ok := e.Configuration.APIConfig.Services["console"]; ok {
if _, ok := e.Configuration.APIConfig.Services["model"]; !ok {
return errors.New("console needs the model service to be configured")
if _, ok := e.Configuration.APIConfig.Services["reader"]; ok {
if _, ok := e.Configuration.APIConfig.Services["model"]; !ok {
return errors.New("console needs the model service to be configured")
}
}
}

Expand Down
162 changes: 0 additions & 162 deletions pkg/app/ui/handler.go

This file was deleted.

7 changes: 0 additions & 7 deletions pre-build.sh

This file was deleted.

0 comments on commit 9677d2a

Please sign in to comment.