Skip to content

Commit

Permalink
Add full scan flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ozencb committed Nov 15, 2024
1 parent e6f05a5 commit 25268af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
18 changes: 17 additions & 1 deletion config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
port string
dbFilePath string
jsonFilePath string
fullScan bool
readonly bool
once sync.Once
)
Expand All @@ -25,7 +26,8 @@ func init() {

port = getEnv("PORT", "8363")
dbFilePath = getEnv("DATABASE_FILE_PATH", "couchtube.db")
jsonFilePath = getEnv("JSON_FILE_PATH", "videos.json")
jsonFilePath = getEnv("JSON_FILE_PATH", "/videos.json")
fullScan = getEnvAsBool("FULL_SCAN", false)
readonly = getEnvAsBool("READONLY_MODE", false)
})
}
Expand All @@ -49,6 +51,16 @@ func getEnvAsBool(key string, fallback bool) bool {
return fallback
}

func getEnvAsPath(key string, fallback string) string {
path := getEnv(key, fallback)

if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatalf("Path %s does not exist", path)
}

return path
}

func GetPort() string {
return port
}
Expand All @@ -61,6 +73,10 @@ func GetJSONFilePath() string {
return jsonFilePath
}

func GetFullScan() bool {
return fullScan
}

func GetReadonlyMode() bool {
return readonly
}
19 changes: 15 additions & 4 deletions db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,21 @@ func populateDatabase(db *sql.DB) error {

// Check if any channels already exist to avoid re-population.
var exists int
err = db.QueryRow(`SELECT EXISTS(SELECT 1 FROM channels LIMIT 1);`).Scan(&exists)
if err != nil {
log.Fatal(err)
return err

if !config.GetFullScan() {
err = db.QueryRow(`SELECT EXISTS(SELECT 1 FROM channels LIMIT 1);`).Scan(&exists)
if err != nil {
log.Fatal(err)
return err
}
} else {
// If full scan is enabled, delete all data from the tables.
log.Println("Full scan enabled. Deleting all data from the database.")
_, err = db.Exec(`DELETE FROM channels; DELETE FROM videos; DELETE FROM channel_videos;`)
if err != nil {
log.Fatal(err)
return err
}
}

if exists == 1 {
Expand Down
11 changes: 6 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ services:
- "8363:8363"
environment:
- PORT=8363
# - DATABASE_FILE_PATH=/app/data/couchtube.db
# - JSON_FILE_PATH=/data/videos.json
- DATABASE_FILE_PATH=/app/data/couchtube.db
- JSON_FILE_PATH=/data/videos.json
- READONLY_MODE=false
# volumes:
# - ./videos.json:/app/data/videos.json:ro
# - couchtube_db:/app/data
- FULL_SCAN=true
volumes:
- couchtube_db:/app/data
- ./videos.json:/app/data/videos.json:ro
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8363"]
Expand Down

0 comments on commit 25268af

Please sign in to comment.