Embed frontend dist into Go binary; remove frontend path from health
- New backend/embed package embeds frontend dist/ via //go:embed - Go binary serves frontend from embedded FS (no external files needed) - Dev mode uses placeholder dir (dev.txt) so Go compiles; Vite handles frontend in dev mode - Makefile copies frontend/dist/ to backend/embed/dist/ during build - Removed FRONTEND_DIR env var and filesystem serving fallback - Removed frontend path from Health API response and Status page frontend section (only Served boolean remains) - Removed frontendDir from Handler struct/constructor (unused)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
dist/
|
dist/
|
||||||
frontend/dist/
|
frontend/dist/
|
||||||
|
backend/embed/dist/
|
||||||
|
|
||||||
*.exe
|
*.exe
|
||||||
*.test
|
*.test
|
||||||
|
|||||||
12
Makefile
12
Makefile
@@ -15,15 +15,23 @@ backend:
|
|||||||
frontend:
|
frontend:
|
||||||
cd frontend && npm run build
|
cd frontend && npm run build
|
||||||
|
|
||||||
build: backend frontend
|
.PHONY: copyfrontend
|
||||||
|
copyfrontend:
|
||||||
|
rm -rf backend/embed/dist
|
||||||
|
mkdir -p backend/embed/dist
|
||||||
|
cp -r frontend/dist/. backend/embed/dist/
|
||||||
|
|
||||||
|
build: frontend copyfrontend backend
|
||||||
|
|
||||||
run:
|
run:
|
||||||
cd backend && DEV_DEPLOY_DIR=$(DEV_DEPLOY_DIR) SERVERFILE_DIR=$(SERVERFILE_DIR) MODS_DIR=$(MODS_DIR) CFG_DIR=$(CFG_DIR) PROFILES_DIR=$(PROFILES_DIR) DATA_DIR=$(DATA_DIR) GIN_MODE=debug go run ./cmd/server/
|
cd backend && DEV_DEPLOY_DIR=$(DEV_DEPLOY_DIR) SERVERFILE_DIR=$(SERVERFILE_DIR) MODS_DIR=$(MODS_DIR) CFG_DIR=$(CFG_DIR) PROFILES_DIR=$(PROFILES_DIR) DATA_DIR=$(DATA_DIR) GIN_MODE=debug go run ./cmd/server/
|
||||||
|
|
||||||
dev:
|
dev:
|
||||||
|
mkdir -p backend/embed/dist
|
||||||
|
test -f backend/embed/dist/dev.txt || touch backend/embed/dist/dev.txt
|
||||||
cd backend && DEV_DEPLOY_DIR=$(DEV_DEPLOY_DIR) SERVERFILE_DIR=$(SERVERFILE_DIR) MODS_DIR=$(MODS_DIR) CFG_DIR=$(CFG_DIR) PROFILES_DIR=$(PROFILES_DIR) DATA_DIR=$(DATA_DIR) GIN_MODE=debug go run ./cmd/server/ &
|
cd backend && DEV_DEPLOY_DIR=$(DEV_DEPLOY_DIR) SERVERFILE_DIR=$(SERVERFILE_DIR) MODS_DIR=$(MODS_DIR) CFG_DIR=$(CFG_DIR) PROFILES_DIR=$(PROFILES_DIR) DATA_DIR=$(DATA_DIR) GIN_MODE=debug go run ./cmd/server/ &
|
||||||
cd frontend && npm run dev
|
cd frontend && npm run dev
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f backend/arma3-web-server
|
rm -f backend/arma3-web-server
|
||||||
rm -rf frontend/dist
|
rm -rf frontend/dist backend/embed/dist
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"arma3-web-server/embed"
|
||||||
"arma3-web-server/internal/api"
|
"arma3-web-server/internal/api"
|
||||||
"arma3-web-server/internal/services"
|
"arma3-web-server/internal/services"
|
||||||
|
|
||||||
@@ -24,7 +26,6 @@ func main() {
|
|||||||
cfgDir := mustAbs(getEnv("CFG_DIR", filepath.Join(devDeploy, "serverfiles", "cfg")))
|
cfgDir := mustAbs(getEnv("CFG_DIR", filepath.Join(devDeploy, "serverfiles", "cfg")))
|
||||||
profilesDir := mustAbs(getEnv("PROFILES_DIR", filepath.Join(devDeploy, "serverfiles", "profiles")))
|
profilesDir := mustAbs(getEnv("PROFILES_DIR", filepath.Join(devDeploy, "serverfiles", "profiles")))
|
||||||
listen := getEnv("LISTEN", ":8080")
|
listen := getEnv("LISTEN", ":8080")
|
||||||
frontendDir := mustAbs(getEnv("FRONTEND_DIR", filepath.Join("..", "frontend", "dist")))
|
|
||||||
|
|
||||||
log.Printf("data dir: %s", dataDir)
|
log.Printf("data dir: %s", dataDir)
|
||||||
log.Printf("serverfile dir: %s", serverfileDir)
|
log.Printf("serverfile dir: %s", serverfileDir)
|
||||||
@@ -55,16 +56,19 @@ func main() {
|
|||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
if fi, err := os.Stat(frontendDir); err == nil && fi.IsDir() {
|
subFS, subErr := fs.Sub(embed.Frontend, "dist")
|
||||||
r.Static("/assets", filepath.Join(frontendDir, "assets"))
|
if subErr == nil {
|
||||||
r.StaticFile("/favicon.ico", filepath.Join(frontendDir, "favicon.ico"))
|
if _, err := subFS.Open("index.html"); err == nil {
|
||||||
r.NoRoute(func(c *gin.Context) {
|
r.StaticFS("/assets", http.FS(subFS))
|
||||||
c.File(filepath.Join(frontendDir, "index.html"))
|
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(subFS))
|
||||||
})
|
r.NoRoute(func(c *gin.Context) {
|
||||||
log.Printf("serving frontend from %s", frontendDir)
|
c.FileFromFS("index.html", http.FS(subFS))
|
||||||
|
})
|
||||||
|
log.Print("serving embedded frontend")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := api.New(settings, configMgr, modlistMgr, process, steamcmd, scheduler, streamer, dataDir, serverfileDir, modsDir, cfgDir, profilesDir, frontendDir)
|
handler := api.New(settings, configMgr, modlistMgr, process, steamcmd, scheduler, streamer, dataDir, serverfileDir, modsDir, cfgDir, profilesDir)
|
||||||
handler.SetupRoutes(r)
|
handler.SetupRoutes(r)
|
||||||
|
|
||||||
// Startup auto-tasks
|
// Startup auto-tasks
|
||||||
|
|||||||
6
backend/embed/embed.go
Normal file
6
backend/embed/embed.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package embed
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed dist
|
||||||
|
var Frontend embed.FS
|
||||||
@@ -50,8 +50,7 @@ type SteamCMDHealth struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type FrontendHealth struct {
|
type FrontendHealth struct {
|
||||||
Served bool `json:"served"`
|
Served bool `json:"served"`
|
||||||
Path string `json:"path"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) Health(c *gin.Context) {
|
func (h *Handler) Health(c *gin.Context) {
|
||||||
@@ -60,7 +59,6 @@ func (h *Handler) Health(c *gin.Context) {
|
|||||||
|
|
||||||
paths := h.checkPaths()
|
paths := h.checkPaths()
|
||||||
disk := h.checkDisk()
|
disk := h.checkDisk()
|
||||||
frontendServed, _ := h.checkFrontend()
|
|
||||||
|
|
||||||
resp := HealthResponse{
|
resp := HealthResponse{
|
||||||
Server: ServerHealth{
|
Server: ServerHealth{
|
||||||
@@ -78,8 +76,7 @@ func (h *Handler) Health(c *gin.Context) {
|
|||||||
Running: h.steamcmd.IsRunning(),
|
Running: h.steamcmd.IsRunning(),
|
||||||
},
|
},
|
||||||
Frontend: FrontendHealth{
|
Frontend: FrontendHealth{
|
||||||
Served: frontendServed,
|
Served: true,
|
||||||
Path: h.frontendDir,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,14 +126,6 @@ func (h *Handler) checkDisk() []DiskHealth {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) checkFrontend() (bool, string) {
|
|
||||||
if h.frontendDir == "" {
|
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
fi, err := os.Stat(h.frontendDir)
|
|
||||||
return err == nil && fi.IsDir(), h.frontendDir
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkServerBinary(serverfileDir string) bool {
|
func checkServerBinary(serverfileDir string) bool {
|
||||||
_, found := findServerBinary(serverfileDir)
|
_, found := findServerBinary(serverfileDir)
|
||||||
return found
|
return found
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ type Handler struct {
|
|||||||
modsDir string
|
modsDir string
|
||||||
cfgDir string
|
cfgDir string
|
||||||
profilesDir string
|
profilesDir string
|
||||||
frontendDir string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
@@ -30,7 +29,7 @@ func New(
|
|||||||
steamcmd *services.SteamCmdManager,
|
steamcmd *services.SteamCmdManager,
|
||||||
scheduler *services.Scheduler,
|
scheduler *services.Scheduler,
|
||||||
streamer *services.LogStreamer,
|
streamer *services.LogStreamer,
|
||||||
dataDir, serverfileDir, modsDir, cfgDir, profilesDir, frontendDir string,
|
dataDir, serverfileDir, modsDir, cfgDir, profilesDir string,
|
||||||
) *Handler {
|
) *Handler {
|
||||||
return &Handler{
|
return &Handler{
|
||||||
settings: settings,
|
settings: settings,
|
||||||
@@ -45,7 +44,6 @@ func New(
|
|||||||
modsDir: modsDir,
|
modsDir: modsDir,
|
||||||
cfgDir: cfgDir,
|
cfgDir: cfgDir,
|
||||||
profilesDir: profilesDir,
|
profilesDir: profilesDir,
|
||||||
frontendDir: frontendDir,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,6 @@ export default function Status() {
|
|||||||
|
|
||||||
<Section title="Frontend">
|
<Section title="Frontend">
|
||||||
<StatusRow label="Served" value={data.frontend.served ? 'Yes' : 'No'} ok={data.frontend.served} />
|
<StatusRow label="Served" value={data.frontend.served ? 'Yes' : 'No'} ok={data.frontend.served} />
|
||||||
<StatusRow label="Path" value={data.frontend.path ? <CopyPath path={data.frontend.path} /> : '-'} />
|
|
||||||
</Section>
|
</Section>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ export interface ServerHealth {
|
|||||||
}
|
}
|
||||||
frontend: {
|
frontend: {
|
||||||
served: boolean
|
served: boolean
|
||||||
path: string
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user