feat: add Go migration compatibility foundation
This commit is contained in:
1 parent
7de3300034
commit
716a8031b1
27 files changed
+2582
No files matched your search
@@ -0,0 +1,79 @@
|
||||
// Package application composes the Go backend foundation Modules.
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/httpapi"
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/postgres"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Context context.Context
|
||||
Getenv postgres.Getenv
|
||||
ReadFile postgres.ReadFile
|
||||
}
|
||||
|
||||
type App struct {
|
||||
handler http.Handler
|
||||
db *postgres.Module
|
||||
}
|
||||
|
||||
func New(options Options) (*App, error) {
|
||||
ctx := options.Context
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
getenv := options.Getenv
|
||||
if getenv == nil {
|
||||
getenv = os.Getenv
|
||||
}
|
||||
readFile := options.ReadFile
|
||||
if readFile == nil {
|
||||
readFile = os.ReadFile
|
||||
}
|
||||
|
||||
config, err := postgres.ParseConfig(getenv, readFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
database, err := postgres.Open(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
readiness := databaseReadiness{config: config, store: database.Store}
|
||||
return &App{
|
||||
db: database,
|
||||
handler: httpapi.NewHandler(readiness),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (app *App) Handler() http.Handler {
|
||||
return app.handler
|
||||
}
|
||||
|
||||
func (app *App) Close() {
|
||||
if app.db != nil {
|
||||
app.db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
type databaseReadiness struct {
|
||||
config postgres.Config
|
||||
store *postgres.Store
|
||||
}
|
||||
|
||||
func (adapter databaseReadiness) Status() httpapi.DatabaseStatus {
|
||||
configured := adapter.config.Backend == postgres.BackendLocal || adapter.config.DatabaseURL != ""
|
||||
return httpapi.DatabaseStatus{
|
||||
Backend: string(adapter.config.Backend),
|
||||
Configured: configured,
|
||||
}
|
||||
}
|
||||
|
||||
func (adapter databaseReadiness) Ready(ctx context.Context) (httpapi.DatabaseStatus, error) {
|
||||
status := adapter.Status()
|
||||
return status, adapter.store.Readiness(ctx)
|
||||
}
|
||||
Reference in new issue
Block a user