ps2_artwork_api/api.js

81 lines
2.1 KiB
JavaScript

const express = require('express');
const fs = require('fs');
const path = require('path');
const https = require('https');
const app = express();
const os = require('os');
const PORT = 7722;
const data = fs.readFileSync('db/PS2_LIST.txt', 'utf-8');
const gameMap = {};
data.split('\n').forEach(line => {
const match = line.match(/^(\S+)\s+(.*)$/);
if (match) {
const [_, id, title] = match;
gameMap[id] = title.trim();
}
});
function normalizeId(input) {
const cleaned = input.toUpperCase().replace(/[_\.]/g, ''); // remove _ and .
const match = cleaned.match(/^([A-Z]+)(\d+)$/);
if (!match) return input.toUpperCase(); // fallback
const [, prefix, digits] = match;
return `${prefix}-${digits}`;
}
app.get('/search/:id', (req, res) => {
const normalizedId = normalizeId(req.params.id);
const title = gameMap[normalizedId];
if (title) {
res.json({ id: normalizedId, title });
} else {
res.status(404).json({ error: 'Game ID not found' });
}
});
app.use('/art', express.static(path.join(__dirname, 'db/PS2')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.join(__dirname, 'contact.html'));
});
const certDir = path.join(os.homedir(), 'certs');
const keyPath = path.join(certDir, 'privkey.pem');
const certPath = path.join(certDir, 'cert.pem');
const caPath = path.join(certDir, 'fullchain.pem');
const areCertsPresent = () => {
return fs.existsSync(keyPath) && fs.existsSync(certPath) && fs.existsSync(caPath);
};
if (areCertsPresent()) {
console.log('SSL certificates found. Starting HTTPS server...');
const sslOptions = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
ca: fs.readFileSync(caPath)
};
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`HTTPS Server running at https://localhost:${PORT}`);
});
} else {
console.log('SSL certificates not found. Starting HTTP server...');
app.listen(PORT, () => {
console.log(`HTTP Server running at http://localhost:${PORT}`);
});
}