Skip to content

Commit f6ff9d8

Browse files
feat(dandi-contrib/mvc-express): support HttpServer injection token
- move server initialization / port binding out to separate server provider - refactor `ExpressMvcApplication` to use `HttpServer` injection token instead of automatically doing its own port binding
1 parent 2319b03 commit f6ff9d8

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import { EntryPoint, Inject, Injectable, Logger } from '@dandi/core'
1+
import { Server } from 'http'
2+
3+
import { EntryPoint, Inject, Injectable, Injector, Logger } from '@dandi/core'
4+
import { HttpServer } from '@dandi/http-pipeline'
25
import { Route, RouteMapper, Routes } from '@dandi/mvc'
36

47
import * as bodyParser from 'body-parser'
58
import { Express } from 'express'
69

710
import { ExpressInstance } from './express-instance'
8-
import { ExpressMvcConfig } from './express-mvc-config'
911

1012
@Injectable(EntryPoint)
11-
export class ExpressMvcApplication implements EntryPoint {
13+
export class ExpressMvcApplication implements EntryPoint<Server> {
1214
constructor(
1315
@Inject(ExpressInstance) private app: Express,
14-
@Inject(ExpressMvcConfig) private config: ExpressMvcConfig,
1516
@Inject(Routes) private routes: Route[],
1617
@Inject(RouteMapper) private routeMapper: RouteMapper,
18+
@Inject(Injector) private injector: Injector,
1719
@Inject(Logger) private logger: Logger,
1820
) {}
1921

20-
public run(): void {
22+
public async run(): Promise<Server> {
2123
this.app.use(bodyParser.raw({ type: '*/*' }))
2224

2325
for (const route of this.routes) {
2426
this.routeMapper.mapRoute(route)
2527
}
2628

27-
this.logger.debug(`starting on ${this.config.host}:${this.config.port}`)
28-
this.app.listen(this.config.port, this.config.host)
29-
this.logger.info(`listening on http://${this.config.host}:${this.config.port}`)
29+
return await this.injector.inject(HttpServer)
3030
}
3131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ExpressMvcConfig } from '@dandi-contrib/mvc-express'
2+
import { Logger } from '@dandi/core'
3+
import { HttpServer } from '@dandi/http-pipeline'
4+
import { Express } from 'express'
5+
6+
import { ExpressInstance } from './express-instance'
7+
8+
function expressMvcServerFactory(app: Express, config: ExpressMvcConfig, logger: Logger): HttpServer {
9+
logger.debug(`starting on ${config.host}:${config.port}`)
10+
const server = app.listen(config.port, config.host)
11+
logger.info(`listening on http://${config.host}:${config.port}`)
12+
return server
13+
}
14+
15+
export const ExpressMvcServerProvider = {
16+
provide: HttpServer,
17+
useFactory: expressMvcServerFactory,
18+
deps: [ExpressInstance, ExpressMvcConfig, Logger],
19+
}

packages/dandi-contrib/mvc-express/src/mvc-express.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ExpressMvcApplication } from './express-mvc-application'
66
import { ExpressMvcConfig } from './express-mvc-config'
77
import { DefaultExpressMvcConfigProvider, expressMvcConfigFactory } from './express-mvc-config-factory'
88
import { ExpressMvcRouteMapper } from './express-mvc-route-mapper'
9+
import { ExpressMvcServerProvider } from './express-mvc-server-provider'
910
import { localToken } from './local-token'
1011

1112
export class MvcExpressModuleBuilder extends ModuleBuilder<MvcExpressModuleBuilder> {
@@ -23,5 +24,6 @@ export const MvcExpressModule = new MvcExpressModuleBuilder(
2324
ExpressInstanceProvider,
2425
ExpressMvcApplication,
2526
ExpressMvcRouteMapper,
27+
ExpressMvcServerProvider,
2628
MvcModule,
2729
)

0 commit comments

Comments
 (0)