feat: add Go current-session HTTP adapter

This commit is contained in:
zn-admin committed 2026-08-13 13:39:41 +08:00
1 parent c849077591
commit 772795e7eb
10 files changed
+1608 -7

No files matched your search

+35 -1
View File
@@ -7,6 +7,7 @@ import (
"os"
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/httpapi"
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/identity"
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/postgres"
)
@@ -14,6 +15,10 @@ type Options struct {
Context context.Context
Getenv postgres.Getenv
ReadFile postgres.ReadFile
// AuthorizationLoader is an optional Identity persistence Adapter used by
// composition tests and alternate runtime backends. Production defaults to
// the PostgreSQL Store opened below.
AuthorizationLoader identity.AuthorizationSnapshotLoader
}
type App struct {
@@ -35,6 +40,10 @@ func New(options Options) (*App, error) {
readFile = os.ReadFile
}
authConfig, err := ParseAuthConfig(getenv)
if err != nil {
return nil, err
}
config, err := postgres.ParseConfig(getenv, readFile)
if err != nil {
return nil, err
@@ -43,10 +52,35 @@ func New(options Options) (*App, error) {
if err != nil {
return nil, err
}
closeOnError := true
defer func() {
if closeOnError {
database.Close()
}
}()
readiness := databaseReadiness{config: config, store: database.Store}
var resolver httpapi.SessionResolver
if authConfig.Configured {
loader := options.AuthorizationLoader
if loader == nil {
loader = database.Store
}
resolver = identity.NewResolver(loader, authConfig.SessionSecret, "platform", nil)
}
authMe, err := httpapi.NewAuthMeHandler(httpapi.AuthState{
Required: authConfig.Required, Configured: authConfig.Configured,
}, resolver)
if err != nil {
return nil, err
}
foundation := httpapi.NewHandler(readiness)
mux := http.NewServeMux()
mux.Handle("/api/auth/me", authMe)
mux.Handle("/", foundation)
closeOnError = false
return &App{
db: database,
handler: httpapi.NewHandler(readiness),
handler: mux,
}, nil
}