v0.2.0 transitioned to api-only, extended and improved features, docs and tests added
This commit is contained in:
@ -1,26 +0,0 @@
|
||||
// FILE: cmd/chess/main.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"chess/internal/cli"
|
||||
"chess/internal/service"
|
||||
clitransport "chess/internal/transport/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
svc, err := service.New()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to start: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer svc.Close()
|
||||
|
||||
view := cli.New(os.Stdin, os.Stdout)
|
||||
handler := clitransport.New(svc, view)
|
||||
|
||||
view.ShowWelcome()
|
||||
handler.Run() // All game loop logic is in the handler
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user