mirror of
https://github.com/Tencent/WeKnora.git
synced 2026-11-30 15:47:25 +08:00
Why:
1. Frontend i18n bundle & conflict bottleneck:
frontend/src/i18n/index.ts statically imported all six monolithic locale
files (3.27 MB raw across en-US, zh-CN, zh-TW, ko-KR, ja-JP, ru-RU).
Rollup fused all six locales into the shared synchronous entry chunk
preloaded by both index.html and embed.html (4,435 kB minified / 1,299 kB
gzip), penalizing first paint on both surfaces and causing frequent merge
conflicts on single 14k-line locale files.
2. Backend Swagger duplication & drift:
docs/docs.go (1.73 MB, 44,791 lines) duplicated docs/swagger.json inside a
generated Go string literal, doubling merge conflicts across 4.49 MB of
committed Swagger artifacts while lacking a CI sync check against Go
annotations (allowing swagger.json and swagger.yaml on main to drift).
What:
- Partition each of the 6 locales into 9 cohesive business-domain modules
under frontend/src/i18n/locales/<locale>/{common,auth,chat,agent,knowledge,
datasource,model,settings,system}.ts (54 domain files) with lightweight
locales/<locale>.ts barrel re-exports so existing tests and audit scripts
remain compatible; update localeKeyAudit.ts / localeSerialize.ts so
check-i18n, scan-i18n-gaps, and regenerate-i18n-locales read and write the
domain-split layout losslessly.
- Refactor frontend/src/i18n/index.ts to load locales on demand via dynamic
import(), await initI18n() in main.ts before mount, await setI18nLanguage()
in Login.vue and GeneralSettings.vue, and skip main-locale loading on the
embed surface — cutting the shared preloaded chunk from 4,435.03 kB (1,298.68
kB gzip) to 1,544.92 kB (424.15 kB gzip) (-2.89 MB minified / -874.5 kB
gzip on embed.html, and -86% locale payload on index.html).
- Replace generated docs/docs.go (1.73 MB -> 2.6 KB) with //go:embed
swagger.json and runtime metadata override support, add scripts/swagger-gen
and Makefile docs/docs-check targets pinned to go.mod's swag version, remove
the obsolete docs/docs.go lint exclusion, regenerate synced swagger.{json,yaml},
and add TestSwaggerDocsSync in docs/swagger_sync_test.go.
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
// Package docs exposes the embedded OpenAPI 2.0 specification for Swagger UI
|
|
// and runtime operation discovery without duplicating swagger.json in Go code.
|
|
package docs
|
|
|
|
import (
|
|
_ "embed"
|
|
"encoding/json"
|
|
|
|
"github.com/swaggo/swag"
|
|
)
|
|
|
|
//go:embed swagger.json
|
|
var swaggerJSON string
|
|
|
|
const (
|
|
defaultVersion = "1.0"
|
|
defaultHost = ""
|
|
defaultBasePath = "/api/v1"
|
|
defaultTitle = "WeKnora API"
|
|
defaultDescription = "WeKnora 知识库管理系统 API 文档"
|
|
)
|
|
|
|
// Spec wraps swag.Spec so callers can read or override metadata fields while
|
|
// serving the embedded swagger.json directly in the common unmodified path.
|
|
type Spec struct {
|
|
swag.Spec
|
|
}
|
|
|
|
// ReadDoc returns the OpenAPI JSON document, applying any runtime metadata
|
|
// overrides configured on SwaggerInfo without running text/template over
|
|
// endpoint descriptions.
|
|
func (s *Spec) ReadDoc() string {
|
|
if s.isDefault() {
|
|
return swaggerJSON
|
|
}
|
|
|
|
var root map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(swaggerJSON), &root); err != nil {
|
|
return swaggerJSON
|
|
}
|
|
|
|
if s.Host != "" {
|
|
if raw, err := json.Marshal(s.Host); err == nil {
|
|
root["host"] = raw
|
|
}
|
|
} else {
|
|
delete(root, "host")
|
|
}
|
|
|
|
if s.BasePath != "" {
|
|
if raw, err := json.Marshal(s.BasePath); err == nil {
|
|
root["basePath"] = raw
|
|
}
|
|
} else {
|
|
delete(root, "basePath")
|
|
}
|
|
|
|
if len(s.Schemes) > 0 {
|
|
if raw, err := json.Marshal(s.Schemes); err == nil {
|
|
root["schemes"] = raw
|
|
}
|
|
} else {
|
|
delete(root, "schemes")
|
|
}
|
|
|
|
var info map[string]any
|
|
if rawInfo, ok := root["info"]; ok {
|
|
_ = json.Unmarshal(rawInfo, &info)
|
|
}
|
|
if info == nil {
|
|
info = make(map[string]any, 3)
|
|
}
|
|
info["version"] = s.Version
|
|
info["title"] = s.Title
|
|
info["description"] = s.Description
|
|
if rawInfo, err := json.Marshal(info); err == nil {
|
|
root["info"] = rawInfo
|
|
}
|
|
|
|
out, err := json.Marshal(root)
|
|
if err != nil {
|
|
return swaggerJSON
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
func (s *Spec) isDefault() bool {
|
|
return s.Version == defaultVersion &&
|
|
s.Host == defaultHost &&
|
|
s.BasePath == defaultBasePath &&
|
|
len(s.Schemes) == 0 &&
|
|
s.Title == defaultTitle &&
|
|
s.Description == defaultDescription
|
|
}
|
|
|
|
// SwaggerInfo holds exported Swagger metadata so callers can inspect or
|
|
// override Host, BasePath, Schemes, Version, Title, or Description at runtime.
|
|
var SwaggerInfo = &Spec{
|
|
Spec: swag.Spec{
|
|
Version: defaultVersion,
|
|
Host: defaultHost,
|
|
BasePath: defaultBasePath,
|
|
Schemes: []string{},
|
|
Title: defaultTitle,
|
|
Description: defaultDescription,
|
|
InfoInstanceName: "swagger",
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
|
|
}
|