Remove Go doc comments, TS/Svelte scaffold comments and explanatory notes left over from the initial scaffold, plus a few leftover Spanish notes in the compose file, SQL scripts and env template. Also drops frontend/src/lib/index.ts, which only held the SvelteKit placeholder comment and was never imported.
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/go-chi/cors"
|
|
|
|
dockerconn "dockermanager/backend/internal/docker"
|
|
)
|
|
|
|
type Deps struct {
|
|
Logger *slog.Logger
|
|
Docker dockerconn.HostConnector
|
|
Origins []string
|
|
}
|
|
|
|
func NewRouter(deps Deps) http.Handler {
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.RequestID)
|
|
r.Use(middleware.RealIP)
|
|
r.Use(middleware.Recoverer)
|
|
r.Use(middleware.Timeout(15 * time.Second))
|
|
r.Use(slogRequestLogger(deps.Logger))
|
|
|
|
r.Use(cors.Handler(cors.Options{
|
|
AllowedOrigins: deps.Origins,
|
|
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete},
|
|
AllowedHeaders: []string{"Accept", "Content-Type", "Authorization"},
|
|
AllowCredentials: true,
|
|
MaxAge: 300,
|
|
}))
|
|
|
|
r.Get("/healthz", handleHealthz)
|
|
|
|
r.Route("/api", func(r chi.Router) {
|
|
r.Get("/containers", handleListContainers(deps.Docker))
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
func slogRequestLogger(logger *slog.Logger) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
next.ServeHTTP(ww, r)
|
|
logger.Info("request",
|
|
"method", r.Method,
|
|
"path", r.URL.Path,
|
|
"status", ww.Status(),
|
|
"duration_ms", time.Since(start).Milliseconds(),
|
|
)
|
|
})
|
|
}
|
|
}
|