feat: add request logging middleware and improve server logging

This commit is contained in:
Tanishq Dubey 2025-05-17 12:18:32 -04:00 committed by Tanishq Dubey (aider)
parent 8f1944ba15
commit af6a584628
No known key found for this signature in database
GPG Key ID: CFC1931B84DFC3F9
2 changed files with 56 additions and 4 deletions

View File

@ -5,11 +5,53 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net/http"
"os"
"time"
)
// loggingResponseWriter is a wrapper for http.ResponseWriter to capture status code
type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
}
// WriteHeader captures the status code before passing to the underlying ResponseWriter
func (lrw *loggingResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
// LoggingMiddleware logs information about each request
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// Create a response writer wrapper to capture status code
lrw := &loggingResponseWriter{
ResponseWriter: w,
statusCode: http.StatusOK, // Default status
}
// Process the request
next.ServeHTTP(lrw, r)
// Calculate duration
duration := time.Since(start)
// Log the request details
log.Printf("REQUEST: %s %s - %d %s - %s - %v",
r.Method,
r.URL.Path,
lrw.statusCode,
http.StatusText(lrw.statusCode),
r.RemoteAddr,
duration,
)
})
}
// Server represents the API server for KAT
type Server struct {
httpServer *http.Server
@ -22,7 +64,7 @@ type Server struct {
// NewServer creates a new API server instance
func NewServer(addr string, certFile, keyFile, caFile string) (*Server, error) {
router := NewRouter()
server := &Server{
router: router,
certFile: certFile,
@ -33,7 +75,7 @@ func NewServer(addr string, certFile, keyFile, caFile string) (*Server, error) {
// Create the HTTP server with TLS config
server.httpServer = &http.Server{
Addr: addr,
Handler: router,
Handler: LoggingMiddleware(router), // Add logging middleware
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
@ -44,6 +86,8 @@ func NewServer(addr string, certFile, keyFile, caFile string) (*Server, error) {
// Start begins listening for requests
func (s *Server) Start() error {
log.Printf("Starting server on %s", s.httpServer.Addr)
// Load server certificate and key
cert, err := tls.LoadX509KeyPair(s.certFile, s.keyFile)
if err != nil {
@ -69,13 +113,21 @@ func (s *Server) Start() error {
MinVersion: tls.VersionTLS12,
}
log.Printf("Server configured with TLS, starting to listen for requests")
// Start the server
return s.httpServer.ListenAndServeTLS("", "")
}
// Stop gracefully shuts down the server
func (s *Server) Stop(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
log.Printf("Shutting down server on %s", s.httpServer.Addr)
err := s.httpServer.Shutdown(ctx)
if err != nil {
log.Printf("Error during server shutdown: %v", err)
return err
}
log.Printf("Server shutdown complete")
return nil
}
// RegisterJoinHandler registers the handler for agent join requests

View File

@ -22,7 +22,7 @@ const (
// Default certificate validity period
DefaultCertValidityDays = 365 // 1 year
// Default PKI directory
DefaultPKIDir = "~/.kat/pki"
DefaultPKIDir = ".kat/pki"
)
// GenerateCA creates a new Certificate Authority key pair and certificate.