feat(server): hot-reload config store and reloadable infra clients
- Replace single Load() with a watching Store that re-reads config files
(and config.local.yaml) and fans out a ReloadEvent with a per-field diff
so consumers can decide whether the change is hot-applicable or requires
a process restart.
- Wrap llm, retrieval, vector store, and object storage clients in
Reloadable* shells so the bootstrap can swap their underlying impls when
config changes without re-instantiating handlers.
- Make jwt.Manager and ops TokenIssuer mutable under a lock so secrets and
TTLs can be rotated live; thread default plan code through a setter on
the ops AdminUserService.
- Wire ConfigStore through bootstrap and every cmd/main.go, scheduler /
worker / tenant-api / ops-api start the watcher; services and handlers
take a config.Provider so they always read current values for things
like generation.stream_enabled, scheduler dispatch, retrieval, etc.
- Switch shared/config decoding off viper to a Kratos-derived runtime
package so env placeholders (\${VAR:default}) resolve consistently and
the same source machinery powers both the loader and the watcher.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -24,7 +24,7 @@ type ArticleHandler struct {
|
||||
promptGenerateSvc *app.PromptRuleGenerationService
|
||||
imitationSvc *app.ArticleImitationService
|
||||
streams *stream.GenerationHub
|
||||
streamEnabled bool
|
||||
app *bootstrap.App
|
||||
assets *AssetHandler
|
||||
}
|
||||
|
||||
@@ -36,40 +36,40 @@ func NewArticleHandler(a *bootstrap.App) *ArticleHandler {
|
||||
a.ObjectStorage,
|
||||
a.LLM,
|
||||
a.Logger,
|
||||
a.Config.Retrieval,
|
||||
a.Config.LLM,
|
||||
a.Config().Retrieval,
|
||||
a.Config().LLM,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
).WithConfigProvider(a.ConfigStore)
|
||||
|
||||
return &ArticleHandler{
|
||||
svc: app.NewArticleService(a.DB, a.AuditLogs, a.ObjectStorage).WithCache(a.Cache),
|
||||
selectionOptimize: app.NewArticleSelectionOptimizeService(
|
||||
a.DB,
|
||||
a.LLM,
|
||||
a.Config.LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache),
|
||||
a.Config().LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache).WithConfigProvider(a.ConfigStore),
|
||||
promptGenerateSvc: app.NewPromptRuleGenerationService(
|
||||
a.DB,
|
||||
a.LLM,
|
||||
a.RabbitMQ,
|
||||
knowledgeSvc,
|
||||
a.GenerationStreams,
|
||||
a.Config.Generation,
|
||||
a.Config.LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache).WithLogger(a.Logger),
|
||||
a.Config().Generation,
|
||||
a.Config().LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache).WithLogger(a.Logger).WithConfigProvider(a.ConfigStore),
|
||||
imitationSvc: app.NewArticleImitationService(
|
||||
a.DB,
|
||||
a.LLM,
|
||||
a.RabbitMQ,
|
||||
knowledgeSvc,
|
||||
a.GenerationStreams,
|
||||
a.Config.Generation,
|
||||
a.Config.LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache),
|
||||
streams: a.GenerationStreams,
|
||||
streamEnabled: a.Config.Generation.StreamEnabled,
|
||||
assets: NewAssetHandler(a),
|
||||
a.Config().Generation,
|
||||
a.Config().LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache).WithConfigProvider(a.ConfigStore),
|
||||
streams: a.GenerationStreams,
|
||||
app: a,
|
||||
assets: NewAssetHandler(a),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,7 +396,8 @@ func (h *ArticleHandler) OptimizeSelectionStream(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *ArticleHandler) Stream(c *gin.Context) {
|
||||
if !h.streamEnabled {
|
||||
cfg := h.app.Config()
|
||||
if cfg == nil || !cfg.Generation.StreamEnabled {
|
||||
response.Error(c, response.ErrNotFound(40412, "generation_stream_disabled", "generation stream is disabled"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -20,15 +20,11 @@ import (
|
||||
)
|
||||
|
||||
type AssetHandler struct {
|
||||
secret string
|
||||
app *bootstrap.App
|
||||
app *bootstrap.App
|
||||
}
|
||||
|
||||
func NewAssetHandler(app *bootstrap.App) *AssetHandler {
|
||||
return &AssetHandler{
|
||||
secret: strings.TrimSpace(app.Config.JWT.Secret),
|
||||
app: app,
|
||||
}
|
||||
return &AssetHandler{app: app}
|
||||
}
|
||||
|
||||
func (h *AssetHandler) BuildArticleImageURL(objectKey string) string {
|
||||
@@ -109,12 +105,19 @@ func convertPublicAssetFormat(content []byte, format string) ([]byte, string, bo
|
||||
|
||||
func (h *AssetHandler) signObjectKey(objectKey string) string {
|
||||
encodedKey := base64.RawURLEncoding.EncodeToString([]byte(objectKey))
|
||||
mac := hmac.New(sha256.New, []byte(h.secret))
|
||||
mac := hmac.New(sha256.New, []byte(h.secret()))
|
||||
_, _ = mac.Write([]byte(objectKey))
|
||||
signature := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
return fmt.Sprintf("%s.%s", encodedKey, signature)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) secret() string {
|
||||
if h == nil || h.app == nil || h.app.Config() == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(h.app.Config().JWT.Secret)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) parseToken(token string) (string, bool) {
|
||||
parts := strings.Split(token, ".")
|
||||
if len(parts) != 2 {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
||||
"github.com/geo-platform/tenant-api/internal/shared/config"
|
||||
"github.com/geo-platform/tenant-api/internal/tenant/repository"
|
||||
)
|
||||
|
||||
@@ -50,10 +51,9 @@ func TestAssetHandlerServeDesktop_AllowsStaleSignedTenantAsset(t *testing.T) {
|
||||
objectKey := "tenants/7/images/test.webp"
|
||||
storage := &fakeAssetObjectStorage{content: mustDecodeBase64(t, "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=")}
|
||||
handler := &AssetHandler{
|
||||
secret: "current-secret",
|
||||
app: &bootstrap.App{ObjectStorage: storage},
|
||||
app: testAssetApp(storage, "current-secret"),
|
||||
}
|
||||
staleToken := (&AssetHandler{secret: "old-secret"}).signObjectKey(objectKey)
|
||||
staleToken := testSignedAssetToken(objectKey, "old-secret")
|
||||
|
||||
router := gin.New()
|
||||
router.GET("/api/desktop/content/assets/:token", func(c *gin.Context) {
|
||||
@@ -76,10 +76,9 @@ func TestAssetHandlerServeDesktop_RejectsOtherTenantAsset(t *testing.T) {
|
||||
|
||||
storage := &fakeAssetObjectStorage{content: []byte("not used")}
|
||||
handler := &AssetHandler{
|
||||
secret: "current-secret",
|
||||
app: &bootstrap.App{ObjectStorage: storage},
|
||||
app: testAssetApp(storage, "current-secret"),
|
||||
}
|
||||
token := (&AssetHandler{secret: "old-secret"}).signObjectKey("tenants/8/images/test.webp")
|
||||
token := testSignedAssetToken("tenants/8/images/test.webp", "old-secret")
|
||||
|
||||
router := gin.New()
|
||||
router.GET("/api/desktop/content/assets/:token", func(c *gin.Context) {
|
||||
@@ -105,3 +104,15 @@ func mustDecodeBase64(t *testing.T, value string) []byte {
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func testAssetApp(storage *fakeAssetObjectStorage, secret string) *bootstrap.App {
|
||||
store, err := config.NewStaticStore(&config.Config{JWT: config.JWTConfig{Secret: secret}})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &bootstrap.App{ConfigStore: store, ObjectStorage: storage}
|
||||
}
|
||||
|
||||
func testSignedAssetToken(objectKey, secret string) string {
|
||||
return (&AssetHandler{app: testAssetApp(nil, secret)}).signObjectKey(objectKey)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ type BrandHandler struct {
|
||||
}
|
||||
|
||||
func NewBrandHandler(a *bootstrap.App) *BrandHandler {
|
||||
return &BrandHandler{svc: app.NewBrandService(a.DB, a.MonitoringDB, a.AuditLogs, a.Config.BrandLibrary).WithCache(a.Cache)}
|
||||
return &BrandHandler{svc: a.BrandService}
|
||||
}
|
||||
|
||||
func (h *BrandHandler) List(c *gin.Context) {
|
||||
|
||||
@@ -11,14 +11,14 @@ import (
|
||||
)
|
||||
|
||||
func registerInternalMetricsRoutes(app *bootstrap.App) {
|
||||
token := strings.TrimSpace(app.Config.Scheduler.InternalMetricsToken)
|
||||
token := strings.TrimSpace(app.Config().Scheduler.InternalMetricsToken)
|
||||
if token == "" {
|
||||
app.Logger.Warn("tenant-api internal metrics routes not registered: scheduler.internal_metrics_token is required")
|
||||
return
|
||||
}
|
||||
|
||||
metrics := app.Engine.Group("/api/internal/metrics")
|
||||
metrics.Use(internalMetricsAuthMiddleware(token))
|
||||
metrics.Use(internalMetricsAuthMiddleware(app))
|
||||
metrics.GET("/monitoring/heartbeat-primary-lease", func(c *gin.Context) {
|
||||
response.Success(c, tenantapp.MonitoringHeartbeatPrimaryLeaseMetricsSnapshotValue())
|
||||
})
|
||||
@@ -35,9 +35,12 @@ func registerInternalMetricsRoutes(app *bootstrap.App) {
|
||||
})
|
||||
}
|
||||
|
||||
func internalMetricsAuthMiddleware(token string) gin.HandlerFunc {
|
||||
expected := strings.TrimSpace(token)
|
||||
func internalMetricsAuthMiddleware(app *bootstrap.App) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
expected := ""
|
||||
if cfg := app.Config(); cfg != nil {
|
||||
expected = strings.TrimSpace(cfg.Scheduler.InternalMetricsToken)
|
||||
}
|
||||
if expected == "" || !internalMetricsTokenMatches(c, expected) {
|
||||
response.Error(c, response.ErrUnauthorized(40181, "internal_metrics_unauthorized", "valid internal metrics token is required"))
|
||||
c.Abort()
|
||||
|
||||
@@ -25,11 +25,11 @@ func NewKnowledgeHandler(a *bootstrap.App) *KnowledgeHandler {
|
||||
a.ObjectStorage,
|
||||
a.LLM,
|
||||
a.Logger,
|
||||
a.Config.Retrieval,
|
||||
a.Config.LLM,
|
||||
a.Config.Generation.QueueSize,
|
||||
a.Config.Generation.WorkerConcurrency,
|
||||
),
|
||||
a.Config().Retrieval,
|
||||
a.Config().LLM,
|
||||
a.Config().Generation.QueueSize,
|
||||
a.Config().Generation.WorkerConcurrency,
|
||||
).WithConfigProvider(a.ConfigStore),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ func NewKolManageHandler(a *bootstrap.App, assets *AssetHandler) *KolManageHandl
|
||||
profileSvc: profileSvc,
|
||||
pkgSvc: app.NewKolPackageService(profileSvc, a.KolPackages, a.KolPrompts, a.KolSubscriptions).WithCache(a.Cache),
|
||||
promptSvc: app.NewKolPromptService(a.DB, profileSvc, a.KolPackages, a.KolPrompts, a.KolSubscriptions, a.KolPromptAsset).WithCache(a.Cache),
|
||||
assistSvc: app.NewKolAssistService(a.DB, profileSvc, a.KolAssists, a.LLM, a.RabbitMQ, a.Config.LLM, a.Logger).WithCache(a.Cache),
|
||||
assistSvc: app.NewKolAssistService(a.DB, profileSvc, a.KolAssists, a.LLM, a.RabbitMQ, a.Config().LLM, a.Logger).WithCache(a.Cache).WithConfigProvider(a.ConfigStore),
|
||||
assets: assets,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ type monitoringCollectNowRequest struct {
|
||||
|
||||
func NewMonitoringHandler(a *bootstrap.App) *MonitoringHandler {
|
||||
return &MonitoringHandler{
|
||||
svc: app.NewMonitoringService(a.DB, a.MonitoringDB, a.RabbitMQ, a.Config.MonitoringDispatch, a.Config.BrandLibrary, a.Logger).WithRedis(a.Redis),
|
||||
svc: a.MonitoringService,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -247,7 +247,7 @@ func RegisterRoutes(app *bootstrap.App) {
|
||||
imageFolders.GET("/:id/delete-preview", imgHandler.FolderDeletePreview)
|
||||
imageFolders.DELETE("/:id", imgHandler.DeleteFolder)
|
||||
|
||||
if swagger.EnabledForMode(app.Config.Server.Mode) {
|
||||
if swagger.EnabledForMode(app.Config().Server.Mode) {
|
||||
swagger.Register(app.Engine, swagger.Config{
|
||||
Title: "省心推 Admin API",
|
||||
Version: "dev",
|
||||
|
||||
@@ -23,11 +23,11 @@ func NewTemplateHandler(a *bootstrap.App) *TemplateHandler {
|
||||
a.ObjectStorage,
|
||||
a.LLM,
|
||||
a.Logger,
|
||||
a.Config.Retrieval,
|
||||
a.Config.LLM,
|
||||
a.Config().Retrieval,
|
||||
a.Config().LLM,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
).WithConfigProvider(a.ConfigStore)
|
||||
|
||||
return &TemplateHandler{
|
||||
svc: app.NewTemplateService(
|
||||
@@ -40,9 +40,9 @@ func NewTemplateHandler(a *bootstrap.App) *TemplateHandler {
|
||||
a.RabbitMQ,
|
||||
knowledgeSvc,
|
||||
a.GenerationStreams,
|
||||
a.Config.Generation,
|
||||
a.Config.LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache),
|
||||
a.Config().Generation,
|
||||
a.Config().LLM.MaxOutputTokens,
|
||||
).WithCache(a.Cache).WithConfigProvider(a.ConfigStore),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user