Add configurable library path with settings API and UI
This commit is contained in:
64
src/routes/settings.js
Normal file
64
src/routes/settings.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import express from 'express';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const CONFIG_FILE = path.join(process.cwd(), 'config.json');
|
||||
|
||||
// Default config
|
||||
const DEFAULT_CONFIG = {
|
||||
libraryPath: './audio_library',
|
||||
port: 3000
|
||||
};
|
||||
|
||||
// Load config
|
||||
async function loadConfig() {
|
||||
try {
|
||||
const data = await fs.readFile(CONFIG_FILE, 'utf-8');
|
||||
return { ...DEFAULT_CONFIG, ...JSON.parse(data) };
|
||||
} catch (error) {
|
||||
return DEFAULT_CONFIG;
|
||||
}
|
||||
}
|
||||
|
||||
// Save config
|
||||
async function saveConfig(config) {
|
||||
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
||||
}
|
||||
|
||||
// Get settings
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const config = await loadConfig();
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Update settings
|
||||
router.patch('/', async (req, res) => {
|
||||
try {
|
||||
const config = await loadConfig();
|
||||
const { libraryPath } = req.body;
|
||||
|
||||
if (libraryPath) {
|
||||
// Validate path exists
|
||||
try {
|
||||
await fs.access(libraryPath);
|
||||
config.libraryPath = libraryPath;
|
||||
} catch (error) {
|
||||
return res.status(400).json({ error: 'Library path does not exist' });
|
||||
}
|
||||
}
|
||||
|
||||
await saveConfig(config);
|
||||
res.json({ success: true, config });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
export { loadConfig };
|
||||
@@ -7,13 +7,18 @@ 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();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// Load config
|
||||
const config = await loadConfig();
|
||||
const PORT = process.env.PORT || config.port || 3000;
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
@@ -27,6 +32,7 @@ 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);
|
||||
@@ -34,8 +40,11 @@ app.use('/rest', subsonicRouter);
|
||||
// Serve static files
|
||||
app.use(express.static('public'));
|
||||
|
||||
// Serve audio files
|
||||
app.use('/audio', express.static(path.join(process.cwd(), 'audio_library')));
|
||||
// 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));
|
||||
app.use('/artwork', express.static(path.join(process.cwd(), 'artwork')));
|
||||
|
||||
// Health check
|
||||
@@ -45,5 +54,6 @@ app.get('/health', (req, res) => {
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🎵 CarPlay Audio Server running on port ${PORT}`);
|
||||
console.log(`📁 Audio library: ${path.join(process.cwd(), 'audio_library')}`);
|
||||
console.log(`📁 Audio library: ${libraryPath}`);
|
||||
console.log(`🌐 Admin panel: http://localhost:${PORT}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user