2021-08-17 19:25:27 +00:00
|
|
|
const Container = require('./src/container');
|
|
|
|
const winston = require('winston');
|
2021-08-17 17:35:40 +00:00
|
|
|
const express = require('express');
|
2021-08-17 19:25:27 +00:00
|
|
|
|
|
|
|
const AsyncFs = require('./src/asyncfs');
|
|
|
|
const DateTimeProvider = require('./src/datetimeprovider');
|
|
|
|
const ApiRoutes = require('./src/routes/api');
|
|
|
|
const NotificationRepository = require('./src/notification/repository');
|
|
|
|
const NotificationFacade = require('./src/notification/facade');
|
|
|
|
const TransportProvider = require('./src/transport/provider');
|
|
|
|
const SubjectParser = require('./src/subjectparser');
|
2021-08-18 13:50:34 +00:00
|
|
|
const ReminderScheduler = require('./src/reminderscheduler');
|
2021-08-17 19:25:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
const logger = winston.createLogger({
|
|
|
|
level: 'verbose',
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.add(new winston.transports.Console({
|
|
|
|
format: winston.format.simple()
|
2021-08-18 13:50:34 +00:00
|
|
|
}));
|
2021-08-17 19:25:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
const container = new Container();
|
2021-08-17 19:38:20 +00:00
|
|
|
container.registerInstance('Logger', logger);
|
|
|
|
container.registerType('AsyncFs', AsyncFs);
|
|
|
|
container.registerType('DateTimeProvider', DateTimeProvider);
|
|
|
|
container.registerType('ApiRoutes', ApiRoutes);
|
|
|
|
container.registerType('NotificationRepository', NotificationRepository);
|
|
|
|
container.registerType('NotificationFacade', NotificationFacade);
|
|
|
|
container.registerType('TransportProvider', TransportProvider);
|
|
|
|
container.registerType('SubjectParser', SubjectParser);
|
2021-08-18 13:50:34 +00:00
|
|
|
container.registerType('ReminderScheduler', ReminderScheduler);
|
2021-08-17 17:35:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
async function asyncMain()
|
|
|
|
{
|
2021-08-17 19:25:27 +00:00
|
|
|
let config;
|
|
|
|
|
|
|
|
if (await container.AsyncFs.exists('config.js'))
|
|
|
|
{
|
|
|
|
logger.verbose('Using config.js');
|
|
|
|
config = require('./config.js');
|
|
|
|
}
|
2021-08-17 17:35:40 +00:00
|
|
|
else
|
2021-08-17 19:25:27 +00:00
|
|
|
{
|
|
|
|
logger.verbose('No config.js found, using config.default.js');
|
|
|
|
config = require('./config.default.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-17 19:38:20 +00:00
|
|
|
container.registerInstance('Config', config);
|
2021-08-17 17:35:40 +00:00
|
|
|
|
2021-08-17 19:25:27 +00:00
|
|
|
|
|
|
|
await container.NotificationRepository.init();
|
2021-08-18 13:50:34 +00:00
|
|
|
container.ReminderScheduler.start();
|
2021-08-17 17:35:40 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2021-08-18 16:50:47 +00:00
|
|
|
|
|
|
|
const logMiddleware = (req, res, next) =>
|
|
|
|
{
|
|
|
|
// TODO merge with api.js' version
|
|
|
|
const ip = req.headers['x-forwarded-for'] || req.ip;
|
|
|
|
logger.verbose(`[${ip}] ${req.path} (body = ${JSON.stringify(req.body)})`);
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
2021-08-17 17:35:40 +00:00
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: false }));
|
2021-08-18 16:50:47 +00:00
|
|
|
app.use(logMiddleware);
|
2021-08-17 17:35:40 +00:00
|
|
|
|
2021-08-17 19:38:20 +00:00
|
|
|
app.use('/api', container.ApiRoutes.createRouter(express));
|
2021-08-17 17:35:40 +00:00
|
|
|
app.use('/', express.static('frontend/dist'));
|
|
|
|
|
2021-08-18 13:50:34 +00:00
|
|
|
app.listen(config.port, '0.0.0.0', () =>
|
2021-08-17 17:35:40 +00:00
|
|
|
{
|
2021-08-17 19:25:27 +00:00
|
|
|
logger.info(`NotificationLatch listening at http://localhost:${config.port}`);
|
2021-08-17 17:35:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-17 19:25:27 +00:00
|
|
|
|
|
|
|
asyncMain()
|
|
|
|
.catch(err =>
|
|
|
|
{
|
|
|
|
logger.error(`Unhandled exception: ${err}`);
|
|
|
|
logger.verbose(err.stack);
|
|
|
|
});
|