refactor(ESISSO): enhance error handling during initialization and update startup logic in App
This commit is contained in:
7
app.go
7
app.go
@@ -36,7 +36,7 @@ func (a *App) startup(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add location read scope so we can fetch character locations
|
// 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_location.v1",
|
||||||
"esi-location.read_ship_type.v1",
|
"esi-location.read_ship_type.v1",
|
||||||
"esi-mail.organize_mail.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_titles.v1",
|
||||||
"esi-characters.read_fw_stats.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
|
// Greet returns a greeting for the given name
|
||||||
|
|||||||
32
esi_sso.go
32
esi_sso.go
@@ -74,6 +74,10 @@ type ESIToken struct {
|
|||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ESIToken) TableName() string {
|
||||||
|
return "esi_tokens"
|
||||||
|
}
|
||||||
|
|
||||||
type CharacterInfo struct {
|
type CharacterInfo struct {
|
||||||
CharacterID int64 `json:"character_id"`
|
CharacterID int64 `json:"character_id"`
|
||||||
CharacterName string `json:"character_name"`
|
CharacterName string `json:"character_name"`
|
||||||
@@ -108,28 +112,42 @@ type SystemKills struct {
|
|||||||
NpcKills int64 `json:"npc_kills"`
|
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{
|
s := &ESISSO{
|
||||||
clientID: clientID,
|
clientID: clientID,
|
||||||
redirectURI: redirectURI,
|
redirectURI: redirectURI,
|
||||||
scopes: scopes,
|
scopes: scopes,
|
||||||
}
|
}
|
||||||
_ = s.initDB()
|
if err := s.initDB(); err != nil {
|
||||||
return s
|
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ESISSO) initDB() error {
|
func (s *ESISSO) initDB() error {
|
||||||
home, err := os.UserHomeDir()
|
home, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to get user home directory: %w", err)
|
||||||
}
|
}
|
||||||
dbPath := filepath.Join(home, ".industrializer", "sqlite-latest.sqlite")
|
dbPath := filepath.Join(home, ".industrializer", "sqlite-latest.sqlite")
|
||||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||||
if err != nil {
|
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
|
s.db = db
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user