This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
86 lines (73 loc) · 2.65 KB
/
index.js
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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
'use strict';
const path = require('path');
const oav = require('oav');
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
var swaggerSpecDevelopment = require('./openapi/oav-express.json');
var swaggerSpecProduction = require('./openapi/oav-express-production.json');
const ErrorCodes = oav.Constants.ErrorCodes;
const port = process.env.PORT || 8080;
const app = express();
var server;
// LiveValidator configuration options
const liveValidatorOptions = {
git: {
shouldClone: true,
url: 'https://github.com/Azure/azure-rest-api-specs.git'
}
};
//console.log(process.env['NODE_ENV']);
const validator = new oav.LiveValidator(liveValidatorOptions);
//view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.send('Welcome to oav-express');
});
// serve swagger
app.get('/swagger.json', function (req, res) {
res.setHeader('Content-Type', 'application/json');
let host;
if (server && server.address() && server.address().address) {
host = server.address().address;
}
if (host && (host === '::' || host === 'localhost')) {
res.send(swaggerSpecDevelopment);
} else {
res.send(swaggerSpecProduction);
}
});
// This responds a POST request for live validation
app.post('/validate', (req, res) => {
let validationResult = validator.validateLiveRequestResponse(req.body);
// Something went wrong
if (validationResult && validationResult.errors && Array.isArray(validationResult.errors) && validationResult.errors.length) {
let errors = validationResult.errors;
let is400 = errors.some((error) => { return error.code === ErrorCodes.IncorrectInput; });
if (is400) {
// Return 400 with validationResult
return res.send(400, validationResult);
}
}
// Return 200 with validationResult
return res.send(validationResult);
});
console.log('Initializing the validator takes about 30 seconds. Please be patient :-).');
validator.initialize().then(() => {
console.log('Live validator initialized.');
server = app.listen(port, () => {
let host = server.address().address;
let port = server.address().port;
console.log(`oav - express app listening at http://${host}:${port}`);
return server;
});
});