feat: complete remaining Go backend modules
This commit is contained in:
1 parent
cea2751dc5
commit
aef5a97165
145 files changed
+18376
-199
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/application"
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/settings"
|
||||
)
|
||||
|
||||
const shutdownTimeout = 10 * time.Second
|
||||
@@ -28,14 +29,18 @@ func run() error {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
app, err := application.New(application.Options{Context: ctx})
|
||||
getenv, err := runtimeGetenv(os.Getenv)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load Go backend runtime settings: %w", err)
|
||||
}
|
||||
app, err := application.New(application.Options{Context: ctx, Getenv: getenv})
|
||||
if err != nil {
|
||||
return fmt.Errorf("initialize Go backend: %w", err)
|
||||
}
|
||||
defer app.Close()
|
||||
|
||||
server := &http.Server{
|
||||
Addr: listenAddress(os.Getenv),
|
||||
Addr: listenAddress(getenv),
|
||||
Handler: app.Handler(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
}
|
||||
@@ -66,6 +71,29 @@ func run() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runtimeGetenv(process func(string) string) (func(string) string, error) {
|
||||
path := strings.TrimSpace(process("ZHINIAN_SETTINGS_FILE"))
|
||||
if path == "" {
|
||||
path = ".env.local"
|
||||
}
|
||||
values := map[string]string{}
|
||||
for _, key := range settings.RuntimeSettingKeys() {
|
||||
if value := process(key); value != "" {
|
||||
values[key] = value
|
||||
}
|
||||
}
|
||||
merged, err := settings.LoadEnvironment(path, values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(name string) string {
|
||||
if value, exists := merged[name]; exists {
|
||||
return value
|
||||
}
|
||||
return process(name)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func listenAddress(getenv func(string) string) string {
|
||||
host := strings.TrimSpace(getenv("GO_BACKEND_HOST"))
|
||||
if host == "" {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListenAddressDefaultsToLoopbackAndSupportsExplicitBinding(t *testing.T) {
|
||||
if got := listenAddress(func(string) string { return "" }); got != "127.0.0.1:8080" {
|
||||
@@ -11,3 +15,18 @@ func TestListenAddressDefaultsToLoopbackAndSupportsExplicitBinding(t *testing.T)
|
||||
t.Fatalf("listenAddress(explicit) = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeGetenvLoadsWhitelistedSettingsAndPreservesProcessPrecedence(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), ".env.local")
|
||||
if err := os.WriteFile(path, []byte("IMAGE_GENERATE_ENGINE=evolink\nEVOLINK_API_KEY=file-secret\nDATABASE_URL=postgres://must-not-load\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
process := map[string]string{"ZHINIAN_SETTINGS_FILE": path, "EVOLINK_API_KEY": "process-secret", "ZHINIAN_DATA_BACKEND": "local"}
|
||||
getenv, err := runtimeGetenv(func(name string) string { return process[name] })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if getenv("IMAGE_GENERATE_ENGINE") != "evolink" || getenv("EVOLINK_API_KEY") != "process-secret" || getenv("DATABASE_URL") != "" || getenv("ZHINIAN_DATA_BACKEND") != "local" {
|
||||
t.Fatalf("loaded engine=%q key=%q database=%q backend=%q", getenv("IMAGE_GENERATE_ENGINE"), getenv("EVOLINK_API_KEY"), getenv("DATABASE_URL"), getenv("ZHINIAN_DATA_BACKEND"))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user