v0.3.0 storage with sqlite3 and pid management added
This commit is contained in:
@ -2,6 +2,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"chess/cmd/chessd/cli"
|
||||
"chess/internal/http"
|
||||
"chess/internal/processor"
|
||||
"chess/internal/service"
|
||||
"chess/internal/storage"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -9,29 +14,69 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"chess/internal/http"
|
||||
"chess/internal/processor"
|
||||
"chess/internal/service"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Check for CLI database commands
|
||||
if len(os.Args) > 1 && os.Args[1] == "db" {
|
||||
if err := cli.Run(os.Args[2:]); err != nil {
|
||||
log.Fatalf("CLI error: %v", err)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Command-line flags
|
||||
var (
|
||||
host = flag.String("host", "localhost", "Server host")
|
||||
port = flag.Int("port", 8080, "Server port")
|
||||
dev = flag.Bool("dev", false, "Development mode (relaxed rate limits)")
|
||||
host = flag.String("host", "localhost", "Server host")
|
||||
port = flag.Int("port", 8080, "Server port")
|
||||
dev = flag.Bool("dev", false, "Development mode (relaxed rate limits)")
|
||||
storagePath = flag.String("storage-path", "", "Path to SQLite database file (disables persistence if empty)")
|
||||
pidPath = flag.String("pid", "", "Optional path to write PID file")
|
||||
pidLock = flag.Bool("pid-lock", false, "Lock PID file to allow only one instance (requires -pid)")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
// 1. Initialize the Service (Pure State Manager)
|
||||
svc, err := service.New()
|
||||
// Validate PID flags
|
||||
if *pidLock && *pidPath == "" {
|
||||
log.Fatal("Error: -pid-lock flag requires the -pid flag to be set")
|
||||
}
|
||||
|
||||
// Manage PID file if requested
|
||||
if *pidPath != "" {
|
||||
cleanup, err := managePIDFile(*pidPath, *pidLock)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to manage PID file: %v", err)
|
||||
}
|
||||
defer cleanup()
|
||||
log.Printf("PID file created at: %s (lock: %v)", *pidPath, *pidLock)
|
||||
}
|
||||
|
||||
// 1. Initialize Storage (optional)
|
||||
var store *storage.Store
|
||||
if *storagePath != "" {
|
||||
log.Printf("Initializing persistent storage at: %s", *storagePath)
|
||||
var err error
|
||||
store, err = storage.NewStore(*storagePath, *dev) // CHANGED: Added *dev parameter
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize storage: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := store.Close(); err != nil {
|
||||
log.Printf("Warning: failed to close storage cleanly: %v", err)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
log.Printf("Persistent storage disabled (use -storage-path to enable)")
|
||||
}
|
||||
|
||||
// 2. Initialize the Service with optional storage
|
||||
svc, err := service.New(store)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize service: %v", err)
|
||||
}
|
||||
defer svc.Close()
|
||||
|
||||
// 2. Initialize the Processor (Orchestrator), injecting the service
|
||||
// 3. Initialize the Processor (Orchestrator), injecting the service
|
||||
proc, err := processor.New(svc)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize processor: %v", err)
|
||||
@ -42,8 +87,8 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// 3. Initialize the Fiber App/HTTP Handler, injecting the processor
|
||||
app := http.NewFiberApp(proc, *dev)
|
||||
// 4. Initialize the Fiber App/HTTP Handler, injecting processor and service
|
||||
app := http.NewFiberApp(proc, svc, *dev)
|
||||
|
||||
// Server configuration
|
||||
addr := fmt.Sprintf("%s:%d", *host, *port)
|
||||
@ -54,9 +99,14 @@ func main() {
|
||||
log.Printf("Listening on: http://%s", addr)
|
||||
log.Printf("API Version: v1")
|
||||
if *dev {
|
||||
log.Printf("Rate Limit: 10 requests/second per IP (DEV MODE)")
|
||||
log.Printf("Rate Limit: 20 requests/second per IP (DEV MODE)")
|
||||
} else {
|
||||
log.Printf("Rate Limit: 1 request/second per IP")
|
||||
log.Printf("Rate Limit: 10 requests/second per IP")
|
||||
}
|
||||
if *storagePath != "" {
|
||||
log.Printf("Storage: Enabled (%s)", *storagePath)
|
||||
} else {
|
||||
log.Printf("Storage: Disabled")
|
||||
}
|
||||
log.Printf("Endpoints: http://%s/api/v1/games", addr)
|
||||
log.Printf("Health: http://%s/health", addr)
|
||||
|
||||
Reference in New Issue
Block a user