assetflow/src/index.ts
2023-06-27 20:54:47 +02:00

96 lines
2.7 KiB
TypeScript

import { Signale } from 'signale';
import ConfigHandler from './assets/configHandler';
import express, { NextFunction, Request, Response } from 'express';
import fileUpload from 'express-fileupload';
import { PrismaClient } from '@prisma/client';
import * as eta from 'eta';
import bodyParser from 'body-parser';
// Sentry
import * as Sentry from '@sentry/node';
import * as Tracing from '@sentry/tracing';
import routes from './routes/index.js';
// Get app directory.
export const __path = process.argv[1];
const logger_settings = {
disabled: false,
logLevel: 'info',
scope: 'Core',
stream: process.stdout,
displayFilename: false
};
const coreLogger = new Signale(logger_settings);
export const log = {
core: coreLogger,
db: coreLogger.scope('DB'),
web: coreLogger.scope('WEB'),
helper: coreLogger.scope('HELPER')
};
// Create a new config instance.
export const config = new ConfigHandler(__path + '/config.json', {
db_connection_string: 'mysql://USER:PASSWORD@HOST:3306/DATABASE',
http_listen_address: '127.0.0.1',
http_port: 3000,
sentry_dsn: 'https://ID@sentry.example.com/PROJECTID',
debug: false
});
export const prisma = new PrismaClient({
datasources: {
db: {
url: config.global.db_connection_string
}
}
});
export const app = express();
Sentry.init({
dsn: config.global.sentry_dsn,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
// @ts-ignore
new Sentry.Integrations.Prisma({ prisma })
],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: config.global.debug ? 1.0 : 0.5,
environment: config.global.debug ? 'development' : 'production'
});
// 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());
app.set('x-powered-by', false);
app.engine('html', eta.renderFile);
// app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
// Using bodyParser to parse JSON bodies into JS objects
app.use(bodyParser.json());
app.use(fileUpload());
app.use(express.static(__path + '/static'));
app.use(routes);
// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
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}`);
});