71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import tracksRouter from './routes/tracks.js';
|
|
import playlistsRouter from './routes/playlists.js';
|
|
import albumsRouter from './routes/albums.js';
|
|
import uploadRouter from './routes/upload.js';
|
|
import subsonicRouter from './routes/subsonic.js';
|
|
import settingsRouter from './routes/settings.js';
|
|
import { initDatabase } from './database/db.js';
|
|
import { loadConfig } from './routes/settings.js';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const app = express();
|
|
|
|
// Load config
|
|
const config = await loadConfig();
|
|
const PORT = process.env.PORT || config.port || 3000;
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
// Log all requests for debugging
|
|
app.use((req, res, next) => {
|
|
console.log(`📥 ${req.method} ${req.url}`);
|
|
next();
|
|
});
|
|
|
|
// Initialize database
|
|
await initDatabase();
|
|
|
|
// Routes
|
|
app.use('/api/tracks', tracksRouter);
|
|
app.use('/api/playlists', playlistsRouter);
|
|
app.use('/api/albums', albumsRouter);
|
|
app.use('/api/upload', uploadRouter);
|
|
app.use('/api/settings', settingsRouter);
|
|
|
|
// Subsonic API
|
|
app.use('/rest', subsonicRouter);
|
|
|
|
// Serve static files
|
|
app.use(express.static('public'));
|
|
|
|
// Serve audio files (use configured path)
|
|
const libraryPath = path.isAbsolute(config.libraryPath)
|
|
? config.libraryPath
|
|
: path.join(process.cwd(), config.libraryPath);
|
|
app.use('/audio', express.static(libraryPath));
|
|
|
|
// Artwork can be in data directory (Docker) or root (local)
|
|
const artworkPath = process.env.NODE_ENV === 'production'
|
|
? path.join(process.cwd(), 'data', 'artwork')
|
|
: path.join(process.cwd(), 'artwork');
|
|
app.use('/artwork', express.static(artworkPath));
|
|
|
|
// Health check
|
|
app.get('/health', (req, res) => {
|
|
res.json({ status: 'ok', message: 'CarPlay Audio Server is running' });
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`🎵 CarPlay Audio Server running on port ${PORT}`);
|
|
console.log(`📁 Audio library: ${libraryPath}`);
|
|
console.log(`🌐 Admin panel: http://localhost:${PORT}`);
|
|
});
|