Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
changed: OC: relinted all files to prepare for merge from enketo/enke…
Browse files Browse the repository at this point in the history
…to-express with code style changes
  • Loading branch information
MartijnR committed Feb 25, 2022
1 parent 8d702c8 commit 7744988
Show file tree
Hide file tree
Showing 86 changed files with 11,122 additions and 8,418 deletions.
440 changes: 281 additions & 159 deletions Gruntfile.js

Large diffs are not rendered by default.

33 changes: 18 additions & 15 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
#!/usr/bin/env node
/* eslint no-console: ["error", { allow: ["log", "error"] }] */

const cluster = require( 'cluster' );
const numCPUs = require( 'os' ).cpus().length;

if ( cluster.isMaster ) {
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
// Fork workers.
for ( let i = 0; i < numCPUs; i++ ) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on( 'exit', function( worker ) {
console.log( 'Worker ' + worker.process.pid + ' sadly passed away. It will be reincarnated.' );
cluster.on('exit', (worker) => {
console.log(
`Worker ${worker.process.pid} sadly passed away. It will be reincarnated.`
);
cluster.fork();
} );
});
} else {
const app = require( './config/express' );
const server = app.listen( app.get( 'port' ), () => {
const worker = ( cluster.worker ) ? cluster.worker.id : 'Master';
const msg = 'Worker ' + worker + ' ready for duty at port ' + server.address().port + '! (environment: ' + app.get( 'env' ) + ')';
console.log( msg );
} );
const app = require('./config/express');
const server = app.listen(app.get('port'), () => {
const worker = cluster.worker ? cluster.worker.id : 'Master';
const msg = `Worker ${worker} ready for duty at port ${
server.address().port
}! (environment: ${app.get('env')})`;
console.log(msg);
});
/**
* The goal of this timeout is to time out AFTER the client (browser request) times out.
* This avoids nasty issues where a proxied submission is still ongoing but Enketo
Expand All @@ -30,5 +33,5 @@ if ( cluster.isMaster ) {
*
* https://github.com/kobotoolbox/enketo-express/issues/564
*/
server.timeout = app.get( 'timeout' ) + 1000;
server.timeout = app.get('timeout') + 1000;
}
172 changes: 90 additions & 82 deletions app/controllers/account-controller.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,133 @@
const account = require( '../models/account-model' );
const auth = require( 'basic-auth' );
const express = require( 'express' );
const auth = require('basic-auth');
const express = require('express');
const account = require('../models/account-model');

const router = express.Router();
//const debug = require( 'debug' )( 'account-controller' );
// const debug = require( 'debug' )( 'account-controller' );

