-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
114 lines (103 loc) · 3.78 KB
/
Copy pathmiddleware.js
File metadata and controls
114 lines (103 loc) · 3.78 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
103
104
105
106
107
108
109
110
111
112
113
114
const httpClient = require('./httpClient');
const infrastructure = require('./infrastructure');
const package = require(__dirname + '/package.json');
const fs = require('fs');
let servicePackage = JSON.parse(fs.readFileSync(__dirname + '/package.json'));
const express = require('express');
const tracer = require('./tracer');
const governify = require('./index');
const logger = governify.getLogger();
const logClass = require('./logger');
let mainMiddleware = express.Router();
module.exports.mainMiddleware = mainMiddleware;
mainMiddleware.use(tracer.middlewareTracer)
//mainMiddleware.use(authMiddleware)
mainMiddleware.use('/commons/requestLogging', requestLoggingMiddleware);
mainMiddleware.use('/commons/infrastructure', infrastructureMiddleware);
mainMiddleware.use('/commons/logger', loggerMiddleware);
mainMiddleware.use('/commons', baseMiddleware);
function authMiddleware(req, res, next) {
let commonsConfig = governify.configurator.getConfig("commons")
if (commonsConfig.auth) {
} else {
next()
}
}
function baseMiddleware(req, res) {
if (req.url === '/') {
res.send({
title: 'Governify Commons',
version: package.version,
requestLogging: httpClient.getRequestLogging() ? 'true' : 'false',
serviceName: servicePackage.name,
serviceVersion: servicePackage.version,
});
return;
}
res.status(400).send('Method not implemented')
}
async function infrastructureMiddleware(req, res) {
if (req.url === '/') {
if (req.method === 'GET') {
try{
res.send(infrastructure.getServices())
} catch (err) {
logger.fatal('Internal error loading infrastructure');
res.status(500).send('Internal error loading infrastructure, please reload this service: ' + err.message);
}
return;
}
} else if (req.url.startsWith('/update')) {
if (req.method === 'POST') {
await infrastructure.loadServices().then(infrastructure => {
res.send('Updated infrastructure: ' + JSON.stringify(infrastructure))
}).catch(err => {
logger.fatal('Internal error reloading infrastructure');
res.status(500).send('Internal error reloading infrastructure, please reload this service: ' + err.message);
});
return;
}
}
res.status(400).send('Method not implemented')
}
async function requestLoggingMiddleware(req, res) {
if (req.method === 'POST') {
if (req.url === '/enable') {
httpClient.setRequestLogging(true);
res.send('Enabled');
}
else if (req.url === '/disable') {
httpClient.setRequestLogging(false);
res.send('Disabled');
}
else if (req.url === '/swap') {
httpClient.setRequestLogging(!httpClient.getRequestLogging());
res.send(httpClient.getRequestLogging() ? 'Enabled' : 'Disabled');
} else
res.status(400).send('Method not implemented');
}
else if (req.method === 'GET') {
res.send(httpClient.getRequestLogging() ? 'Enabled' : 'Disabled');
}
else {
res.status(400).send('Method not implemented');
}
}
async function loggerMiddleware(req, res) {
if (req.method === 'POST') {
if (req.url === '/config') {
logClass.setLogConfig(req.body);
res.send('Updated');
} else
res.status(400).send('Method not implemented');
}
else if (req.method === 'GET') {
if (req.url === '/config') {
res.send(logClass.getLogConfig());
} else
res.status(400).send('Method not implemented');
}
else {
res.status(400).send('Method not implemented');
}
}