2023-04-30 22:04:13 +02:00
|
|
|
import { Signale } from 'signale';
|
|
|
|
import ConfigHandler from './assets/configHandler';
|
2023-05-15 21:58:01 +02:00
|
|
|
import express, { NextFunction, Request, Response } from 'express';
|
2023-05-02 18:23:47 +02:00
|
|
|
import fileUpload from 'express-fileupload';
|
2023-04-30 22:04:13 +02:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
2023-05-01 22:15:35 +02:00
|
|
|
import * as eta from 'eta';
|
2023-05-08 23:30:19 +02:00
|
|
|
import bodyParser from 'body-parser';
|
2023-05-01 22:15:35 +02:00
|
|
|
|
2023-05-15 21:58:01 +02:00
|
|
|
// Sentry
|
|
|
|
import * as Sentry from '@sentry/node';
|
|
|
|
import * as Tracing from '@sentry/tracing';
|
|
|
|
|
2023-05-01 00:07:13 +02:00
|
|
|
import routes from './routes/index.js';
|
2023-04-30 22:04:13 +02:00
|
|
|
|
|
|
|
// Get app directory.
|
2023-05-01 00:14:16 +02:00
|
|
|
export const __path = process.argv[1];
|
2023-04-30 22:04:13 +02:00
|
|
|
|
|
|
|
const logger_settings = {
|
|
|
|
disabled: false,
|
|
|
|
logLevel: 'info',
|
|
|
|
scope: 'Core',
|
|
|
|
stream: process.stdout,
|
|
|
|
displayFilename: true
|
|
|
|
};
|
|
|
|
|
|
|
|
const coreLogger = new Signale(logger_settings);
|
2023-05-01 00:07:13 +02:00
|
|
|
export const log = {
|
2023-04-30 22:04:13 +02:00
|
|
|
core: coreLogger,
|
|
|
|
db: coreLogger.scope('DB'),
|
|
|
|
web: coreLogger.scope('WEB')
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a new config instance.
|
2023-05-01 22:15:35 +02:00
|
|
|
export const config = new ConfigHandler(__path + '/config.json', {
|
2023-04-30 22:04:13 +02:00
|
|
|
db_connection_string: 'mysql://USER:PASSWORD@HOST:3306/DATABASE',
|
|
|
|
http_listen_address: '127.0.0.1',
|
2023-05-01 22:15:35 +02:00
|
|
|
http_port: 3000,
|
2023-05-15 21:58:01 +02:00
|
|
|
sentry_dsn: 'https://ID@sentry.example.com/PROJECTID',
|
2023-05-01 22:15:35 +02:00
|
|
|
debug: false
|
2023-04-30 22:04:13 +02:00
|
|
|
});
|
|
|
|
|
2023-05-01 00:07:13 +02:00
|
|
|
export const prisma = new PrismaClient({
|
2023-04-30 22:04:13 +02:00
|
|
|
datasources: {
|
|
|
|
db: {
|
|
|
|
url: config.global.db_connection_string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-05-01 00:07:13 +02:00
|
|
|
export const app = express();
|
2023-05-15 21:58:01 +02:00
|
|
|
Sentry.init({
|
|
|
|
dsn: config.global.sentry_dsn,
|
|
|
|
integrations: [
|
|
|
|
// enable HTTP calls tracing
|
|
|
|
new Sentry.Integrations.Http({ tracing: true }),
|
|
|
|
// enable Express.js middleware tracing
|
2023-05-15 22:57:00 +02:00
|
|
|
new Tracing.Integrations.Express({ app }),
|
|
|
|
// @ts-ignore
|
|
|
|
new Sentry.Integrations.Prisma({ prisma })
|
2023-05-15 21:58:01 +02:00
|
|
|
],
|
|
|
|
|
|
|
|
// Set tracesSampleRate to 1.0 to capture 100%
|
|
|
|
// of transactions for performance monitoring.
|
|
|
|
// We recommend adjusting this value in production
|
2023-05-15 22:57:00 +02:00
|
|
|
tracesSampleRate: config.global.debug ? 1.0 : 0.5,
|
|
|
|
environment: config.global.debug ? 'development' : 'production'
|
2023-05-15 21:58:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// RequestHandler creates a separate execution context using domains, so that every
|
|
|
|
// transaction/span/breadcrumb is attached to its own Hub instance
|
|
|
|
app.use(Sentry.Handlers.requestHandler());
|
|
|
|
// TracingHandler creates a trace for every incoming request
|
|
|
|
app.use(Sentry.Handlers.tracingHandler());
|
|
|
|
|
2023-05-04 20:21:10 +02:00
|
|
|
app.set('x-powered-by', false);
|
|
|
|
app.engine('html', eta.renderFile);
|
2023-05-08 23:30:19 +02:00
|
|
|
|
|
|
|
// app.use(cors());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
|
|
|
|
// Using bodyParser to parse JSON bodies into JS objects
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
2023-05-02 18:23:47 +02:00
|
|
|
app.use(fileUpload());
|
2023-05-04 20:21:10 +02:00
|
|
|
app.use(express.static(__path + '/static'));
|
2023-05-15 02:08:43 +02:00
|
|
|
|
2023-05-04 20:21:10 +02:00
|
|
|
app.use(routes);
|
2023-04-30 22:04:13 +02:00
|
|
|
|
2023-05-15 21:58:01 +02:00
|
|
|
// The error handler must be before any other error middleware and after all controllers
|
|
|
|
app.use(Sentry.Handlers.errorHandler());
|
|
|
|
|
2023-04-30 22:04:13 +02:00
|
|
|
app.listen(config.global.http_port, config.global.http_listen_address, () => {
|
|
|
|
log.web.info(`Listening at http://${config.global.http_listen_address}:${config.global.http_port}`);
|
|
|
|
});
|