NotificationLatch/index.js

75 lines
2.0 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');
const logger = winston.createLogger({
level: 'verbose',
});
logger.add(new winston.transports.Console({
format: winston.format.simple()
}))
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);
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();
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'));
app.listen(config.port, () =>
{
logger.info(`NotificationLatch listening at http://localhost:${config.port}`);
});
}
asyncMain()
.catch(err =>
{
logger.error(`Unhandled exception: ${err}`);
logger.verbose(err.stack);
});