ws4kp/index.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-09-04 18:02:20 +00:00
// express
2021-01-04 17:58:58 +00:00
// eslint-disable-next-line import/no-extraneous-dependencies
2020-09-04 18:02:20 +00:00
const express = require('express');
2020-12-29 15:58:01 +00:00
2020-09-04 18:02:20 +00:00
const app = express();
2020-12-29 15:58:01 +00:00
const port = process.env.WS4KP_PORT ?? 8080;
2020-09-04 18:02:20 +00:00
const path = require('path');
// template engine
app.set('view engine', 'ejs');
// cors pass through
2021-01-04 17:58:58 +00:00
const fs = require('fs');
2020-09-04 18:02:20 +00:00
const corsPassThru = require('./cors');
2020-09-04 22:03:03 +00:00
const radarPassThru = require('./cors/radar');
2020-09-23 19:43:49 +00:00
const outlookPassThru = require('./cors/outlook');
2020-09-04 18:02:20 +00:00
// cors pass-thru to api.weather.gov
app.get('/stations/*', corsPassThru);
2020-09-04 22:03:03 +00:00
app.get('/Conus/*', radarPassThru);
2020-09-23 19:43:49 +00:00
app.get('/products/*', outlookPassThru);
2020-09-04 18:02:20 +00:00
2020-09-08 19:39:17 +00:00
// version
2021-01-04 17:58:58 +00:00
const { version } = JSON.parse(fs.readFileSync('package.json'));
2020-09-04 18:02:20 +00:00
const index = (req, res) => {
res.render(path.join(__dirname, 'views/index'), {
production: false,
2020-09-08 19:39:17 +00:00
version,
2020-09-04 18:02:20 +00:00
});
};
2020-10-20 16:33:44 +00:00
// main page
2020-09-04 18:02:20 +00:00
app.get('/index.html', index);
app.get('/', index);
// fallback
2020-10-20 16:33:44 +00:00
app.get('*', express.static(path.join(__dirname, './server')));
2020-09-04 18:02:20 +00:00
const server = app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
// graceful shutdown
process.on('SIGINT', () => {
2021-01-04 17:58:58 +00:00
server.close(() => {
2020-09-04 18:02:20 +00:00
console.log('Server closed');
});
});