Log all requests, using the bunyan format (JSON with certain fields). This

is most easily consumed with the bunyan formatter; see README.md for more
details.
This commit is contained in:
Marsell Kukuljevic 2021-04-10 23:06:24 +02:00
parent 8672439358
commit 3c38a932ed
2 changed files with 13 additions and 6 deletions

View File

@ -23,6 +23,12 @@ The SSH key used must be the correct format, e.g. generated with:
node bin/server.js config/prod.json
The server generates a lot of JSON data about every request. This is easier
for a human to handle if they have bunyan installed ("npm install -g bunyan"),
and instead:
node bin/server.js config/prod.json | bunyan
# Endpoints
## GET /*

View File

@ -6,6 +6,7 @@ const mod_cueball = require('cueball');
const mod_crypto = require('crypto');
const mod_fs = require('fs');
const mod_sdcauth = require('smartdc-auth');
const mod_bunyan = require('bunyan');
// Globals that are assigned to by main(). They are used by proxy() and login().
@ -25,8 +26,6 @@ const STATIC_RE = new RegExp('^/');
// from cloudapi to our client caller. Effectively this function is a proxy
// that solely signs the request as it passes through.
function proxy(req, res, cb) {
console.log('### proxy', req.url)
// return data from cloudapi to the client caller
function proxyReturn(err, _, res2, data) {
if (err && !res2) {
@ -71,8 +70,6 @@ function proxy(req, res, cb) {
headers: headers
};
console.dir(opts);
// make the call to cloudapi
switch (req.method) {
case 'GET': CLOUDAPI.get(opts, proxyReturn); break;
@ -89,8 +86,6 @@ console.dir(opts);
// secure token. Once the user successfully logs in, the token is returned
// through an SSO redirect to token() below.
function login(req, res, cb) {
console.log('### login');
const query = {
permissions: '{"cloudapi":["/my/*"]}',
returnto: CONFIG.urls.local,
@ -159,9 +154,15 @@ function main() {
};
const server = mod_restify.createServer(options);
server.use(mod_restify.requestLogger());
server.use(mod_restify.authorizationParser());
server.use(mod_restify.bodyReader());
// log requests
server.on('after', mod_restify.auditLogger({
log: mod_bunyan.createLogger({ name: 'proxy' })
}));
// login path is /api/login
server.get(LOGIN_PATH, login);