From a1c189c885344b656b5b8a04712734dcde584d8a Mon Sep 17 00:00:00 2001 From: Dmitrii Andreev Date: Mon, 7 Apr 2025 12:47:36 +0300 Subject: [PATCH] renamed database to filestore --- .dockerignore | 1 + .gitignore | 5 ++-- cmd/icecast-ripper/main.go | 4 ++-- .../database.go => filestore/filestore.go} | 23 ++++++++++--------- internal/recorder/recorder.go | 6 ++--- internal/rss/rss.go | 6 ++--- 6 files changed, 24 insertions(+), 21 deletions(-) rename internal/{database/database.go => filestore/filestore.go} (82%) diff --git a/.dockerignore b/.dockerignore index c88dc07..3007497 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ .github bin .idea +/icecast-ripper diff --git a/.gitignore b/.gitignore index 88d9daa..0b7f87a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -records -bin +/records +/bin +/icecast-ripper # If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore diff --git a/cmd/icecast-ripper/main.go b/cmd/icecast-ripper/main.go index d06afef..8751990 100644 --- a/cmd/icecast-ripper/main.go +++ b/cmd/icecast-ripper/main.go @@ -12,7 +12,7 @@ import ( "time" "github.com/kemko/icecast-ripper/internal/config" - "github.com/kemko/icecast-ripper/internal/database" + "github.com/kemko/icecast-ripper/internal/filestore" "github.com/kemko/icecast-ripper/internal/logger" "github.com/kemko/icecast-ripper/internal/recorder" "github.com/kemko/icecast-ripper/internal/rss" @@ -51,7 +51,7 @@ func main() { storePath = changeExtension(cfg.DatabasePath, ".json") } - fileStore, err := database.InitDB(storePath) + fileStore, err := filestore.Init(storePath) if err != nil { slog.Error("Failed to initialize file store", "error", err) os.Exit(1) diff --git a/internal/database/database.go b/internal/filestore/filestore.go similarity index 82% rename from internal/database/database.go rename to internal/filestore/filestore.go index 329bea6..d35aa88 100644 --- a/internal/database/database.go +++ b/internal/filestore/filestore.go @@ -1,4 +1,5 @@ -package database +// Package filestore provides functionality for storing and retrieving metadata about recorded files +package filestore import ( "encoding/json" @@ -21,18 +22,18 @@ type RecordedFile struct { RecordedAt time.Time `json:"recordedAt"` } -// FileStore represents an in-memory store with optional file persistence -type FileStore struct { +// Store represents an in-memory store with optional file persistence +type Store struct { files map[string]*RecordedFile storePath string mu sync.RWMutex } -// InitDB initializes a new FileStore -func InitDB(dataSourceName string) (*FileStore, error) { +// Init initializes a new Store +func Init(dataSourceName string) (*Store, error) { slog.Info("Initializing file store", "path", dataSourceName) - fs := &FileStore{ + fs := &Store{ files: make(map[string]*RecordedFile), storePath: dataSourceName, } @@ -46,7 +47,7 @@ func InitDB(dataSourceName string) (*FileStore, error) { return fs, nil } -func (fs *FileStore) loadFromFile() error { +func (fs *Store) loadFromFile() error { fs.mu.Lock() defer fs.mu.Unlock() @@ -73,7 +74,7 @@ func (fs *FileStore) loadFromFile() error { return nil } -func (fs *FileStore) saveToFile() error { +func (fs *Store) saveToFile() error { fs.mu.RLock() defer fs.mu.RUnlock() @@ -105,7 +106,7 @@ func (fs *FileStore) saveToFile() error { } // AddRecordedFile adds a file to the store -func (fs *FileStore) AddRecordedFile(filename, hash string, fileSize int64, duration time.Duration, recordedAt time.Time) (int64, error) { +func (fs *Store) AddRecordedFile(filename, hash string, fileSize int64, duration time.Duration, recordedAt time.Time) (int64, error) { fs.mu.Lock() defer fs.mu.Unlock() @@ -128,7 +129,7 @@ func (fs *FileStore) AddRecordedFile(filename, hash string, fileSize int64, dura } // GetRecordedFiles retrieves all recorded files, ordered by recording date descending -func (fs *FileStore) GetRecordedFiles(limit int) ([]RecordedFile, error) { +func (fs *Store) GetRecordedFiles(limit int) ([]RecordedFile, error) { fs.mu.RLock() defer fs.mu.RUnlock() @@ -149,6 +150,6 @@ func (fs *FileStore) GetRecordedFiles(limit int) ([]RecordedFile, error) { } // Close ensures all data is persisted -func (fs *FileStore) Close() error { +func (fs *Store) Close() error { return fs.saveToFile() } diff --git a/internal/recorder/recorder.go b/internal/recorder/recorder.go index 0f68a07..0f7e220 100644 --- a/internal/recorder/recorder.go +++ b/internal/recorder/recorder.go @@ -13,14 +13,14 @@ import ( "sync" "time" - "github.com/kemko/icecast-ripper/internal/database" + "github.com/kemko/icecast-ripper/internal/filestore" "github.com/kemko/icecast-ripper/internal/hash" ) type Recorder struct { tempPath string recordingsPath string - db *database.FileStore + db *filestore.Store client *http.Client mu sync.Mutex isRecording bool @@ -28,7 +28,7 @@ type Recorder struct { streamName string } -func New(tempPath, recordingsPath string, db *database.FileStore, streamName string) (*Recorder, error) { +func New(tempPath, recordingsPath string, db *filestore.Store, streamName string) (*Recorder, error) { for _, dir := range []string{tempPath, recordingsPath} { if err := os.MkdirAll(dir, 0755); err != nil { return nil, fmt.Errorf("failed to create directory %s: %w", dir, err) diff --git a/internal/rss/rss.go b/internal/rss/rss.go index 137945d..f6dc365 100644 --- a/internal/rss/rss.go +++ b/internal/rss/rss.go @@ -9,12 +9,12 @@ import ( "github.com/gorilla/feeds" "github.com/kemko/icecast-ripper/internal/config" - "github.com/kemko/icecast-ripper/internal/database" + "github.com/kemko/icecast-ripper/internal/filestore" ) // Generator creates RSS feeds type Generator struct { - fileStore *database.FileStore + fileStore *filestore.Store feedBaseURL string recordingsPath string feedTitle string @@ -22,7 +22,7 @@ type Generator struct { } // New creates a new RSS Generator instance -func New(fileStore *database.FileStore, cfg *config.Config, title, description string) *Generator { +func New(fileStore *filestore.Store, cfg *config.Config, title, description string) *Generator { baseURL := cfg.RSSFeedURL if baseURL == "" { slog.Warn("RSS_FEED_URL not set, using default")