clean up main.go

This commit is contained in:
2026-02-09 04:15:11 -08:00
parent b9a7a972e2
commit 38adb391be
2 changed files with 19 additions and 51 deletions

59
main.go
View File

@@ -2,10 +2,8 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"muzi/db"
"muzi/web"
@@ -13,64 +11,25 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
)
func dirCheck(path string) error {
_, err := os.Stat(path)
func check(msg string, err error) {
if err != nil {
if errors.Is(err, os.ErrNotExist) {
os.MkdirAll(path, os.ModePerm)
} else {
fmt.Fprintf(os.Stderr, "Error checking dir: %s: %v\n", path, err)
return err
}
fmt.Fprintf(os.Stderr, "Error %s: %v\n", msg, err)
os.Exit(1)
}
return nil
}
func main() {
zipDir := filepath.Join(".", "imports", "spotify", "zip")
extDir := filepath.Join(".", "imports", "spotify", "extracted")
dirs := []string{zipDir, extDir}
for _, dir := range dirs {
err := dirCheck(dir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error checking dir: %s: %v\n", dir, err)
return
}
}
err := db.CreateDB()
if err != nil {
fmt.Fprintf(os.Stderr, "Error ensuring muzi DB exists: %v\n", err)
return
}
check("ensuring muzi DB exists", db.CreateDB())
var err error
db.Pool, err = pgxpool.New(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
fmt.Sprintf(db.GetDbUrl(), "/muzi"),
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
return
}
check("connecting to muzi database", err)
defer db.Pool.Close()
err = db.CreateAllTables()
if err != nil {
fmt.Fprintf(os.Stderr, "Error ensuring all tables exist: %v\n", err)
return
}
err = db.CleanupExpiredSessions()
if err != nil {
fmt.Fprintf(os.Stderr, "Error cleaning expired sessions: %v\n", err)
return
}
/*
err = migrate.ImportSpotify(1)
if err != nil {
return
}
*/
check("ensuring all tables exist", db.CreateAllTables())
check("cleaning expired sessions", db.CleanupExpiredSessions())
web.Start()
}