-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from ccremer/dist-ui
Bundle the UI in the Go binary
- Loading branch information
Showing
15 changed files
with
234 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package webui | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/ccremer/clustercode/pkg/internal/pipe" | ||
"github.com/ccremer/clustercode/ui" | ||
pipeline "github.com/ccremer/go-command-pipeline" | ||
"github.com/go-logr/logr" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
type Command struct { | ||
Log logr.Logger | ||
} | ||
|
||
type commandContext struct { | ||
context.Context | ||
dependencyResolver pipeline.DependencyResolver[*commandContext] | ||
|
||
echo *echo.Echo | ||
} | ||
|
||
// Execute runs the command and returns an error, if any. | ||
func (c *Command) Execute(ctx context.Context) error { | ||
|
||
pctx := &commandContext{ | ||
dependencyResolver: pipeline.NewDependencyRecorder[*commandContext](), | ||
Context: ctx, | ||
} | ||
|
||
p := pipeline.NewPipeline[*commandContext]().WithBeforeHooks(pipe.DebugLogger[*commandContext](c.Log), pctx.dependencyResolver.Record) | ||
p.WithSteps( | ||
p.NewStep("create server", c.createServer), | ||
p.NewStep("setup routes", c.setupRoutes), | ||
p.NewStep("run server", c.startServer), | ||
) | ||
return p.RunWithContext(pctx) | ||
} | ||
|
||
func (c *Command) createServer(ctx *commandContext) error { | ||
ctx.echo = echo.New() | ||
ctx.echo.Pre(middleware.RemoveTrailingSlash()) | ||
ctx.echo.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ | ||
Skipper: skipAccessLogs, | ||
Format: middleware.DefaultLoggerConfig.Format, | ||
CustomTimeFormat: middleware.DefaultLoggerConfig.CustomTimeFormat, | ||
})) | ||
return nil | ||
} | ||
|
||
func (c *Command) setupRoutes(ctx *commandContext) error { | ||
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createServer) | ||
assetHandler := http.FileServer(c.getFileSystem()) | ||
ctx.echo.GET("/", echo.WrapHandler(assetHandler)) | ||
ctx.echo.GET("/vite.svg", echo.WrapHandler(assetHandler)) | ||
ctx.echo.GET("/assets/*", echo.WrapHandler(assetHandler)) | ||
ctx.echo.GET("/healthz", healthz) | ||
return nil | ||
} | ||
|
||
func (c *Command) getFileSystem() http.FileSystem { | ||
dir := "ui/dist" | ||
if ui.IsEmbedded() { | ||
c.Log.Info("Using embedded assets") | ||
fsys, err := fs.Sub(ui.PublicFs, "dist") | ||
if err == nil { | ||
return http.FS(fsys) | ||
} | ||
c.Log.Info("Cannot use embedded assets, resort to live assets", "error", err.Error()) | ||
} else { | ||
c.Log.Info("This release was built without embedding UI assets. Build or download the assets separately.") | ||
} | ||
c.Log.Info("Serving assets from local filesystem", "dir", dir) | ||
return http.FS(os.DirFS(dir)) | ||
} | ||
|
||
func (c *Command) startServer(ctx *commandContext) error { | ||
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createServer) | ||
return ctx.echo.Start(":8080") | ||
} | ||
|
||
var publicRoutes = []string{ | ||
"/favicon.ico", | ||
"/robots.txt", | ||
"/vite.svg", | ||
"/assets/*", | ||
"/healthz", | ||
} | ||
|
||
func skipAccessLogs(ctx echo.Context) bool { | ||
for _, path := range publicRoutes { | ||
if path == ctx.Path() { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func healthz(c echo.Context) error { | ||
return c.String(http.StatusNoContent, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build ui | ||
|
||
package ui | ||
|
||
import ( | ||
"embed" | ||
) | ||
|
||
//go:embed dist/* | ||
//go:embed dist/assets | ||
var publicFs embed.FS | ||
|
||
func init() { | ||
PublicFs = publicFs | ||
} |
Oops, something went wrong.