Skip to content

Commit

Permalink
Cleanup and fixup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Mar 3, 2021
1 parent 838be54 commit 3c7e3ab
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 303 deletions.
225 changes: 0 additions & 225 deletions oada/services/http-handler/src/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,132 +322,6 @@ router.put(
})
);

/*
router.put('/*', async function ensureTypeTreeExists (req, res, next) {
if (req.headers['x-oada-bookmarks-type']) {
let rev = req.oadaGraph.rev
let tree = trees[req.headers['x-oada-bookmarks-type']] // Get the tree
//First, get the appropriate subTree rooted at the resource returned by
//graph lookup
let path = req.originalUrl
.split(req.oadaGraph.path_leftover)[0]
.replace(/^\/bookmarks/, '')
let subTree = await getFromStarredTree(path, tree)
// Find all resources in the subTree that haven't been created
let pieces = pointer.parse(req.oadaGraph.path_leftover)
let piecesPath = ''
let id = req.oadaGraph.resource_id.replace(/^\//, '')
let parentId = req.oadaGraph.resource_id.replace(/^\//, '')
let parentPath = ''
let parentRev = rev
return Bluebird.each(pieces, async function (piece, i) {
let nextPiece = pointer.has(subTree, '/' + piece)
? '/' + piece
: pointer.has(subTree, '/*')
? '/*'
: undefined
path += '/' + piece
piecesPath += nextPiece
parentPath += '/' + piece
if (nextPiece) {
subTree = pointer.get(subTree, nextPiece)
if (pointer.has(subTree, '/_type')) {
let contentType = pointer.get(subTree, '/_type')
id = 'resources/' + ksuid.randomSync()
let body = await replaceLinks(_.cloneDeep(subTree))
// Write new resource. This may potentially become an
// orphan if concurrent requests make links below
return requester
.send(
{
resource_id: '',
path_leftover: '/' + id,
user_id: req.user.user_id,
contentType,
body
},
config.get('kafka:topics:writeRequest')
)
.tap(resp => {
switch (resp.code) {
case 'success':
return
default:
return Promise.reject(new Error(resp.code))
}
})
.then(() => {
// Write link from parent. These writes reference the
// path from the known resource returned by graph lookup
let linkBody = { _id: id }
if (typeof body._rev === 'number')
linkBody._rev = body._rev
return requester
.send(
{
rev: parentRev,
resource_id: parentId,
path_leftover: parentPath,
user_id: req.user.user_id,
contentType,
body: linkBody
},
config.get('kafka:topics:writeRequest')
)
.tap(result => {
switch (result.code) {
case 'success':
return
default:
return Promise.reject(
new Error(result.code)
)
}
})
})
.then(result => {
// Write link from parent. These writes reference the
})
.then(() => {
parentId = id
parentPath = ''
parentRev = ''
return
})
}
}
return
})
.then(() => {
req.oadaGraph.resource_id = parentId
req.oadaGraph.path_leftover = parentPath
next()
})
.catch({ message: 'rev mismatch' }, err => {
trace('rev mismatch, rerunning', req.get('Authorization'))
return axios({
method: 'PUT',
url: 'http://http-handler' + req.originalUrl,
data: req.body,
headers: {
Authorization: 'Bearer def',
'x-oada-bookmarks-type': req.get(
'x-oada-bookmarks-type'
),
'Content-Type': req.get('content-type')
}
})
})
.catch(error => {
trace('ERROR', error)
next(error)
})
} else {
next()
}
})
*/
router.put('/*', async function putResource(req, res, next) {
trace(`Saving PUT body for request ${req.id}`);

Expand Down Expand Up @@ -605,105 +479,6 @@ router.delete('/*', function deleteResource(req, res, next) {
.catch(next);
});

let trees = {
'fields': {
fields: {
'_type': 'application/vnd.oada.fields.1+json',
'_rev': 0,
'fields-index': {
'*': {
'_type': 'application/vnd.oada.field.1+json',
'_rev': 0,
'fields-index': {
'*': {
_type: 'application/vnd.oada.field.1+json',
_rev: 0,
},
},
},
},
},
},
'as-harvested': {
harvest: {
'_type': 'application/vnd.oada.harvest.1+json',
'_rev': 0,
'as-harvested': {
'_type': 'application/vnd.oada.as-harvested.1+json',
'_rev': 0,
'yield-moisture-dataset': {
'_type':
'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
'_rev': 0,
'crop-index': {
'*': {
'_type':
'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
'_rev': 0,
'geohash-length-index': {
'*': {
'_type':
'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
'_rev': 0,
'geohash-index': {
'*': {
_type:
'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
},
},
},
},
},
},
},
},
},
},
'tiled-maps': {
harvest: {
'_type': 'application/vnd.oada.harvest.1+json',
'_rev': 0,
'tiled-maps': {
'_type': 'application/vnd.oada.tiled-maps.1+json',
'_rev': 0,
'dry-yield-map': {
'_type': 'application/vnd.oada.tiled-maps.dry-yield-map.1+json',
'_rev': 0,
'crop-index': {
'*': {
'_type': 'application/vnd.oada.tiled-maps.dry-yield-map.1+json',
'_rev': 0,
'geohash-length-index': {
'*': {
'_type':
'application/vnd.oada.tiled-maps.dry-yield-map.1+json',
'_rev': 0,
'geohash-index': {
'*': {
_type:
'application/vnd.oada.tiled-maps.dry-yield-map.1+json',
},
},
},
},
},
},
},
},
},
},
'services': {
services: {
_type: 'application/vnd.oada.services.1+json',
_rev: 0,
datasilo: {
_type: 'application/vnd.oada.services.1+json',
_rev: 0,
},
},
},
};

function replaceLinks(obj) {
let ret = Array.isArray(obj) ? [] : {};
if (!obj) return obj; // no defined objriptors for this level
Expand Down
10 changes: 5 additions & 5 deletions oada/services/http-handler/src/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var Promise = require('bluebird');
const Bluebird = require('bluebird');
const express = require('express');
const expressPromise = require('express-promise');
const ksuid = require('ksuid');
const cors = require('cors');
const wellKnownJson = require('well-known-json');
const oadaError = require('oada-error');
const OADAError = oadaError.OADAError;
const { OADAError } = oadaError;

const info = require('debug')('http-handler:server:info');
const warn = require('debug')('http-handler:server:warn');
Expand Down Expand Up @@ -35,7 +35,7 @@ app.get('*', (req, res, next) => {
app.get('/favicon.ico', (req, res) => res.end());

function start() {
return Promise.fromCallback(function (done) {
return Bluebird.fromCallback(function (done) {
info('Starting server...');
server.listen(config.get('server:port'), done);
}).tap(() => {
Expand All @@ -48,8 +48,8 @@ app.use(expressPromise());
// Log all requests before anything else gets them for debugging:
app.use(function (req, res, next) {
trace('Received request: ' + req.method + ' ' + req.url);
trace('req.headers = ', req.headers);
trace('req.body = ', req.body);
trace('req.headers = %O' + req.headers);
trace('req.body = %O', req.body);
next();
});
// Turn on CORS for all domains, allow the necessary headers
Expand Down
33 changes: 18 additions & 15 deletions oada/services/http-handler/src/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const Promise = require('bluebird');
const express = require('express');
const bodyParser = require('body-parser');
const debug = require('debug');
Expand All @@ -14,9 +13,9 @@ const { OADAError, middleware } = require('oada-error');

const config = require('./config');

var requester = require('./requester');
const requester = require('./requester');

var router = express.Router();
const router = express.Router();

const { users } = require('@oada/lib-arangodb');

Expand Down Expand Up @@ -67,14 +66,16 @@ function requestUserWrite(req, id) {
}

router.post('/', function (req, res) {
info('Users POST, body = ', req.body);
info('Users POST, body = %O', req.body);
// Note: if the username already exists, the ksuid() below will end up
// silently discarded and replaced in the response with the real one.
const newid = ksuid.randomSync().string; // generate a random string for ID
if (!req.id) req.id = ksuid.randomSync().string; // generate an ID for this particular request
// generate an ID for this particular request
if (!req.id) req.id = ksuid.randomSync().string;
return requestUserWrite(req, newid).then((resp) => {
// TODO: Better status code choices?
const id = resp && resp.user ? resp.user['_key'] : newid; // if db didn't send back a user, it was an update so use id from URL
// if db didn't send back a user, it was an update so use id from URL
const id = resp && resp.user ? resp.user['_key'] : newid;
// return res.redirect(201, req.baseUrl + '/' + id)
res.set('content-location', req.baseUrl + '/' + id);
return res.status(200).end();
Expand All @@ -83,22 +84,24 @@ router.post('/', function (req, res) {

// Update (merge) a user:
router.put('/:id', function (req, res) {
info('Users PUT(id: ', req.params.id, '), body = ', req.body);
if (!req.id) req.id = ksuid.randomSync().string; // generate an ID for this particular request
info('Users PUT(id: ' + req.params.id + '), body = %O', req.body);
// generate an ID for this particular request
if (!req.id) req.id = ksuid.randomSync().string;
return requestUserWrite(req, req.params.id).then((resp) => {
// TODO: Better status code choices?
const id = resp && resp.user ? resp.user['_key'] : req.params.id; // if db didn't send back a user, it was an update so use id from URL
// if db didn't send back a user, it was an update so use id from URL
const id = resp && resp.user ? resp.user['_key'] : req.params.id;
// return res.redirect(201, req.baseUrl + '/' + id)
res.set('content-location', req.baseUrl + '/' + id);
return res.status(200).end();
});
});

// Lookup a username, limited only to tokens and users with oada.admin.user scope
// Lookup a username, limited to tokens and users with oada.admin.user scope
router.get('/username-index/:uname', function (req, res) {
// Check token scope
trace(
'username-index: Checking token scope, req.authorization.scope = ',
'username-index: Checking token scope, req.authorization.scope = %s',
req.authorization ? req.authorization.scope : null
);
const havetokenscope = req.authorization.scope.find(
Expand All @@ -115,7 +118,7 @@ router.get('/username-index/:uname', function (req, res) {
}

// Check user's scope
trace('username-index: Checking user scope, req.user = ', req.user);
trace('username-index: Checking user scope, req.user = %O', req.user);
const haveuserscope =
Array.isArray(req.user.scope) &&
req.user.scope.find(
Expand Down Expand Up @@ -154,9 +157,9 @@ router.get('/username-index/:uname', function (req, res) {
})
.catch((e) => {
error(
'FAILED to find user in DB for username-index, username = ',
req.params.uname,
'. Error was: ',
'FAILED to find user in DB for username-index, username = ' +
req.params.uname +
'. Error was: %O',
e
);
res.status(500).send('Internal Error: ', e.toString());
Expand Down
Loading

0 comments on commit 3c7e3ab

Please sign in to comment.