Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(server): allow explicit frontend directory in standard edition (#4637)

Albert3db70c29add6
diff --git a/internal/router/router.go b/internal/router/router.go
index d0419a5bc..fab23293c 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -170,8 +170,8 @@ func NewRouter(params RouterParams) *gin.Engine {
r.Use(embedFrameAncestorsMiddleware(params.EmbedChannelService))
}
- // 前端静态文件(仅 Lite 版本内嵌前端)
- if handler.Edition == "lite" {
+ // Lite bundles the frontend; other editions can explicitly opt in.
+ if handler.Edition == "lite" || strings.TrimSpace(os.Getenv("WEKNORA_WEB_DIR")) != "" {
serveFrontendStatic(r)
}
diff --git a/internal/router/router_static_test.go b/internal/router/router_static_test.go
index f8d7a1a98..2ec4d0557 100644
--- a/internal/router/router_static_test.go
+++ b/internal/router/router_static_test.go
@@ -7,10 +7,130 @@ import (
"path/filepath"
"testing"
+ "github.com/Tencent/WeKnora/internal/config"
+ "github.com/Tencent/WeKnora/internal/handler"
+ "github.com/Tencent/WeKnora/internal/mcpserver"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
+func TestNewRouterFrontendStaticEditionOptIn(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ for _, tc := range []struct {
+ name string
+ edition string
+ webDir string
+ wantIndex bool
+ }{
+ {name: "standard explicit directory", edition: "standard", webDir: "web", wantIndex: true},
+ {name: "standard default directory", edition: "standard"},
+ {name: "standard missing directory", edition: "standard", webDir: "missing"},
+ {name: "standard missing index", edition: "standard", webDir: "empty"},
+ {name: "lite default directory", edition: "lite", wantIndex: true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ root := t.TempDir()
+ t.Chdir(root)
+ require.NoError(t, os.MkdirAll(filepath.Join(root, "web", "assets"), 0o700))
+ require.NoError(t, os.Mkdir(filepath.Join(root, "empty"), 0o700))
+ index := "<!doctype html><html><body>WeKnora</body></html>"
+ asset := "console.log('WeKnora');"
+ require.NoError(t, os.WriteFile(filepath.Join(root, "web", "index.html"), []byte(index), 0o600))
+ require.NoError(t, os.WriteFile(filepath.Join(root, "web", "assets", "app-test.js"), []byte(asset), 0o600))
+ webDir := ""
+ if tc.webDir != "" {
+ webDir = filepath.Join(root, tc.webDir)
+ }
+ t.Setenv("WEKNORA_WEB_DIR", webDir)
+ t.Setenv("LOCAL_STORAGE_BASE_DIR", filepath.Join(root, "files"))
+ previousEdition := handler.Edition
+ handler.Edition = tc.edition
+ t.Cleanup(func() { handler.Edition = previousEdition })
+
+ r := NewRouter(RouterParams{
+ Config: &config.Config{},
+ SystemHandler: &handler.SystemHandler{},
+ MCPServer: &mcpserver.Server{},
+ MCPEndpointService: &stubMCPEndpointServiceForRoutes{},
+ })
+ for _, path := range []string{"/", "/login", "/assets/app-test.js"} {
+ recorder := httptest.NewRecorder()
+ r.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
+ if !tc.wantIndex {
+ require.Equal(t, http.StatusUnauthorized, recorder.Code, path)
+ continue
+ }
+ require.Equal(t, http.StatusOK, recorder.Code, path)
+ if path == "/assets/app-test.js" {
+ require.Equal(t, asset, recorder.Body.String())
+ } else {
+ require.Contains(t, recorder.Header().Get("Content-Type"), "text/html")
+ require.Equal(t, index, recorder.Body.String())
+ }
+ }
+
+ recorder := httptest.NewRecorder()
+ r.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/auth/me", nil))
+ require.Equal(t, http.StatusUnauthorized, recorder.Code)
+ require.Contains(t, recorder.Body.String(), "missing authentication")
+
+ // Anonymous requests must be rejected before the unused MCP transport.
+ for _, request := range []struct {
+ method string
+ path string
+ }{
+ {method: http.MethodGet, path: "/mcp/endpoint"},
+ {method: http.MethodHead, path: "/mcp/endpoint"},
+ {method: http.MethodGet, path: "/mcp/endpoint/unknown"},
+ } {
+ t.Run(request.method+" "+request.path, func(t *testing.T) {
+ recorder := httptest.NewRecorder()
+ r.ServeHTTP(recorder, httptest.NewRequest(request.method, request.path, nil))
+ require.Equal(t, http.StatusUnauthorized, recorder.Code)
+ require.Contains(t, recorder.Header().Get("Content-Type"), "application/json")
+ if request.method == http.MethodGet && request.path == "/mcp/endpoint" {
+ require.Equal(t, `Bearer realm="weknora-mcp"`, recorder.Header().Get("WWW-Authenticate"))
+ require.Contains(t, recorder.Body.String(), "missing bearer token")
+ }
+ })
+ }
+ })
+ }
+}
+
+func TestFrontendStaticDoesNotInterceptMatchedBackendRoute(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ webDir := t.TempDir()
+ require.NoError(t, os.Mkdir(filepath.Join(webDir, "backend"), 0o700))
+ require.NoError(t, os.WriteFile(filepath.Join(webDir, "index.html"), []byte("spa"), 0o600))
+ require.NoError(t, os.WriteFile(filepath.Join(webDir, "backend", "collision"), []byte("static file"), 0o600))
+ t.Setenv("WEKNORA_WEB_DIR", webDir)
+
+ for _, method := range []string{http.MethodGet, http.MethodHead} {
+ t.Run(method, func(t *testing.T) {
+ r := gin.New()
+ serveFrontendStatic(r)
+ authCalled := false
+ handlerCalled := false
+ r.Handle(method, "/backend/:id", func(c *gin.Context) {
+ authCalled = true
+ c.AbortWithStatus(http.StatusUnauthorized)
+ }, func(c *gin.Context) {
+ handlerCalled = true
+ c.Status(http.StatusOK)
+ })
+
+ recorder := httptest.NewRecorder()
+ r.ServeHTTP(recorder, httptest.NewRequest(method, "/backend/collision", nil))
+
+ require.Equal(t, http.StatusUnauthorized, recorder.Code)
+ require.True(t, authCalled, "matched backend authentication must run before static serving")
+ require.False(t, handlerCalled, "authentication rejection must stop the backend handler")
+ require.Empty(t, recorder.Header().Get("Cache-Control"))
+ })
+ }
+}
+
func TestFrontendStaticDoesNotInterceptResourceGrant(t *testing.T) {
gin.SetMode(gin.TestMode)
webDir := t.TempDir()
diff --git a/internal/router/static.go b/internal/router/static.go
index 2acb63f08..cde666692 100644
--- a/internal/router/static.go
+++ b/internal/router/static.go
@@ -36,9 +36,14 @@ func serveFrontendStatic(r *gin.Engine) {
c.Next()
return
}
+ // Gin resolves registered routes before middleware; preserve their auth and handlers.
+ if c.FullPath() != "" {
+ c.Next()
+ return
+ }
path := c.Request.URL.Path
if strings.HasPrefix(path, "/api/") || strings.HasPrefix(path, "/health") || strings.HasPrefix(path, "/swagger/") ||
- strings.HasPrefix(path, "/r/") || path == "/files" {
+ strings.HasPrefix(path, "/mcp/") || strings.HasPrefix(path, "/r/") || path == "/files" {
c.Next()
return
}
diff --git a/website-docs/01-getting-started/02-installation.md b/website-docs/01-getting-started/02-installation.md
index beb91ad35..5504fca60 100644
--- a/website-docs/01-getting-started/02-installation.md
+++ b/website-docs/01-getting-started/02-installation.md
@@ -283,6 +283,22 @@ cd docreader && uv sync --locked && bash scripts/generate_proto.sh && uv run -m
配置文件查找顺序(`internal/config/config.go` 的 `LoadConfig`):当前目录 → `./config` → `$HOME/.appname` → `/etc/appname/`,文件名 `config.yaml`。
+### 标准版可选托管前端
+
+标准版默认仅提供 API,前端由独立服务提供。若希望由同一个标准版进程提供 Web UI,可显式设置 `WEKNORA_WEB_DIR`,指向已构建的前端目录;该目录必须包含 `index.html`。未设置该变量时保持默认行为,目录不存在或缺少 `index.html` 时也不会启用前端托管。
+
+以下命令从仓库根目录执行,要求已安装 Go 1.26、CGO 所需工具与依赖、Node.js 和 npm,并已提供标准版所需的数据库、Redis、存储、模型服务及所用解析服务等配置与依赖:
+
+```bash
+(cd frontend && npm ci && npm run build)
+make build-prod
+SERVER_HOST=127.0.0.1 WEKNORA_WEB_DIR="$PWD/frontend/dist" ./WeKnora
+```
+
+启动后通过 `http://127.0.0.1:8080` 访问(使用默认 `SERVER_PORT` 时)。`WEKNORA_WEB_DIR` 目录仅应包含前端构建产物,不要放入数据、配置或密钥;其中的文件会作为静态资源提供。已注册的后端路由(包括 `/mcp/` 端点)优先于静态文件和 SPA 回退,现有 API 的认证与授权规则保持不变。
+
+此选项可省去独立前端服务,不会替代数据库、Redis 等后端依赖。需要 SQLite + 内存队列的最小化无 Docker 部署时,请参考现有的 [Lite 运行时](#_7-1-lite-运行时-零外部依赖)。
+
## 常见部署拓扑
```mermaid