ampcast/server.js

49 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-07-20 09:29:42 +08:00
const {resolve} = require('path');
const express = require('express');
2024-04-12 08:14:21 +08:00
const dotenv = require('dotenv').config();
2021-07-20 09:29:42 +08:00
const host = 'localhost';
2024-04-12 08:14:21 +08:00
const port = dotenv.parsed.DEV_PORT || 8000;
2021-07-20 09:29:42 +08:00
const app = express();
2024-04-17 06:56:57 +08:00
const wwwDir = resolve(__dirname, './app/www');
2021-07-20 09:29:42 +08:00
const devDir = resolve(__dirname, './www-dev');
2024-04-12 08:14:21 +08:00
const runtimeDir = process.argv[2] === '--pwa' ? wwwDir : devDir;
2023-11-15 08:11:50 +08:00
const webIndex = resolve(runtimeDir, './index.html');
2022-12-29 03:21:10 +08:00
2023-08-10 22:04:04 +08:00
express.static.mime.define({
2023-11-15 08:11:50 +08:00
'application/javascript': ['js'],
2023-08-10 22:04:04 +08:00
'application/json': ['json'],
'image/vnd.microsoft.icon': ['ico'],
'image/png': ['png'],
'image/svg+xml': ['svg'],
2023-11-15 08:11:50 +08:00
'text/css': ['css'],
'text/html': ['html'],
2023-08-10 22:04:04 +08:00
});
2024-02-09 01:54:31 +08:00
app.get('/', (_, res) => res.sendFile(webIndex));
app.get('/privacy-policy.html', (_, res) => res.sendFile(resolve(wwwDir, './privacy-policy.html')));
app.use('/apple-touch-icon.png', express.static(resolve(wwwDir, './apple-touch-icon.png')));
app.use('/favicon.ico', express.static(resolve(wwwDir, './favicon.ico')));
app.use('/favicon.svg', express.static(resolve(wwwDir, './favicon.svg')));
app.use('/icon-192.png', express.static(resolve(wwwDir, './icon-192.png')));
app.use('/icon-512.png', express.static(resolve(wwwDir, './icon-512.png')));
app.use('/manifest.json', express.static(resolve(wwwDir, './manifest.json')));
app.use('/auth', express.static(resolve(wwwDir, './auth')));
2024-04-17 06:56:57 +08:00
app.get('/:version/:id.css', async (req, res) =>
res.sendFile(resolve(runtimeDir, `./${req.params.version}/${req.params.id}.css`))
2023-11-15 08:11:50 +08:00
);
2024-02-09 01:54:31 +08:00
app.get('/:version/:id.js', async (req, res) =>
2023-11-15 08:11:50 +08:00
res.sendFile(resolve(runtimeDir, `./${req.params.version}/${req.params.id}.js`))
2022-12-29 03:21:10 +08:00
);
2024-02-09 01:54:31 +08:00
app.get('/:version/lib/:id.js', async (req, res) =>
2023-11-15 08:11:50 +08:00
res.sendFile(resolve(runtimeDir, `./${req.params.version}/lib/${req.params.id}.js`))
2022-12-29 03:21:10 +08:00
);
2021-07-20 09:29:42 +08:00
app.get('*', (_, res) => res.redirect('/'));
app.listen(port, host, () => {
2023-06-06 02:03:14 +08:00
const timestamp = new Date().toLocaleString();
2021-07-20 09:29:42 +08:00
console.info(`Serving from: http://${host}:${port}`);
2022-12-29 03:21:10 +08:00
console.info(`Using files from: ${runtimeDir}`);
2021-07-20 09:29:42 +08:00
console.info(`Server started at: ${timestamp}`);
});