v0.2.0 transitioned to api-only, extended and improved features, docs and tests added

This commit is contained in:
2025-10-29 23:28:19 -04:00
parent b98ea83012
commit 0ad608293e
32 changed files with 3683 additions and 1670 deletions

View File

@ -2,7 +2,6 @@
package main
import (
"context"
"flag"
"fmt"
"log"
@ -11,8 +10,9 @@ import (
"syscall"
"time"
"chess/internal/http"
"chess/internal/processor"
"chess/internal/service"
"chess/internal/transport/http"
)
func main() {
@ -24,48 +24,57 @@ func main() {
)
flag.Parse()
// Initialize service (includes engine)
// 1. Initialize the Service (Pure State Manager)
svc, err := service.New()
if err != nil {
log.Fatalf("Failed to initialize service: %v", err)
}
defer svc.Close()
// 2. Initialize the Processor (Orchestrator), injecting the service
proc, err := processor.New(svc)
if err != nil {
log.Fatalf("Failed to initialize processor: %v", err)
}
defer func() {
if err := svc.Close(); err != nil {
log.Printf("Warning: failed to close service cleanly: %v", err)
if err := proc.Close(); err != nil {
log.Printf("Warning: failed to close processor cleanly: %v", err)
}
}()
// Create Fiber app with dev mode flag
app := http.NewFiberApp(svc, *dev)
// 3. Initialize the Fiber App/HTTP Handler, injecting the processor
app := http.NewFiberApp(proc, *dev)
// Server configuration
addr := fmt.Sprintf("%s:%d", *host, *port)
// Start server in goroutine
// Start server in a goroutine
go func() {
log.Printf("Chess API Server starting...")
log.Printf("Listening on: http://%s", addr)
log.Printf("API Version: v1")
log.Printf("Rate Limit: 1 request/second per IP")
if *dev {
log.Printf("Rate Limit: 10 requests/second per IP (DEV MODE)")
} else {
log.Printf("Rate Limit: 1 request/second per IP")
}
log.Printf("Endpoints: http://%s/api/v1/games", addr)
log.Printf("Health: http://%s/health", addr)
if err := app.Listen(addr); err != nil {
log.Printf("Server error: %v", err)
// This log often prints on graceful shutdown, which is normal.
log.Printf("Server listen error: %v", err)
}
}()
// Wait for interrupt signal
// Wait for an interrupt signal to gracefully shut down
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
// Graceful shutdown with timeout
_, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Graceful shutdown with a timeout
if err := app.ShutdownWithTimeout(5 * time.Second); err != nil {
log.Printf("Server forced to shutdown: %v", err)
}