Skip to content

Commit

Permalink
Add server export
Browse files Browse the repository at this point in the history
  • Loading branch information
adaex committed Jun 8, 2021
1 parent 5823530 commit 9830a5c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"name": "coajs",
"image": "mcr.microsoft.com/vscode/devcontainers/javascript-node",
"postCreateCommand": "yarn",
"extensions": ["mutantdino.resourcemonitor", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "eamodio.gitlens"]
"extensions": ["mutantdino.resourcemonitor", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint"]
}
18 changes: 8 additions & 10 deletions lib/service/CoaApplication.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import { echo } from 'coa-echo'
import { _ } from 'coa-helper'
import { createServer, IncomingMessage, ServerResponse } from 'http'
import { createServer, IncomingMessage, Server, ServerResponse } from 'http'
import { CoaRequestBody } from '../base/CoaRequestBody'
import { CoaContext, CoaContextConstructor } from './CoaContext'
import { CoaRouter } from './CoaRouter'

export class CoaApplication<T extends CoaContext> {
private readonly router: CoaRouter<T>
public readonly server: Server
public readonly router: CoaRouter<T>
private readonly Context: CoaContextConstructor<T>
private readonly startAt: bigint = process.hrtime.bigint()

constructor(Context: CoaContextConstructor<T>, router: CoaRouter<T>) {
echo.info('[server] Booting...')
this.Context = Context
this.router = router
echo.info('[server] Booting...')
this.server = createServer((req, res) => {
this.requestListener(req, res).catch((e) => echo.error(e))
})
}

async start(entry: string = '') {
// 设置端口
const port = parseInt(process.env.HOST || '') || 8000

// 启动服务
const server = createServer(
async (req, res) =>
await this.requestListener(req, res).catch((e) => {
echo.error(e)
})
)
server.listen(port, () => {
this.server.listen(port, () => {
echo.info(`[server] Startup successful in: ${Number(process.hrtime.bigint() - this.startAt) / 1e6} ms`)
echo.info(`[server] Listening on: http://localhost:${port}${entry}`)
})
Expand Down
2 changes: 2 additions & 0 deletions lib/service/CoaHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class CoaHttp<T extends CoaContext> {

// 启动应用
await this.application.start(this.config.baseUrl + 'doc')

return { server: this.application.server }
}

// 注册路由
Expand Down

0 comments on commit 9830a5c

Please sign in to comment.