-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
50 lines (41 loc) · 1.2 KB
/
application.js
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
class Application {
constructor({routerAdapter, repository, corsAdapter, Router, databaseAdapter}) {
this.routerAdapter = routerAdapter;
this.app = routerAdapter();
this.repository = repository;
this.corsAdapter = corsAdapter;
this.router = Router;
this.databaseAdapter = databaseAdapter;
}
//================================================
// Middlewares
//================================================
applyMidlewares() {
// Express v4.16.0 and higher
// --------------------------
this.app.use(this.routerAdapter.json());
this.app.use(this.routerAdapter.urlencoded({
extended: true
}));
this.app.use(this.corsAdapter)
this.app.options('*', this.corsAdapter)
const routes = this.router.build();
this.app.use(routes);
}
async connectDb() {
this.databaseAdapter.connect();
return this;
}
setDocs(uiAdapter, docsConfig) {
this.app.use("/api-docs", uiAdapter.serve, uiAdapter.setup(docsConfig));
return this;
}
init(port = 3000) {
this.applyMidlewares();
this.connectDb();
this.app.listen(port);
console.log(`Listening on port ${port}`)
return this.app;
}
}
export default Application