refactor(ESISSO): enhance error handling during initialization and update startup logic in App

This commit is contained in:
2025-11-20 22:55:07 +01:00
parent 2c0134ed4d
commit d27d54804a
2 changed files with 31 additions and 8 deletions

View File

@@ -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