module.exports = app => {
app.use( '/accounts/api/v1', router );
module.exports = (app) => {
app.use('/accounts/api/v1', router);
};

router
.all( '*', ( req, res, next ) => {
.all('*', (req, res, next) => {
// set content-type to json to provide appropriate json Error responses
res.set( 'Content-Type', 'application/json' );
res.set('Content-Type', 'application/json');
next();
} )
.all( '*', authCheck )
.get( '/account', getExistingAccount )
.post( '/account', getNewOrExistingAccount )
.put( '/account', updateExistingAccount )
.delete( '/account', removeAccount )
.get( '/list', getList )
.post( '/list', getList )
.all( '*', ( req, res, next ) => {
const error = new Error( 'Not allowed' );
})
.all('*', authCheck)
.get('/account', getExistingAccount)
.post('/account', getNewOrExistingAccount)
.put('/account', updateExistingAccount)
.delete('/account', removeAccount)
.get('/list', getList)
.post('/list', getList)
.all('*', (req, res, next) => {
const error = new Error('Not allowed');
error.status = 405;
next( error );
} );
next(error);
});

function authCheck( req, res, next ) {
function authCheck(req, res, next) {
// check authentication and account
let error;

const creds = auth( req );
const key = ( creds ) ? creds.name : undefined;
const creds = auth(req);
const key = creds ? creds.name : undefined;

if ( !key || ( key !== req.app.get( 'account manager api key' ) ) ) {
error = new Error( 'Not Allowed. Invalid API key.' );
if (!key || key !== req.app.get('account manager api key')) {
error = new Error('Not Allowed. Invalid API key.');
error.status = 401;
res
.status( error.status )
.set( 'WWW-Authenticate', 'Basic realm="Enter valid API key as user name"' );
next( error );
res.status(error.status).set(
'WWW-Authenticate',
'Basic realm="Enter valid API key as user name"'
);
next(error);
} else {
next();
}
}

function getExistingAccount( req, res, next ) {
return account.get( {
linkedServer: req.query.server_url,
key: req.query.api_key
} )
.then( account => {
_render( 200, account, res );
} )
.catch( next );
function getExistingAccount(req, res, next) {
return account
.get({
linkedServer: req.query.server_url,
key: req.query.api_key,
})
.then((account) => {
_render(200, account, res);
})
.catch(next);
}

function getNewOrExistingAccount( req, res, next ) {
return account.set( {
linkedServer: req.body.server_url || req.query.server_url,
key: req.body.api_key || req.query.api_key
} )
.then( account => {
_render( account.status || 201, account, res );
} )
.catch( next );
function getNewOrExistingAccount(req, res, next) {
return account
.set({
linkedServer: req.body.server_url || req.query.server_url,
key: req.body.api_key || req.query.api_key,
})
.then((account) => {
_render(account.status || 201, account, res);
})
.catch(next);
}

function updateExistingAccount( req, res, next ) {
return account.update( {
linkedServer: req.body.server_url || req.query.server_url,
key: req.body.api_key || req.query.api_key
} )
.then( account => {
_render( account.status || 201, account, res );
} )
.catch( next );
function updateExistingAccount(req, res, next) {
return account
.update({
linkedServer: req.body.server_url || req.query.server_url,
key: req.body.api_key || req.query.api_key,
})
.then((account) => {
_render(account.status || 201, account, res);
})
.catch(next);
}

function removeAccount( req, res, next ) {
return account.remove( {
linkedServer: req.body.server_url || req.query.server_url,
key: req.body.api_key || req.query.api_key
} ).then( () => {
_render( 204, null, res );
} )
.catch( next );
function removeAccount(req, res, next) {
return account
.remove({
linkedServer: req.body.server_url || req.query.server_url,
key: req.body.api_key || req.query.api_key,
})
.then(() => {
_render(204, null, res);
})
.catch(next);
}

function getList( req, res, next ) {
return account.getList()
.then( list => {
_render( 200, list, res );
} )
.catch( next );
function getList(req, res, next) {
return account
.getList()
.then((list) => {
_render(200, list, res);
})
.catch(next);
}

function _render( status, body, res ) {
if ( status === 204 ) {
function _render(status, body, res) {
if (status === 204) {
// send 204 response without a body
res.status( status ).end();
res.status(status).end();
} else {
body = body || {};
if ( typeof body === 'string' ) {
if (typeof body === 'string') {
body = {
message: body
message: body,
};
} else if ( Array.isArray( body ) ) {
body = body.map( account => _renameProps( account ) );
} else if ( typeof body === 'object' ) {
body = _renameProps( body );
} else if (Array.isArray(body)) {
body = body.map((account) => _renameProps(account));
} else if (typeof body === 'object') {
body = _renameProps(body);
}
body.code = status;
res.status( status ).json( body );
res.status(status).json(body);
}
}

function _renameProps( account ) {
function _renameProps(account) {
return {
server_url: account.linkedServer,
api_key: account.key
api_key: account.key,
};
}
14 changes: 7 additions & 7 deletions app/controllers/api-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* @see {@link http://expressjs.com/en/4x/api.html#res|Express Response object documentation}
*/

const express = require( 'express' );
const express = require('express');

const router = express.Router();

module.exports = app => {
app.use( `${app.get( 'base path' )}/api`, router );
module.exports = (app) => {
app.use(`${app.get('base path')}/api`, router);
};

router
.get( '/', ( req, res ) => {
res.redirect( 'http://apidocs.enketo.org' );
} );
router.get('/', (req, res) => {
res.redirect('http://apidocs.enketo.org');
});
Loading

0 comments on commit 7744988

Please sign in to comment.