2025-03-22 13:19:36 +01:00
|
|
|
import express from 'express';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import corsPassThru from './cors/index.mjs';
|
|
|
|
|
import radarPassThru from './cors/radar.mjs';
|
|
|
|
|
import outlookPassThru from './cors/outlook.mjs';
|
2025-03-22 13:52:00 +01:00
|
|
|
import playlist from './src/playlist.mjs';
|
2020-12-29 09:58:01 -06:00
|
|
|
|
2020-09-04 13:02:20 -05:00
|
|
|
const app = express();
|
2020-12-29 09:58:01 -06:00
|
|
|
const port = process.env.WS4KP_PORT ?? 8080;
|
2020-09-04 13:02:20 -05:00
|
|
|
|
|
|
|
|
// template engine
|
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
|
|
|
|
|
// cors pass-thru to api.weather.gov
|
2025-04-02 22:34:59 -05:00
|
|
|
app.get('/stations/*station', corsPassThru);
|
|
|
|
|
app.get('/Conus/*radar', radarPassThru);
|
|
|
|
|
app.get('/products/*product', outlookPassThru);
|
2025-03-22 13:52:00 +01:00
|
|
|
app.get('/playlist.json', playlist);
|
2020-09-04 13:02:20 -05:00
|
|
|
|
2020-09-08 14:39:17 -05:00
|
|
|
// version
|
2021-01-04 11:58:58 -06:00
|
|
|
const { version } = JSON.parse(fs.readFileSync('package.json'));
|
2020-09-04 13:02:20 -05:00
|
|
|
|
|
|
|
|
const index = (req, res) => {
|
2025-03-22 13:19:36 +01:00
|
|
|
res.render('index', {
|
2020-09-04 13:02:20 -05:00
|
|
|
production: false,
|
2020-09-08 14:39:17 -05:00
|
|
|
version,
|
2020-09-04 13:02:20 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-07 10:53:18 -06:00
|
|
|
// debugging
|
2023-01-17 16:10:06 -06:00
|
|
|
if (process.env?.DIST === '1') {
|
2022-12-07 10:53:18 -06:00
|
|
|
// distribution
|
2025-03-22 13:19:36 +01:00
|
|
|
app.use('/images', express.static('./server/images'));
|
|
|
|
|
app.use('/fonts', express.static('./server/fonts'));
|
|
|
|
|
app.use('/scripts', express.static('./server/scripts'));
|
|
|
|
|
app.use('/', express.static('./dist'));
|
2023-01-17 16:10:06 -06:00
|
|
|
} else {
|
|
|
|
|
// debugging
|
|
|
|
|
app.get('/index.html', index);
|
|
|
|
|
app.get('/', index);
|
2025-04-02 22:34:59 -05:00
|
|
|
app.get('*name', express.static('./server'));
|
2022-12-07 10:53:18 -06:00
|
|
|
}
|
2020-09-04 13:02:20 -05:00
|
|
|
|
|
|
|
|
const server = app.listen(port, () => {
|
|
|
|
|
console.log(`Server listening on port ${port}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// graceful shutdown
|
|
|
|
|
process.on('SIGINT', () => {
|
2021-01-04 11:58:58 -06:00
|
|
|
server.close(() => {
|
2020-09-04 13:02:20 -05:00
|
|
|
console.log('Server closed');
|
|
|
|
|
});
|
|
|
|
|
});
|