Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using a custom asset path, overriding the embedded UI assets. #267

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type (
TLS TLS `yaml:"tls"`
Auth Auth `yaml:"auth"`
EnableUI bool `yaml:"enableUi"`
UIAssetPath string `yaml:"uiAssetPath"`
EnableOpenAPI bool `yaml:"enableOpenApi"`
CORS CORS `yaml:"cors"`
DefaultNamespace string `yaml:"defaultNamespace"`
Expand Down
27 changes: 18 additions & 9 deletions server/route/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ package route

import (
"bytes"
"embed"
"io/fs"
"net/http"

"github.com/labstack/echo/v4"
)

// SetUIRoutes sets UI routes
func SetUIRoutes(e *echo.Echo, indexHTML []byte, assets embed.FS) {
func SetUIRoutes(e *echo.Echo, assets fs.FS) error {
assetsHandler := buildUIAssetsHandler(assets)
e.GET("/_app/*", assetsHandler)
e.GET("/css/*", assetsHandler)
Expand All @@ -44,18 +43,28 @@ func SetUIRoutes(e *echo.Echo, indexHTML []byte, assets embed.FS) {
e.GET("/logo*", assetsHandler)
e.GET("/Temporal_Logo_Animation.gif", assetsHandler)
e.GET("/site.webmanifest", assetsHandler)
e.GET("/*", buildUIIndexHandler(indexHTML))
indexHandler, err := buildUIIndexHandler(assets)
if err != nil {
return err
}

e.GET("/*", indexHandler)

return nil
}

func buildUIIndexHandler(indexHTML []byte) echo.HandlerFunc {
func buildUIIndexHandler(assets fs.FS) (echo.HandlerFunc, error) {
indexHTML, err := fs.ReadFile(assets, "index.html")
if err != nil {
return nil, err
}

return func(c echo.Context) (err error) {
return c.Stream(200, "text/html", bytes.NewBuffer(indexHTML))
}
}, nil
}

func buildUIAssetsHandler(assets embed.FS) echo.HandlerFunc {
stream := fs.FS(assets)
stream, _ = fs.Sub(stream, "generated/ui")
handler := http.FileServer(http.FS(stream))
func buildUIAssetsHandler(assets fs.FS) echo.HandlerFunc {
handler := http.FileServer(http.FS(assets))
return echo.WrapHandler(handler)
}
16 changes: 12 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ package server
import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
Expand All @@ -39,9 +41,6 @@ import (
"github.com/temporalio/ui-server/v2/server/server_options"
)

//go:embed generated/ui/index.html
var uiHTML []byte

//go:embed all:generated/ui
var uiAssets embed.FS

Expand Down Expand Up @@ -112,7 +111,16 @@ func NewServer(opts ...server_options.ServerOption) *Server {
route.SetSwaggerUIRoutes(e, swaggeruiHTML, swaggeruiAssets)
}
if cfg.EnableUI {
route.SetUIRoutes(e, uiHTML, uiAssets)
var assets fs.FS
if cfg.UIAssetPath != "" {
assets = os.DirFS(cfg.UIAssetPath)
} else {
assets, err = fs.Sub(uiAssets, "generated/ui")
if err != nil {
panic(err)
}
}
route.SetUIRoutes(e, assets)
}

s := &Server{
Expand Down