ЛР1 на 4: in-memory API, Docker, student-tests и конкурентная проверка.

Реализован контракт lab1 (X-User-Id), non-root образ, SIGTERM ≤10s, DEFENSE/REPORT.
This commit is contained in:
2026-12-01 21:50:50 +03:00
parent 16726dfb97
commit 2cd45ab58e
21 changed files with 1409 additions and 18 deletions
+83
View File
@@ -0,0 +1,83 @@
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"mtusi2026lab1/internal/api"
"mtusi2026lab1/internal/config"
"mtusi2026lab1/internal/store"
)
func main() {
healthcheck := flag.Bool("healthcheck", false, "exit 0 if /health/ready is ok")
flag.Parse()
if *healthcheck {
os.Exit(probeReady())
}
cfg, err := config.Load()
if err != nil {
log.Fatalf("config: %v", err)
}
db := store.New()
svc := api.New(db)
server := &http.Server{
Addr: cfg.Addr(),
Handler: svc.Routes(),
ReadHeaderTimeout: 5 * time.Second,
}
go func() {
log.Printf("starting messenger env=%s listen=%s", cfg.Env, cfg.Addr())
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %v", err)
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
received := <-sig
log.Printf("signal %v received", received)
svc.BeginDrain()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("shutdown: %v", err)
os.Exit(1)
}
log.Printf("shutdown complete")
}
func probeReady() int {
cfg, err := config.Load()
if err != nil {
return 1
}
host := cfg.Host
if host == "0.0.0.0" || host == "::" {
host = "127.0.0.1"
}
url := fmt.Sprintf("http://%s:%d/health/ready", host, cfg.Port)
client := &http.Client{Timeout: 2 * time.Second}
resp, err := client.Get(url)
if err != nil {
return 1
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
if resp.StatusCode != http.StatusOK {
return 1
}
return 0
}