Actually make fucking interfaces
This commit is contained in:
@@ -8,30 +8,26 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go-eve-pi/types"
|
||||||
|
|
||||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CacheEntry represents a cached API response
|
// CacheEntry is now defined in the types package
|
||||||
type CacheEntry struct {
|
|
||||||
ID uint `gorm:"primaryKey"`
|
|
||||||
URLHash string `gorm:"uniqueIndex"`
|
|
||||||
Response string `gorm:"type:text"`
|
|
||||||
CachedAt time.Time `gorm:"index"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// CachedESI implements ESIInterface with caching
|
// CachedESI implements ESIInterface with caching
|
||||||
type CachedESI struct {
|
type CachedESI struct {
|
||||||
direct ESIInterface
|
direct ESIInterface
|
||||||
db interface {
|
db interface {
|
||||||
GetCacheEntry(urlHash string) (*CacheEntry, error)
|
GetCacheEntry(urlHash string) (*types.CacheEntry, error)
|
||||||
SaveCacheEntry(entry *CacheEntry) error
|
SaveCacheEntry(entry *types.CacheEntry) error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCachedESI creates a new CachedESI instance
|
// NewCachedESI creates a new CachedESI instance
|
||||||
func NewCachedESI(direct ESIInterface, db interface {
|
func NewCachedESI(direct ESIInterface, db interface {
|
||||||
GetCacheEntry(urlHash string) (*CacheEntry, error)
|
GetCacheEntry(urlHash string) (*types.CacheEntry, error)
|
||||||
SaveCacheEntry(entry *CacheEntry) error
|
SaveCacheEntry(entry *types.CacheEntry) error
|
||||||
}) *CachedESI {
|
}) *CachedESI {
|
||||||
return &CachedESI{
|
return &CachedESI{
|
||||||
direct: direct,
|
direct: direct,
|
||||||
@@ -113,13 +109,13 @@ func (c *CachedESI) getCachedResponse(url string, fetchFunc func() (interface{},
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheEntry = CacheEntry{
|
cacheEntry = &types.CacheEntry{
|
||||||
URLHash: urlHash,
|
URLHash: urlHash,
|
||||||
Response: string(responseBytes),
|
Response: string(responseBytes),
|
||||||
CachedAt: time.Now(),
|
CachedAt: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.db.SaveCacheEntry(&cacheEntry); err != nil {
|
if err := c.db.SaveCacheEntry(cacheEntry); err != nil {
|
||||||
logger.Warning("Failed to cache response: %v", err)
|
logger.Warning("Failed to cache response: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -49,7 +49,7 @@ func main() {
|
|||||||
options.GlobalOptions.ClientID,
|
options.GlobalOptions.ClientID,
|
||||||
options.GlobalOptions.RedirectURI,
|
options.GlobalOptions.RedirectURI,
|
||||||
options.GlobalOptions.Scopes,
|
options.GlobalOptions.Scopes,
|
||||||
database.Character,
|
database.Character(),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to create SSO instance %v", err)
|
logger.Error("Failed to create SSO instance %v", err)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Package options handles configuration management for the EVE PI application.
|
||||||
package options
|
package options
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -129,7 +130,7 @@ func GenerateEnvExample() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateExampleValue(fieldName, envKey, defaultValue string) string {
|
func generateExampleValue(_, envKey, defaultValue string) string {
|
||||||
// If there's a default value, use it
|
// If there's a default value, use it
|
||||||
if defaultValue != "" {
|
if defaultValue != "" {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// Package repositories provides database repository implementations
|
||||||
|
// with a clean separation of concerns and proper interface abstractions.
|
||||||
package repositories
|
package repositories
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CacheRepository handles cache-related database operations
|
// CacheRepository handles cache-related database operations
|
||||||
|
// Implements CacheRepositoryInterface
|
||||||
type CacheRepository struct {
|
type CacheRepository struct {
|
||||||
*BaseRepository
|
*BaseRepository
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CharacterRepository handles character-related database operations
|
// CharacterRepository handles character-related database operations
|
||||||
|
// Implements CharacterRepositoryInterface
|
||||||
type CharacterRepository struct {
|
type CharacterRepository struct {
|
||||||
*BaseRepository
|
*BaseRepository
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Database manages all repositories and provides a unified interface
|
// Database manages all repositories and provides a unified interface
|
||||||
|
// Implements DatabaseInterface
|
||||||
type Database struct {
|
type Database struct {
|
||||||
*gorm.DB
|
*gorm.DB
|
||||||
Character *CharacterRepository
|
character *CharacterRepository
|
||||||
Cache *CacheRepository
|
cache *CacheRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDatabase creates a new database instance with all repositories
|
// NewDatabase creates a new database instance with all repositories
|
||||||
@@ -49,8 +50,8 @@ func NewDatabase() (*Database, error) {
|
|||||||
|
|
||||||
database := &Database{
|
database := &Database{
|
||||||
DB: db,
|
DB: db,
|
||||||
Character: characterRepo,
|
character: characterRepo,
|
||||||
Cache: cacheRepo,
|
cache: cacheRepo,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run migrations
|
// Run migrations
|
||||||
@@ -62,3 +63,13 @@ func NewDatabase() (*Database, error) {
|
|||||||
logger.Info("Database initialized successfully")
|
logger.Info("Database initialized successfully")
|
||||||
return database, nil
|
return database, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Character returns the character repository
|
||||||
|
func (d *Database) Character() CharacterRepositoryInterface {
|
||||||
|
return d.character
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache returns the cache repository
|
||||||
|
func (d *Database) Cache() CacheRepositoryInterface {
|
||||||
|
return d.cache
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,3 +7,16 @@ type CharacterRepositoryInterface interface {
|
|||||||
GetCharacterByName(characterName string) (*types.Character, error)
|
GetCharacterByName(characterName string) (*types.Character, error)
|
||||||
SaveCharacter(character *types.Character) error
|
SaveCharacter(character *types.Character) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CacheRepositoryInterface defines the interface for cache operations
|
||||||
|
type CacheRepositoryInterface interface {
|
||||||
|
GetCacheEntry(urlHash string) (*types.CacheEntry, error)
|
||||||
|
SaveCacheEntry(entry *types.CacheEntry) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// DatabaseInterface defines the interface for database management
|
||||||
|
type DatabaseInterface interface {
|
||||||
|
Character() CharacterRepositoryInterface
|
||||||
|
Cache() CacheRepositoryInterface
|
||||||
|
AutoMigrate(dst ...interface{}) error
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user