22
33import * as express from 'express' ;
44
5- import { ManifestBackend , ManifestEndpoint } from " ../common/manifest/v1alpha/backend" ;
6- import { loadModule } from " ../common/loader" ;
5+ import { ManifestBackend , ManifestEndpoint } from ' ../common/manifest/v1alpha' ;
6+ import { loadModule } from ' ../common/loader' ;
77
88function extractEndpoints (
9- mod ,
10- endpoints : Record < string , ManifestEndpoint > ,
11- prefix ?: string
9+ mod ,
10+ endpoints : Record < string , ManifestEndpoint > ,
11+ prefix ?: string
1212) {
1313 prefix = prefix || '' ;
1414 for ( const funcName of Object . keys ( mod ) ) {
1515 const child = mod [ funcName ] ;
1616 if (
17- typeof child === 'function' &&
18- child . __endpoint &&
19- typeof child . __endpoint === 'object'
17+ typeof child === 'function' &&
18+ child . __endpoint &&
19+ typeof child . __endpoint === 'object'
2020 ) {
2121 if ( funcName . indexOf ( '-' ) >= 0 ) {
2222 throw new Error (
23- 'Function name "' +
23+ 'Function name "' +
2424 funcName +
2525 '" is invalid. Function names cannot contain dashes.'
2626 ) ;
@@ -36,16 +36,19 @@ function extractEndpoints(
3636 }
3737}
3838
39-
4039async function describeBackend ( functionsDir : string ) : Promise < ManifestBackend > {
4140 const endpoints : Record < string , ManifestEndpoint> = { } ;
4241 const mod = await loadModule ( functionsDir ) ;
4342 extractEndpoints ( mod , endpoints ) ;
4443
45- const backend : ManifestBackend = { endpoints, specVersion : "v1alpha" , requiredAPIs : { } } ;
46- if ( Object . values ( endpoints ) . find ( ep => ep . scheduleTrigger ) ) {
47- backend . requiredAPIs [ "pubsub" ] = "pubsub.googleapis.com" ;
48- backend . requiredAPIs [ "scheduler" ] = "cloudscheduler.googleapis.com" ;
44+ const backend : ManifestBackend = {
45+ endpoints,
46+ specVersion : 'v1alpha1' ,
47+ requiredAPIs : { } ,
48+ } ;
49+ if ( Object . values ( endpoints ) . find ( ( ep ) => ep . scheduleTrigger ) ) {
50+ backend . requiredAPIs [ 'pubsub' ] = 'pubsub.googleapis.com' ;
51+ backend . requiredAPIs [ 'scheduler' ] = 'cloudscheduler.googleapis.com' ;
4952 }
5053 return backend ;
5154}
@@ -68,16 +71,22 @@ if (args.length != 1) {
6871}
6972const functionsDir = args [ 0 ] ;
7073
74+ let server ;
7175const app = express ( ) ;
7276app . get ( '/backend.yaml' , async ( req , res ) => {
7377 const backend = await describeBackend ( functionsDir ) ;
7478 res . setHeader ( 'content-type' , 'text/yaml' ) ;
7579 res . send ( JSON . stringify ( backend ) ) ;
7680} ) ;
7781
82+ app . get ( '/quitquitquit' , async ( req , res ) => {
83+ res . send ( 'ok' ) ;
84+ server . close ( ( ) => console . log ( "shutdown requested via /quitquitquit" ) ) ;
85+ } ) ;
86+
7887let port = 8080 ;
7988if ( process . env . ADMIN_PORT ) {
8089 port = Number . parseInt ( process . env . ADMIN_PORT ) ;
8190}
8291console . error ( 'Serving at port' , port ) ;
83- app . listen ( port ) ;
92+ server = app . listen ( port ) ;
0 commit comments