-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
82 lines (69 loc) · 2.01 KB
/
app.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
const express = require('express');
const jwt = require('jsonwebtoken');
const YAML = require('yamljs');
const path = require('path');
const openapiValidator = require('express-openapi-validator');
const fs = require('fs');
const app = express();
app.use(express.json());
// Load your OpenAPI spec file
const openApiSpecPath = path.join(__dirname, 'openapi.yaml');
const openApiDocument = YAML.load(openApiSpecPath);
app.use(
openapiValidator.middleware({
apiSpec: openApiDocument,
validateRequests: true, // (default)
validateResponses: true, // false by default
}),
);
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({ status: 'OK' });
});
app.use((req, res, next) => {
if (req.path === '/health') {
return next();
}
// Extract username from X-Consumer-Username header
const usernameHeader = req.headers['x-consumer-username'];
if (usernameHeader) {
// If a valid username is found, set it in the req object and continue
if (typeof usernameHeader === 'string') {
req.user = usernameHeader;
next();
} else {
// If there is no valid username, reject the request
res.status(401).end();
}
} else {
// If there is no username, reject the request
res.status(401).end();
}
});
// Load the customer data from the JSON file
let customerData = [];
fs.readFile(path.join(__dirname, 'customers.json'), 'utf8', (err, data) => {
if (err) throw err;
customerData = JSON.parse(data);
});
app.get('/customer', (req, res) => {
const customer = customerData.find(c => c.username === req.user);
if (customer) {
res.json(customer);
} else {
res.status(404).end();
}
});
// Error handler
app.use((err, req, res, next) => {
// If it's the health check path, provide a clear response
if (req.path === '/health') {
return res.status(503).json({ status: 'unhealthy' });
}
// format error for other paths
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
module.exports = app;