Skip to content

Commit

Permalink
feat(ui): embed and serve UI assets from Burrito binary (#191)
Browse files Browse the repository at this point in the history
* feat(api): serve static ui asset

* feat(api): embed assets in the binary

* fix(api): fix broken import directive

* feat(api): add ui build stage to dockerfile
  • Loading branch information
marcantoinegodde authored Nov 13, 2023
1 parent 62639e1 commit dec34ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Build Burrito UI

FROM docker.io/library/node:20.9.0@sha256:cb7cd40ba6483f37f791e1aace576df449fc5f75332c19ff59e2c6064797160e AS builder-ui

WORKDIR /workspace
# Copy the node modules manifests
COPY ui/package.json ui/yarn.lock ./
# Install build dependencies
RUN yarn install --frozen-lockfile

# Copy the UI source
COPY ui .
# Set the API base URL
ENV VITE_API_BASE_URL=/api
RUN yarn build

# Build the manager binary
FROM docker.io/library/golang:1.20.7@sha256:bc5f0b5e43282627279fe5262ae275fecb3d2eae3b33977a7fd200c7a760d6f1 as builder
ARG TARGETOS
Expand All @@ -20,6 +36,8 @@ COPY main.go main.go
COPY api/ api/
COPY internal/ internal/
COPY cmd/ cmd/
# Copy the UI build artifacts
COPY --from=builder-ui /workspace/dist internal/server/dist

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
Empty file added internal/server/dist/.gitkeep
Empty file.
35 changes: 24 additions & 11 deletions internal/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"embed"
"net/http"

"github.com/labstack/echo/v4"
Expand All @@ -18,18 +19,23 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

//go:embed all:dist
var content embed.FS

type Server struct {
config *config.Config
Webhook *webhook.Webhook
API *api.API
client client.Client
config *config.Config
Webhook *webhook.Webhook
API *api.API
staticAssets http.FileSystem
client client.Client
}

func New(c *config.Config) *Server {
return &Server{
config: c,
Webhook: webhook.New(c),
API: api.New(c),
config: c,
Webhook: webhook.New(c),
API: api.New(c),
staticAssets: http.FS(content),
}
}

Expand Down Expand Up @@ -61,11 +67,18 @@ func (s *Server) Exec() {
log.Infof("starting burrito server...")
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.CORS())
e.Use(middleware.StaticWithConfig(
middleware.StaticConfig{
Filesystem: s.staticAssets,
Root: "dist",
Index: "index.html",
HTML5: true,
},
))
e.GET("/healthz", handleHealthz)
e.POST("/webhook", s.Webhook.GetHttpHandler())
e.GET("/layers", s.API.LayersHandler)
e.GET("/repositories", s.API.RepositoriesHandler)
e.POST("/api/webhook", s.Webhook.GetHttpHandler())
e.GET("/api/layers", s.API.LayersHandler)
e.GET("/api/repositories", s.API.RepositoriesHandler)
e.Logger.Fatal(e.Start(s.config.Server.Addr))
log.Infof("burrito server started on addr %s", s.config.Server.Addr)
}
Expand Down

0 comments on commit dec34ef

Please sign in to comment.