This commit is contained in:
Grbisba
2026-11-07 10:41:21 +03:00
parent 4c066ff191
commit 1e0d18e25b
306 changed files with 14036 additions and 627 deletions
+26
View File
@@ -9,6 +9,7 @@ import (
"syscall"
"time"
"encoding/json"
"gitea.privcloud.tech/mtusi2026/internal/adapter/controller/http"
"gitea.privcloud.tech/mtusi2026/internal/adapter/repository/cache/outside"
"gitea.privcloud.tech/mtusi2026/internal/adapter/repository/storage"
@@ -25,6 +26,8 @@ import (
"gitea.privcloud.tech/mtusi2026/internal/service/auth"
"gitea.privcloud.tech/mtusi2026/internal/service/chat"
"gitea.privcloud.tech/mtusi2026/internal/service/user"
"github.com/google/uuid"
stdhttp "net/http"
)
func main() {
@@ -41,9 +44,27 @@ func run() error {
return fmt.Errorf("load server config: %w", err)
}
if len(os.Args) > 1 && os.Args[1] == "migrate" {
cfg.Postgres.RunMigrations = true
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
return migrator.New(cfg).Start(ctx)
}
// API processes never run migrations; the one-shot migrator owns schema changes.
cfg.Postgres.RunMigrations = false
var level slog.Level
if err := level.UnmarshalText([]byte(cfg.Application.LogLevel)); err != nil {
return err
}
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})))
hostname, _ := os.Hostname()
instanceID := hostname + "-" + uuid.NewString()
slog.Info("instance_started", "instance_id", instanceID)
pgx := postgres.New(cfg.Postgres)
rx := cache.New(cfg)
postgresMigrator := migrator.New(cfg)
defer pgx.Close()
defer rx.Close()
tm := txmanager.New(pgx)
provider := jwtprovider.New(cfg)
encryptor := encrypting.New()
@@ -61,10 +82,15 @@ func run() error {
httpRouter := router.New(
middleware.NewAuthMiddleware(authSvc),
middleware.Instance(instanceID),
middleware.RequestIDMiddleware(),
middleware.LoggingMiddleware(),
middleware.RequestTimeout(cfg.Postgres.QueryTimeout),
)
httpRouter.Get("/internal/metrics", func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
stat := pgx.Stat()
_ = json.NewEncoder(w).Encode(map[string]any{"instance_id": instanceID, "acquire_count": stat.AcquireCount(), "acquire_duration_ns": stat.AcquireDuration().Nanoseconds(), "empty_acquire_count": stat.EmptyAcquireCount(), "acquired_conns": stat.AcquiredConns(), "total_conns": stat.TotalConns(), "max_conns": stat.MaxConns()})
})
server := http.New(cfg, httpRouter)
requestValidator := validator.NewValidatorImpl()
registerRoutes(