forked from mtusi2026/mtusi2026lab3
67 lines
1.5 KiB
Go
Executable File
67 lines
1.5 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/config"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/database"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session"
|
|
)
|
|
|
|
func main() {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("configuration: %v", err)
|
|
}
|
|
|
|
rootCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
pool, err := database.Connect(rootCtx, cfg.DatabaseURL, cfg.DBMaxConns, cfg.StartupWait)
|
|
if err != nil {
|
|
log.Fatalf("database: %v", err)
|
|
}
|
|
defer pool.Close()
|
|
|
|
migrateCtx, cancelMigrate := context.WithTimeout(rootCtx, 60*time.Second)
|
|
err = database.Migrate(migrateCtx, pool, cfg.DatabaseURL)
|
|
cancelMigrate()
|
|
if err != nil {
|
|
log.Fatalf("migrations: %v", err)
|
|
}
|
|
|
|
sessions, err := session.New(cfg.CacheURL, cfg.SessionTTL, cfg.RedisTimeout)
|
|
if err != nil {
|
|
log.Fatalf("session store: %v", err)
|
|
}
|
|
defer sessions.Close()
|
|
|
|
app := internal.New(cfg, pool, sessions)
|
|
|
|
addr := net.JoinHostPort(cfg.Host, cfg.Port)
|
|
listenErr := make(chan error, 1)
|
|
go func() {
|
|
listenErr <- app.Listen(addr)
|
|
}()
|
|
|
|
select {
|
|
case err = <-listenErr:
|
|
if err != nil {
|
|
log.Fatalf("server: %v", err)
|
|
}
|
|
case <-rootCtx.Done():
|
|
}
|
|
|
|
log.Printf("shutting down")
|
|
internal.SignalGracefulShutdown()
|
|
if err = app.ShutdownWithTimeout(10 * time.Second); err != nil {
|
|
log.Printf("shutdown: %v", err)
|
|
}
|
|
}
|