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 ReminderScheduler = require('./src/reminderscheduler'); 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); 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(); container.ReminderScheduler.start(); const app = express(); 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() } app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(logMiddleware); app.use('/api', container.ApiRoutes.createRouter(express)); app.use('/', express.static('frontend/dist')); 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); });