ws4kp/index.js

57 lines
1.5 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
});
};
2022-12-07 16:53:18 +00:00
// debugging
2023-01-17 22:10:06 +00:00
if (process.env?.DIST === '1') {
2022-12-07 16:53:18 +00:00
// distribution
app.use('/images', express.static(path.join(__dirname, './server/images')));
app.use('/fonts', express.static(path.join(__dirname, './server/fonts')));
app.use('/scripts', express.static(path.join(__dirname, './server/scripts')));
app.use('/', express.static(path.join(__dirname, './dist')));
2023-01-17 22:10:06 +00:00
} else {
// debugging
app.get('/index.html', index);
app.get('/', index);
app.get('*', express.static(path.join(__dirname, './server')));
2022-12-07 16:53:18 +00:00
}
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');
});
});