1
- import fastify , { FastifyInstance } from "fastify" ;
1
+ import fastify , { FastifyInstance } from "fastify" ;
2
2
import fastifyCompress from "fastify-compress" ;
3
3
import fastifyCors from "fastify-cors" ;
4
4
import fastifyEnv from "fastify-env" ;
5
5
import fastifyFormBody from "fastify-formbody" ;
6
6
import fastifyHealthCheck from "fastify-healthcheck" ;
7
- import { fastifyHelmet } from "fastify-helmet" ;
7
+ import { fastifyHelmet } from "fastify-helmet" ;
8
8
import fastifyMetrics from "fastify-metrics" ;
9
9
import fastifyRateLimit from "fastify-rate-limit" ;
10
10
import fastifySensible from "fastify-sensible" ;
11
11
import fastifySwagger from "fastify-swagger" ;
12
- import { Connection } from "typeorm" ;
12
+ import { Connection } from "typeorm" ;
13
13
14
- import { config as envPluginConfig } from "./config" ;
15
- import { getDatabaseConnection } from "./services/db" ;
16
- import { logger } from "./services/logger" ;
17
- import { fastifyLogger } from "./services/logger/fastify" ;
18
- import { routesPlugin } from "./services/plugins/routes" ;
19
- import { SWAGGER_CONFIG } from "./services/swagger" ;
14
+ import { config as envPluginConfig } from "./config" ;
15
+ import { getDatabaseConnection } from "./services/db" ;
16
+ import { logger } from "./services/logger" ;
17
+ import { fastifyLogger } from "./services/logger/fastify" ;
18
+ import { routesPlugin } from "./services/plugins/routes" ;
19
+ import { SWAGGER_CONFIG } from "./services/swagger" ;
20
20
export class App {
21
-
22
21
public readonly instance : FastifyInstance ;
23
22
24
23
protected constructor ( instance : FastifyInstance ) {
@@ -32,7 +31,7 @@ export class App {
32
31
public static async init ( ) : Promise < App > {
33
32
const instance = fastify ( {
34
33
logger : fastifyLogger ,
35
- return503OnClosing : true
34
+ return503OnClosing : true ,
36
35
} ) ;
37
36
const app = new App ( instance ) ;
38
37
await app . registerPlugins ( ) ;
@@ -41,36 +40,44 @@ export class App {
41
40
42
41
public async start ( ) : Promise < void > {
43
42
try {
44
-
45
43
await this . initDb ( ) ;
46
44
await this . instance . ready ( ) ;
47
45
logger . info ( this . instance . printRoutes ( ) ) ;
48
46
return new Promise ( ( resolve , reject ) => {
49
47
this . instance . listen (
50
48
this . instance . config . SERVER_PORT ,
51
- this . instance . config . SERVER_ADDRESS , ( err ) => {
49
+ this . instance . config . SERVER_ADDRESS ,
50
+ ( err ) => {
52
51
if ( err ) {
53
52
logger . error ( "Failed to start server: " , err ) ;
54
53
reject ( ) ;
55
54
}
56
55
resolve ( ) ;
57
- } ) ;
56
+ }
57
+ ) ;
58
58
} ) ;
59
59
} catch ( error ) {
60
- logger . error ( `Error occurred during app startup because of: ${ error . stack } ` ) ;
60
+ logger . error (
61
+ `Error occurred during app startup because of: ${ error . stack } `
62
+ ) ;
61
63
this . stop ( undefined ) ;
62
64
}
63
65
}
64
66
65
67
public async stop ( signal : string | undefined ) : Promise < void > {
66
- await this . instance . db . close ( )
67
- . catch ( error =>
68
- logger . error ( `Error occurred during database closing because: ${ error . message } ` )
68
+ await this . instance . db
69
+ . close ( )
70
+ . catch ( ( error ) =>
71
+ logger . error (
72
+ `Error occurred during database closing because: ${ error . message } `
73
+ )
69
74
) ;
70
75
try {
71
76
await this . instance . close ( ) ;
72
77
} catch ( e ) {
73
- logger . error ( `Error occurred during server closing because: ${ e . message } ` ) ;
78
+ logger . error (
79
+ `Error occurred during server closing because: ${ e . message } `
80
+ ) ;
74
81
}
75
82
76
83
if ( signal !== "TEST" ) {
@@ -80,17 +87,25 @@ export class App {
80
87
81
88
private async initDb ( ) : Promise < void > {
82
89
this . instance . decorate ( "db" , await getDatabaseConnection ( ) ) ;
83
- await this . instance . db . runMigrations ( { transaction : "all" } ) ;
90
+ await this . instance . db . runMigrations ( { transaction : "all" } ) ;
84
91
}
85
92
86
93
private async registerPlugins ( ) : Promise < void > {
87
94
this . instance . register ( fastifyEnv , envPluginConfig ) ;
88
95
await this . instance . after ( ) ;
89
- this . instance . register ( fastifyCompress , { global : true , encodings : [ "gzip" , "deflate" ] } ) ;
90
- this . instance . register ( fastifyCors , { origin : this . instance . config . CORS_ORIGIN } ) ;
96
+ this . instance . register ( fastifyCompress , {
97
+ global : true ,
98
+ encodings : [ "gzip" , "deflate" ] ,
99
+ } ) ;
100
+ this . instance . register ( fastifyCors , {
101
+ origin : this . instance . config . CORS_ORIGIN ,
102
+ } ) ;
91
103
this . instance . register ( fastifyFormBody ) ;
92
104
this . instance . register ( fastifyHelmet ) ;
93
- this . instance . register ( fastifyRateLimit , { max : this . instance . config . MAX_REQ_PER_MIN , timeWindow : '1 minute' } ) ;
105
+ this . instance . register ( fastifyRateLimit , {
106
+ max : this . instance . config . MAX_REQ_PER_MIN ,
107
+ timeWindow : "1 minute" ,
108
+ } ) ;
94
109
this . instance . register ( fastifySensible ) ;
95
110
this . instance . register ( fastifySwagger , SWAGGER_CONFIG ) ;
96
111
this . instance . register ( fastifyHealthCheck , {
@@ -100,20 +115,20 @@ export class App {
100
115
healthCheckInterval : 5000 ,
101
116
healthCheck : async ( ) => {
102
117
return true ;
103
- }
104
- }
118
+ } ,
119
+ } ,
105
120
} ) ;
106
121
if ( this . instance . config . NODE_ENV !== "test" ) {
107
122
this . instance . register ( fastifyMetrics , {
108
- blacklist : ' /metrics' ,
109
- enableDefaultMetrics : true
123
+ blacklist : " /metrics" ,
124
+ enableDefaultMetrics : true ,
110
125
} ) ;
111
126
}
112
127
this . instance . register ( routesPlugin ) ;
113
128
}
114
129
}
115
130
116
- declare module ' fastify' {
131
+ declare module " fastify" {
117
132
interface FastifyInstance {
118
133
db : Connection ;
119
134
}
0 commit comments