-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.ts
156 lines (130 loc) Β· 4.11 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import cookieParser from 'cookie-parser';
import cors from 'cors';
import express, { Express, NextFunction, Request, Response } from 'express';
import session from 'express-session';
import helmet from 'helmet';
import hpp from 'hpp';
import createError from 'http-errors';
import logger from 'morgan';
import path from 'path';
import swaggerUi from 'swagger-ui-express';
import connectDB from './connectDB';
import routers from './routers';
import swaggerDocument from './swagger/swagger';
import * as Sentry from '@sentry/node';
import * as Tracing from '@sentry/tracing';
class App {
app: Express;
constructor() {
this.app = express();
// db μ μ
this.dbConnection();
// λ·° ν
νλ¦Ώ μμ§ μ
ν
// this.setViewEngine();
// λ―Έλ€μ¨μ΄ μ
ν
this.setMiddleWare();
// μΈμ
μ
ν
// this.setSession();
// μμ λΌμ°ν
μμ ν΄λΉ λΌμ°ν°κ° μμΌλ©΄ 404 νμ΄μ§λ₯Ό μ°Ύμμκ° μμ λ
ΈμΆ
this.status404();
}
dbConnection() {
// connect To DB
connectDB();
}
setSession() {
const sessionOption = {
resave: false,
saveUninitialized: false,
secret: process.env.COOKIE_SECRET || '',
cookie: {
httpOnly: true,
secure: false,
},
proxy: false,
};
if (process.env.NODE_ENV == 'production') {
sessionOption.proxy = true;
// sessionOption.cookie.secure = true;
}
this.app.use(session(sessionOption));
}
setMiddleWare() {
this.app.use(
cors({
origin: true,
credentials: true,
}),
);
if (process.env.NODE_ENV === 'production') {
this.app.enabled('trust proxy');
this.app.use(logger('combined'));
this.app.use(helmet({ contentSecurityPolicy: false }));
this.app.use(hpp());
} else {
this.app.use(logger('dev'));
}
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use(cookieParser(process.env.COOKIE_SECRET || ''));
if (process.env.NODE_ENV === 'production') {
Sentry.init({
dsn: process.env.DSN,
// integrations: [
// enable HTTP calls tracing
// new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
// new Tracing.Integrations.Express({ app: this.app }),
// ],
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
});
}
// this.app.use(Sentry.Handlers.requestHandler());
// this.app.use(Sentry.Handlers.tracingHandler());
// μ μ λλ ν 리 μΆκ°
this.setStatic();
// κΈλ‘λ² λ³μ μ μΈ
// this.setLocals();
// λΌμ°ν
this.getRouting();
// The error handler must be before any other error middleware and after all controllers
if (process.env.NODE_ENV === 'production') {
this.app.use(Sentry.Handlers.errorHandler());
}
// error handler
this.app.use(function (err: Error, req: Request, res: Response, next: NextFunction) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
// res.status(err.status || 500);
res.statusCode = 500;
res.end((res as any).sentry + '\n');
});
}
setStatic() {
this.app.use(express.static(path.join(__dirname, 'public')));
}
getRouting() {
this.app.use('/apiDocs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
this.app.get('/', async (req, res, next) => {
res.json({});
});
this.app.get('/favicon.ico', function (req, res, next) {});
this.app.get('/service-worker.js', function (req, res, next) {});
this.app.use('/api/v1', routers);
this.app.get('/debug-sentry', function mainHandler(req, res) {
throw new Error('My first Sentry error!');
});
}
status404() {
// catch 404 and forward to error handler
this.app.use(function (req, res, next) {
next(createError(404));
});
}
}
const { app } = new App();
export default app;