ws4kp/index.js

48 lines
1 KiB
JavaScript
Raw Normal View History

2020-09-04 18:02:20 +00:00
// express
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
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
const version = require('./version');
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', () => {
server.close(()=> {
console.log('Server closed');
});
});