@@ -2,12 +2,14 @@ package main
22
33import (
44 "context"
5+ "errors"
56 "flag"
67 "fmt"
78 "log/slog"
89 "net/http"
910 "os"
1011 "os/signal"
12+ "strings"
1113 "syscall"
1214 "time"
1315
3840 BuildDate = "unknown"
3941)
4042
43+ func toBackendConfig (cfg * config.Config ) storage.BackendConfig {
44+ pg := cfg .GatewayController .Storage .Postgres
45+ return storage.BackendConfig {
46+ Type : cfg .GatewayController .Storage .Type ,
47+ SQLitePath : cfg .GatewayController .Storage .SQLite .Path ,
48+ Postgres : storage.PostgresConnectionConfig {
49+ DSN : pg .DSN ,
50+ Host : pg .Host ,
51+ Port : pg .Port ,
52+ Database : pg .Database ,
53+ User : pg .User ,
54+ Password : pg .Password ,
55+ SSLMode : pg .SSLMode ,
56+ ConnectTimeout : pg .ConnectTimeout ,
57+ MaxOpenConns : pg .MaxOpenConns ,
58+ MaxIdleConns : pg .MaxIdleConns ,
59+ ConnMaxLifetime : pg .ConnMaxLifetime ,
60+ ConnMaxIdleTime : pg .ConnMaxIdleTime ,
61+ ApplicationName : pg .ApplicationName ,
62+ },
63+ }
64+ }
65+
4166func main () {
4267 // Parse command-line flags
4368 configPath := flag .String ("config" , "" , "Path to configuration file (required)" )
@@ -86,29 +111,20 @@ func main() {
86111 // Initialize storage based on type
87112 var db storage.Storage
88113 if cfg .IsPersistentMode () {
89- switch cfg .GatewayController .Storage .Type {
90- case "sqlite" :
91- log .Info ("Initializing SQLite storage" , slog .String ("path" , cfg .GatewayController .Storage .SQLite .Path ))
92- db , err = storage .NewSQLiteStorage (cfg .GatewayController .Storage .SQLite .Path , log )
93- if err != nil {
94- // Check for database locked error and provide clear guidance
95- if err .Error () == "database is locked" || err .Error () == "failed to open database: database is locked" {
96- log .Error ("Database is locked by another process" ,
97- slog .String ("database_path" , cfg .GatewayController .Storage .SQLite .Path ),
98- slog .String ("troubleshooting" , "Check if another gateway-controller instance is running or remove stale WAL files" ))
99- os .Exit (1 )
100- }
101- log .Error ("Failed to initialize SQLite database" , slog .Any ("error" , err ))
114+ db , err = storage .NewStorage (toBackendConfig (cfg ), log )
115+ if err != nil {
116+ if strings .EqualFold (cfg .GatewayController .Storage .Type , "sqlite" ) && errors .Is (err , storage .ErrDatabaseLocked ) {
117+ log .Error ("Database is locked by another process" ,
118+ slog .String ("database_path" , cfg .GatewayController .Storage .SQLite .Path ),
119+ slog .String ("troubleshooting" , "Check if another gateway-controller instance is running or remove stale WAL files" ))
102120 os .Exit (1 )
103121 }
104- defer db .Close ()
105- case "postgres" :
106- log .Error ("PostgreSQL storage not yet implemented" )
107- os .Exit (1 )
108- default :
109- log .Error ("Unknown storage type" , slog .String ("type" , cfg .GatewayController .Storage .Type ))
122+ log .Error ("Failed to initialize database storage" ,
123+ slog .String ("type" , cfg .GatewayController .Storage .Type ),
124+ slog .Any ("error" , err ))
110125 os .Exit (1 )
111126 }
127+ defer db .Close ()
112128 } else {
113129 log .Info ("Running in memory-only mode (no persistent storage)" )
114130 }
0 commit comments