feat(platform): add postgres, redis, logging and graceful http serving
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
9662a3cea2
commit
1a6fadb030
6 files changed
+231
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/migrations"
|
||||
)
|
||||
|
||||
type Deps struct {
|
||||
Log *slog.Logger
|
||||
DB *pgxpool.Pool
|
||||
Redis *redis.Client
|
||||
}
|
||||
|
||||
func (d *Deps) Close() {
|
||||
if d.DB != nil {
|
||||
d.DB.Close()
|
||||
}
|
||||
if d.Redis != nil {
|
||||
_ = d.Redis.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Deps) Probes() map[string]Probe {
|
||||
return map[string]Probe{
|
||||
"postgres": func(ctx context.Context) error { return d.DB.Ping(ctx) },
|
||||
"redis": func(ctx context.Context) error { return d.Redis.Ping(ctx).Err() },
|
||||
}
|
||||
}
|
||||
|
||||
func SignalContext() (context.Context, context.CancelFunc) {
|
||||
return signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
}
|
||||
|
||||
func Connect(ctx context.Context, c config.Common, log *slog.Logger) (*Deps, error) {
|
||||
if err := c.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pending, err := migrations.Pending(ctx, c.DatabaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if pending {
|
||||
return nil, fmt.Errorf("database schema is behind this release, run \"api migrate up\" first")
|
||||
}
|
||||
d := &Deps{Log: log}
|
||||
if d.DB, err = OpenPostgres(ctx, c.DatabaseURL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d.Redis, err = OpenRedis(ctx, c.RedisURL); err != nil {
|
||||
d.Close()
|
||||
return nil, err
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func NewRouter() *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
return r
|
||||
}
|
||||
Reference in new issue
Block a user