NotificationLatch/index.js

78 lines
2.2 KiB
JavaScript
Raw Normal View History

const Container = require('./src/container');
const winston = require('winston');
const express = require('express');
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');
const logger = winston.createLogger({
level: 'verbose',
});
logger.add(new winston.transports.Console({
format: winston.format.simple()
2021-08-18 13:50:34 +00:00
}));
const container = new Container();
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);
async function asyncMain()
{
let config;
if (await container.AsyncFs.exists('config.js'))
{
logger.verbose('Using config.js');
config = require('./config.js');
}
else
{
logger.verbose('No config.js found, using config.default.js');
config = require('./config.default.js');
}
container.registerInstance('Config', config);
await container.NotificationRepository.init();
2021-08-18 13:50:34 +00:00
container.ReminderScheduler.start();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/api', container.ApiRoutes.createRouter(express));
app.use('/', express.static('frontend/dist'));
2021-08-18 13:50:34 +00:00
app.listen(config.port, '0.0.0.0', () =>
{
logger.info(`NotificationLatch listening at http://localhost:${config.port}`);
});
}
asyncMain()
.catch(err =>
{
logger.error(`Unhandled exception: ${err}`);
logger.verbose(err.stack);
});