forked from mtusi2026/mtusi2026lab2
Перенесена ЛР1
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab2/internal/store"
|
||||
"mtusi2026lab2/internal/transport/http/handlers"
|
||||
"mtusi2026lab2/internal/transport/http/middleware"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := os.Getenv("HTTP_ADDR")
|
||||
if addr == "" {
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
addr = "0.0.0.0:" + port
|
||||
}
|
||||
|
||||
memStore := store.NewMemoryStore()
|
||||
h := handlers.NewHandler(memStore)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
//паблик систем ручки
|
||||
mux.HandleFunc("GET /health/live", h.HealthLive)
|
||||
mux.HandleFunc("GET /health/ready", h.HealthReady)
|
||||
|
||||
// паблик рег юзера
|
||||
mux.HandleFunc("POST /api/v1/users", h.CreateUser)
|
||||
|
||||
//защищенные через мидлварьки
|
||||
mux.HandleFunc("GET /api/v1/users/me", middleware.RequireAuth(memStore, h.GetMe))
|
||||
|
||||
mux.HandleFunc("POST /api/v1/chats", middleware.RequireAuth(memStore, h.CreateChat))
|
||||
mux.HandleFunc("GET /api/v1/chats", middleware.RequireAuth(memStore, h.ListChats))
|
||||
mux.HandleFunc("GET /api/v1/chats/{chat_id}", middleware.RequireAuth(memStore, h.GetChat))
|
||||
|
||||
mux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", middleware.RequireAuth(memStore, h.CreateMessage))
|
||||
mux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", middleware.RequireAuth(memStore, h.GetMessages))
|
||||
|
||||
//конвейер
|
||||
var rootHandler http.Handler = mux
|
||||
rootHandler = middleware.Recovery(rootHandler)
|
||||
rootHandler = middleware.Logger(rootHandler)
|
||||
rootHandler = middleware.RequestID(rootHandler)
|
||||
|
||||
//конф сервака
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: rootHandler,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
//серв в рутине запущен
|
||||
serverErrors := make(chan error, 1)
|
||||
go func() {
|
||||
log.Printf("[INFO] Server starting on %s", addr)
|
||||
serverErrors <- server.ListenAndServe()
|
||||
}()
|
||||
|
||||
shutdown := make(chan os.Signal, 1)
|
||||
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
//падение/сигнал на выкл
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("[FATAL] Server crashed: %v", err)
|
||||
}
|
||||
|
||||
case sig := <-shutdown:
|
||||
log.Printf("[INFO] Signal %v received. Starting graceful shutdown...", sig)
|
||||
|
||||
// завершение не более чем за 10 секунд
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(ctx); err != nil {
|
||||
log.Printf("[WARN] Graceful shutdown failed: %v. Forcing close.", err)
|
||||
_ = server.Close()
|
||||
}
|
||||
log.Println("[INFO] Server stopped gracefully.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user