Files
dyn/cmd/server/main.go
Tanishq Dubey 01694f66a8 Add comprehensive logging and debug endpoints for production troubleshooting
- Add request logging middleware to main.go
- Add debug handler with health, config, stats, and test-dns endpoints
- Add detailed logging to DynDNS handler
- Add logging to Technitium DNS client
- Add database Ping() and GetStats() methods
- New endpoints:
  - /health - detailed health status with database and DNS checks
  - /debug/config - sanitized configuration
  - /debug/stats - database statistics
  - /debug/test-dns - live DNS test endpoint

This will help diagnose production issues with DNS updates.
2026-02-02 22:14:24 -05:00

130 lines
2.9 KiB
Go

package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"git.dws.rip/DWS/dyn/internal/config"
"git.dws.rip/DWS/dyn/internal/database"
"git.dws.rip/DWS/dyn/internal/dns"
"git.dws.rip/DWS/dyn/internal/handlers"
"git.dws.rip/DWS/dyn/internal/middleware"
"github.com/gin-gonic/gin"
)
func main() {
cfg := config.Load()
if errs := cfg.Validate(); len(errs) > 0 {
for _, err := range errs {
log.Printf("Configuration error: %s", err)
}
os.Exit(1)
}
db, err := database.New(cfg.DatabasePath)
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
defer db.Close()
dnsClient := dns.NewClient(
cfg.TechnitiumURL,
cfg.TechnitiumToken,
cfg.TechnitiumUsername,
cfg.TechnitiumPassword,
)
rateLimiter := middleware.NewRateLimiter(cfg.RateLimitPerIP, cfg.RateLimitPerToken)
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
// Add request logging middleware
router.Use(func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
c.Next()
latency := time.Since(start)
clientIP := c.ClientIP()
method := c.Request.Method
statusCode := c.Writer.Status()
if raw != "" {
path = path + "?" + raw
}
log.Printf("[%s] %s %s %d %v", clientIP, method, path, statusCode, latency)
})
if len(cfg.TrustedProxies) > 0 {
router.SetTrustedProxies(cfg.TrustedProxies)
}
router.Static("/static", "./web/static")
router.LoadHTMLGlob("web/templates/*")
webHandler := handlers.NewWebHandler(db, cfg)
dynHandler := handlers.NewDynDNSHandler(db, dnsClient, cfg)
debugHandler := handlers.NewDebugHandler(db, dnsClient, cfg)
router.GET("/", webHandler.Index)
// Debug/health endpoints (no rate limiting)
router.GET("/health", debugHandler.Health)
router.GET("/debug/config", debugHandler.ConfigDebug)
router.GET("/debug/stats", debugHandler.Stats)
router.GET("/debug/test-dns", debugHandler.TestDNS)
api := router.Group("/api")
api.Use(rateLimiter.RateLimitByIP())
{
api.GET("/check", webHandler.CheckSubdomain)
api.POST("/claim", webHandler.ClaimSpace)
nic := api.Group("/nic")
nic.Use(rateLimiter.RateLimitByToken())
nic.GET("/update", dynHandler.Update)
}
port := cfg.ServerPort
if port == "" {
port = "8080"
}
srv := &http.Server{
Addr: ":" + port,
Handler: router,
}
go func() {
log.Printf("Server starting on port %s", port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Failed to start server: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("Server forced to shutdown: %v", err)
}
log.Println("Server exited")
}