-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
100 lines (77 loc) · 2.81 KB
/
handler.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
import express, {Application, Request, Response, NextFunction} from "express";
import {Server} from "http";
import { mongoose} from '@typegoose/typegoose';
import fileUploader from "express-fileupload";
import register from './register';
import createHttpError from "http-errors";
import { config } from "dotenv";
import cors from "cors";
import {logger} from "./events/Logger";
import { MongoMemoryServer } from 'mongodb-memory-server';
import {allowedCors} from "./config/allowedCors";
import {errorHandler} from "./helpers/errorHandler";
config();
let mongo: MongoMemoryServer;
export class Handler {
static app: Application = express();
static server: Server = Handler.app.listen(process.env.PORT, () => console.log('server is connected to port: '+ process.env.PORT));
static async startServer() {
let uri = process.env.ENVIROMENT === 'development'
? process.env.DB_CONNECTION_DEVELOPMENT || ''
: process.env.DB_CONNECTION_PRODUCTION || '';
if (process.env.NODE_ENV === 'test') {
mongo = await MongoMemoryServer.create();
uri = mongo.getUri();
}
//make the connection!!!
await mongoose.connect(uri);
await Handler.server;
//connect to cloudinary
Handler.app.use(express.json());
// custom middleware logger
Handler.app.use(logger);
//allow file upload
Handler.app.use(fileUploader({
useTempFiles: true,
}));
//use cors
Handler.app.use(cors(allowedCors));
//routes
register(Handler.app);
//error handler
Handler.app.use((req: Request, res: Response, next: NextFunction) => {
next(createHttpError(404));
});
//error handler
Handler.app.use(errorHandler)
process.on('SIGINT', () => {
mongoose.connection.close(() => {
console.log('Mongoose default connection is disconnected due to application termination');
process.exit(0);
});
});
}
static async closeServer() {
await Handler.server.close();
await mongoose.disconnect();
}
static async dropMongoDB() {
if (process.env.NODE_ENV === 'test'){
if (mongo) {
await mongoose.connection.dropDatabase()
await mongoose.connection.close()
await mongo.stop();
}
}
}
static async dropCollections() {
if (process.env.NODE_ENV === 'test') {
if (mongo) {
const collections = await mongoose.connection.db.collections();
for(let collection of collections) {
await mongoose.connection.db.dropCollection(collection.collectionName)
}
}
}
}
}