1
- import { serve as stdServe , serveTLS } from 'https://deno.land/[email protected] /http/server.ts'
1
+ import { serve as stdServe , serveTLS , Server as StdServer } from 'https://deno.land/[email protected] /http/server.ts'
2
2
import log from '../shared/log.ts'
3
3
import type { ServerRequest } from '../types.ts'
4
4
import { Application } from './app.ts'
@@ -18,17 +18,15 @@ export type ServeOptions = {
18
18
certFile ?: string
19
19
/** Server public key file. */
20
20
keyFile ?: string
21
+ /* The signal to close the server. */
22
+ signal ?: AbortSignal
21
23
}
22
24
23
25
/** Create a standard Aleph server. */
24
- export async function serve ( { app, port, hostname, certFile, keyFile } : ServeOptions ) {
26
+ export async function serve ( { app, port, hostname, certFile, keyFile, signal } : ServeOptions ) {
25
27
const server = new Server ( app )
26
28
await app . ready
27
29
28
- if ( ! app . isDev && app . config . compress ) {
29
- await compress . init ( )
30
- }
31
-
32
30
while ( true ) {
33
31
try {
34
32
let s : AsyncIterable < ServerRequest >
@@ -37,6 +35,12 @@ export async function serve({ app, port, hostname, certFile, keyFile }: ServeOpt
37
35
} else {
38
36
s = stdServe ( { port, hostname } )
39
37
}
38
+ signal ?. addEventListener ( 'abort' , ( ) => {
39
+ ( s as StdServer ) . close ( )
40
+ } )
41
+ if ( ! app . isDev && app . config . compress ) {
42
+ await compress . init ( )
43
+ }
40
44
log . info ( `Server ready on http://${ hostname || 'localhost' } :${ port } ${ app . config . basePath } ` )
41
45
for await ( const r of s ) {
42
46
server . handle ( r )
0 commit comments