diff --git a/app.go b/app.go index 42781c9..f11225f 100644 --- a/app.go +++ b/app.go @@ -36,7 +36,7 @@ func (a *App) startup(ctx context.Context) { } // Add location read scope so we can fetch character locations - a.ssi = NewESISSO(clientID, redirectURI, []string{ + ssi, err := NewESISSO(clientID, redirectURI, []string{ "esi-location.read_location.v1", "esi-location.read_ship_type.v1", "esi-mail.organize_mail.v1", @@ -76,6 +76,11 @@ func (a *App) startup(ctx context.Context) { "esi-characters.read_titles.v1", "esi-characters.read_fw_stats.v1", }) + if err != nil { + fmt.Printf("ERROR: Failed to initialize ESI SSO: %v\n", err) + return + } + a.ssi = ssi } // Greet returns a greeting for the given name diff --git a/esi_sso.go b/esi_sso.go index c7ff275..3620064 100644 --- a/esi_sso.go +++ b/esi_sso.go @@ -74,6 +74,10 @@ type ESIToken struct { CreatedAt time.Time } +func (ESIToken) TableName() string { + return "esi_tokens" +} + type CharacterInfo struct { CharacterID int64 `json:"character_id"` CharacterName string `json:"character_name"` @@ -108,28 +112,42 @@ type SystemKills struct { NpcKills int64 `json:"npc_kills"` } -func NewESISSO(clientID string, redirectURI string, scopes []string) *ESISSO { +func NewESISSO(clientID string, redirectURI string, scopes []string) (*ESISSO, error) { s := &ESISSO{ clientID: clientID, redirectURI: redirectURI, scopes: scopes, } - _ = s.initDB() - return s + if err := s.initDB(); err != nil { + return nil, fmt.Errorf("failed to initialize database: %w", err) + } + return s, nil } func (s *ESISSO) initDB() error { home, err := os.UserHomeDir() if err != nil { - return err + return fmt.Errorf("failed to get user home directory: %w", err) } dbPath := filepath.Join(home, ".industrializer", "sqlite-latest.sqlite") db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) if err != nil { - return err + return fmt.Errorf("failed to open database at %s: %w", dbPath, err) } - if err := db.AutoMigrate(&ESIToken{}); err != nil { - return err + + sqlDB, err := db.DB() + if err != nil { + return fmt.Errorf("failed to get underlying sql.DB: %w", err) + } + + if err := sqlDB.Ping(); err != nil { + return fmt.Errorf("failed to ping database: %w", err) + } + + if !db.Migrator().HasTable(&ESIToken{}) { + if err := db.Migrator().CreateTable(&ESIToken{}); err != nil { + return fmt.Errorf("failed to create table esi_tokens: %w", err) + } } s.db = db return nil