File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { Server } from "hyper-express";
3
3
import { dispatchHandlerController } from "./dispatch-handler/controller" ;
4
4
import { getConfig } from "./utils/config" ;
5
5
import { getLogger } from "./utils/logger" ;
6
+ import { getOpenApiSpec } from "./utils/openapi" ;
6
7
7
8
/**
8
9
* Describes the result of the bootstrap function.
@@ -32,6 +33,20 @@ export const bootstrap = (async (): Promise<BootstrapResult> => {
32
33
// Register the handler controller.
33
34
server . post ( "/dispatch" , dispatchHandlerController ) ;
34
35
36
+ // Serve the OpenAPI specification as a static file.
37
+ server . get ( "/openapi.yaml" , async ( _ , res ) => {
38
+ const content = await getOpenApiSpec ( ) ;
39
+
40
+ res
41
+ . status ( 200 )
42
+ . header (
43
+ "cache-control" ,
44
+ "public, max-age 172800, stale-while-revalidate 172800"
45
+ )
46
+ . header ( "content-type" , "application/yaml" )
47
+ . send ( content ) ;
48
+ } ) ;
49
+
35
50
// Set up a global not found handler
36
51
server . set_not_found_handler ( ( _ , res ) => {
37
52
res . status ( 404 ) . json ( {
Original file line number Diff line number Diff line change
1
+ import fsp from "fs/promises" ;
2
+ import path from "path" ;
3
+
4
+ let _cache : string | null = null ;
5
+
6
+ /**
7
+ * Provides the OpenAPI specification as a string in YAML format.
8
+ */
9
+ export async function getOpenApiSpec ( ) : Promise < string > {
10
+ if ( ! _cache ) {
11
+ const specPath = path . join ( __dirname , "../assets/openapi/openapi.yaml" ) ;
12
+
13
+ try {
14
+ _cache = await fsp . readFile ( specPath , "utf-8" ) ;
15
+ } catch ( err ) {
16
+ console . error ( "Failed to read OpenAPI spec file" , err ) ;
17
+
18
+ throw err ;
19
+ }
20
+ }
21
+
22
+ return _cache ;
23
+ }
You can’t perform that action at this time.
0 commit comments