-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
102 lines (86 loc) · 3.01 KB
/
server.ts
File metadata and controls
102 lines (86 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Import librarys used
import * as express from 'express';
import * as exegesisExpress from 'exegesis-express';
import * as path from 'path';
import * as fs from 'fs';
//You may choose HTTP or HTTPS, if HTTPS you need a SSL Cert
import * as http from 'http';
import * as swaggerUI from './swagger-ui';
import {Request, Response} from "express";
const PORT = 3000;
export interface SwaggerUIConfig {
title?: string;
url: string;
}
async function createServer() {
// See https://github.com/exegesis-js/exegesis/blob/master/docs/Options.md
const options = {
controllers: path.resolve(__dirname, './controllers'),
controllersPattern: "**/*.@(ts|js)"
};
// This creates an exgesis middleware, which can be used with express,
// connect, or even just by itself.
const exegesisMiddleware = await exegesisExpress.middleware(
path.resolve(__dirname, './api_docs/openapi.yaml'),
options
);
const app = express();
// If you have any body parsers, this should go before them.
app.use(exegesisMiddleware);
//This appears to be needed to generate the docs correctly.
/**
* UI Doc route
* This route is responsible for reading the contents of the openapi.yaml file and then sending it as a JSON object.
*
* This JSON object is then used to generate the swagger docs
*/
app.route('/uidoc')
.get((req: Request, res: Response) => {
res.setHeader('content-type', 'application/json');
res.send(fs.readFileSync(path.resolve(__dirname, './api_docs/openapi.yaml'),));
})
/**
* Route responsible for the generation of UI docs
*
* Gets the OpenAPI spec from /uidoc route as a JSON object
* Takes this JSON object and generates docs using the swagger-ui.ts file.
*/
app.route('/ui')
.get((req: Request, res: Response) => {
//Send the Swagger-UI docs with our OpenAPI spec applied.
res.send(swaggerUI.swaggerUI("/uidoc"));
})
// Return a 404
app.use((req, res) => {
res.status(404).json({message: `Not found`});
});
// Handle any unexpected errors
app.use((err, req, res, next) => {
res.status(500).json({message: `Internal error: ${err.message}`});
});
//Used to create a HTTP Server
const server = http.createServer(app);
/**
* If you want to run a HTTPS server instead you must:
* + Get a SSL Cert and Key to use
* + Change the server type from http to https as shown below
*
const httpsOptions = {
key: fs.readFileSync('./config/key.pem'),
cert: fs.readFileSync('./config/cert.pem')
}
const server = https.createServer(httpsOptions,app);
*/
return server;
}
//Run our createServer function
createServer()
.then(server => {
server.listen(PORT);
console.log("Listening on port 3000");
console.log("Docs are located at the /ui route!");
})
.catch(err => {
console.error(err.stack);
process.exit(1);
});