1
|
|
|
import express, { Express } from 'express'; |
2
|
|
|
import { ExpressBeansOptions } from '@/ExpressBeansTypes'; |
3
|
|
|
import { logger, registeredBeans } from '@/decorators'; |
4
|
|
|
|
5
|
|
|
export default class ExpressBeans { |
6
|
|
|
private readonly app: Express; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Creates a new ExpressBeans application |
10
|
|
|
* @param options {ExpressBeansOptions} |
11
|
|
|
*/ |
12
|
|
|
static createApp(options?: Partial<ExpressBeansOptions>) { |
13
|
|
|
return new ExpressBeans(options); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor of ExpressBeans application |
18
|
|
|
* @constructor |
19
|
|
|
* @param options {ExpressBeansOptions} |
20
|
|
|
* @param onInitialized {Function} |
21
|
|
|
*/ |
22
|
|
|
constructor(options?: Partial<ExpressBeansOptions>) { |
23
|
|
|
this.app = express(); |
24
|
|
|
this.app.disable('x-powered-by'); |
25
|
|
|
this.initialize(options || {}); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes the application and checks |
30
|
|
|
* if all beans are valid |
31
|
|
|
* @param listen {boolean} |
32
|
|
|
* @param port {number} |
33
|
|
|
* @param beans {Object[]} |
34
|
|
|
* @param onInitialized {Function} |
35
|
|
|
* @private |
36
|
|
|
*/ |
37
|
|
|
private initialize({ |
38
|
|
|
listen = true, |
39
|
|
|
port = 8080, |
40
|
|
|
routerBeans = [], |
41
|
|
|
onInitialized, |
42
|
|
|
onError, |
43
|
|
|
}: Partial<ExpressBeansOptions>) { |
44
|
|
|
const invalidBeans = routerBeans |
45
|
|
|
.filter(((bean) => !bean.isExpressBean)) |
46
|
|
|
.map((object) => object.prototype.constructor.name); |
47
|
|
|
if (invalidBeans.length > 0) { |
48
|
|
|
throw new Error(`Trying to use something that is not an ExpressBean: ${invalidBeans.join(', ')}`); |
49
|
|
|
} |
50
|
|
|
setImmediate(() => { |
51
|
|
|
try { |
52
|
|
|
Array.from(registeredBeans.values()) |
53
|
|
|
.filter((bean) => bean.routerConfig) |
54
|
|
|
.forEach((bean) => { |
55
|
|
|
try { |
56
|
|
|
const { path, router } = bean.routerConfig!; |
57
|
|
|
logger.debug(`Registering router ${bean.className}`); |
58
|
|
|
this.app.use(path, router); |
59
|
|
|
} catch (e) { |
60
|
|
|
logger.error(e); |
61
|
|
|
throw new Error(`Router ${bean.className} not initialized correctly`); |
62
|
|
|
} |
63
|
|
|
}); |
64
|
|
|
if (listen) { |
65
|
|
|
this.app.listen(port, () => { |
66
|
|
|
logger.info(`Server listening on port ${port}`); |
67
|
|
|
if (onInitialized) { |
68
|
|
|
onInitialized(); |
69
|
|
|
} |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
} catch (err: any) { |
73
|
|
|
if (onError) { |
74
|
|
|
onError(err); |
75
|
|
|
} else { |
76
|
|
|
throw err; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets Express application |
84
|
|
|
* @returns {Express} |
85
|
|
|
*/ |
86
|
|
|
getApp() { |
87
|
|
|
return this.app; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Exposes use function of Express application |
92
|
|
|
* @param handlers |
93
|
|
|
*/ |
94
|
|
|
use(...handlers: any) { |
95
|
|
|
this.app.use(...handlers); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|