You've already forked agentic-coding-workflow
feat(server): embed SPA dist with fallback handler; single-binary deploy
This commit is contained in:
+12
-1
@@ -18,8 +18,10 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -43,7 +45,9 @@ type App struct {
|
||||
}
|
||||
|
||||
// New 把所有模块按启动顺序拼装好。任何阶段失败都会返回错误,不留半成品资源。
|
||||
func New(ctx context.Context, cfg *config.Config) (*App, error) {
|
||||
// dist 是 cmd/server 用 //go:embed 注入的前端构建产物,根目录为 web/dist;
|
||||
// fs.Sub 之后交给 SPA handler 提供静态文件 + index.html 回退。
|
||||
func New(ctx context.Context, cfg *config.Config, dist embed.FS) (*App, error) {
|
||||
log := logger.FromEnv(cfg.Env, nil)
|
||||
|
||||
// 跑 migrations 必须在开池前;否则首次启动连不存在的表会让 ping 失败。
|
||||
@@ -68,6 +72,12 @@ func New(ctx context.Context, cfg *config.Config) (*App, error) {
|
||||
// 通过 App 字段透出去,目前保留局部变量以避免暴露未稳定 API。
|
||||
_ = notifyDispatcher
|
||||
|
||||
spaSub, err := fs.Sub(dist, "web/dist")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sub fs: %w", err)
|
||||
}
|
||||
spaHandler := httpx.NewSPAHandler(spaSub)
|
||||
|
||||
r := httpx.NewRouter(httpx.RouterDeps{
|
||||
HealthCheck: func(req *http.Request) error {
|
||||
return db.HealthCheck(req.Context(), pool)
|
||||
@@ -75,6 +85,7 @@ func New(ctx context.Context, cfg *config.Config) (*App, error) {
|
||||
ReadyCheck: func(req *http.Request) error {
|
||||
return db.HealthCheck(req.Context(), pool)
|
||||
},
|
||||
SPAHandler: spaHandler,
|
||||
})
|
||||
|
||||
// chi.Router 接口本身已经声明 Use,无需类型断言到 *chi.Mux。
|
||||
|
||||
Reference in New Issue
Block a user