This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Embed React frontend via go:embed and update CI to use make build & make image #14
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
| package rest | ||
|
|
||
| import ( | ||
| "io/fs" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/Gthulhu/api/web" | ||
| "github.com/labstack/echo/v4" | ||
| ) | ||
|
|
||
| // RegisterFrontend serves the embedded React SPA from web/dist. | ||
| // It serves static assets directly and falls back to index.html | ||
| // for client-side routing. | ||
| func RegisterFrontend(e *echo.Echo) { | ||
| distFS, err := fs.Sub(web.DistFS, "dist") | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| // Check whether the frontend was actually built (more than just .gitkeep). | ||
| hasIndex := false | ||
| if f, err := distFS.Open("index.html"); err == nil { | ||
| f.Close() | ||
| hasIndex = true | ||
| } | ||
| if !hasIndex { | ||
| return | ||
| } | ||
|
Comment on lines
+27
to
+29
|
||
|
|
||
| fileServer := http.FileServer(http.FS(distFS)) | ||
|
|
||
| e.GET("/*", echo.WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| path := strings.TrimPrefix(r.URL.Path, "/") | ||
|
|
||
| // Try to open the requested file. If it exists, serve it directly. | ||
| if path != "" { | ||
| if f, err := distFS.Open(path); err == nil { | ||
| f.Close() | ||
| fileServer.ServeHTTP(w, r) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // Fallback to index.html for SPA client-side routing. | ||
| r.URL.Path = "/" | ||
| fileServer.ServeHTTP(w, r) | ||
| }))) | ||
| } | ||
This file contains hidden or 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,90 @@ | ||
| package rest | ||
|
|
||
| import ( | ||
| "io/fs" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "testing/fstest" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRegisterFrontend_WithIndex(t *testing.T) { | ||
| e := echo.New() | ||
|
|
||
| // Create a mock filesystem that simulates embedded dist contents. | ||
| mockFS := fstest.MapFS{ | ||
| "index.html": {Data: []byte("<html>app</html>")}, | ||
| "assets/main.js": {Data: []byte("console.log('ok')")}, | ||
| "assets/style.css": {Data: []byte("body{}")}, | ||
| } | ||
|
|
||
| registerFrontendFromFS(e, mockFS) | ||
|
|
||
| // Requesting an existing asset should return it directly. | ||
| rec := httptest.NewRecorder() | ||
| req := httptest.NewRequest(http.MethodGet, "/assets/main.js", nil) | ||
| e.ServeHTTP(rec, req) | ||
| assert.Equal(t, http.StatusOK, rec.Code) | ||
| assert.Contains(t, rec.Body.String(), "console.log") | ||
|
|
||
| // Requesting a non-existent path should fall back to index.html (SPA). | ||
| rec = httptest.NewRecorder() | ||
| req = httptest.NewRequest(http.MethodGet, "/some/spa/route", nil) | ||
| e.ServeHTTP(rec, req) | ||
| assert.Equal(t, http.StatusOK, rec.Code) | ||
| assert.Contains(t, rec.Body.String(), "<html>app</html>") | ||
| } | ||
|
|
||
| func TestRegisterFrontend_WithoutIndex(t *testing.T) { | ||
| e := echo.New() | ||
|
|
||
| // Filesystem without index.html should not register any routes. | ||
| mockFS := fstest.MapFS{ | ||
| ".gitkeep": {Data: []byte{}}, | ||
| } | ||
|
|
||
| registerFrontendFromFS(e, mockFS) | ||
|
|
||
| rec := httptest.NewRecorder() | ||
| req := httptest.NewRequest(http.MethodGet, "/anything", nil) | ||
| e.ServeHTTP(rec, req) | ||
|
|
||
| // Should get 404/405 because no wildcard route was registered. | ||
| assert.NotEqual(t, http.StatusOK, rec.Code) | ||
| } | ||
|
|
||
| // registerFrontendFromFS is a test helper that mirrors RegisterFrontend logic | ||
| // but accepts an arbitrary fs.FS instead of the embedded one. | ||
| func registerFrontendFromFS(e *echo.Echo, distFS fs.FS) { | ||
| hasIndex := false | ||
| if f, err := distFS.Open("index.html"); err == nil { | ||
| f.Close() | ||
| hasIndex = true | ||
| } | ||
| if !hasIndex { | ||
| return | ||
| } | ||
|
|
||
| fileServer := http.FileServer(http.FS(distFS)) | ||
|
|
||
| e.GET("/*", echo.WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| path := r.URL.Path | ||
| if len(path) > 0 && path[0] == '/' { | ||
| path = path[1:] | ||
| } | ||
|
|
||
| if path != "" { | ||
| if f, err := distFS.Open(path); err == nil { | ||
| f.Close() | ||
| fileServer.ServeHTTP(w, r) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| r.URL.Path = "/" | ||
| fileServer.ServeHTTP(w, r) | ||
| }))) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| dist | ||
| dist/* | ||
| !dist/.gitkeep | ||
| node_modules |
Empty file.
This file contains hidden or 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,6 @@ | ||
| package web | ||
|
|
||
| import "embed" | ||
|
|
||
| //go:embed all:dist | ||
| var DistFS embed.FS |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error from fs.Sub is silently ignored. This could hide configuration issues or embed problems during startup. Consider logging the error to help with debugging if the frontend fails to load. The codebase uses logger.Logger for similar initialization issues (e.g., in manager/app/rest_app.go and manager/service/strategy_svc.go).