-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from akhilesh26/api2
feat: Lookup requests for BB API
- Loading branch information
Showing
39 changed files
with
4,116 additions
and
445 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright (C) 2014-2015 Ben Ockmore | ||
* 2015-2017 Sean Burke | ||
* 2015 Leo Verto | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
/* eslint global-require: 'warn' */ | ||
|
||
|
||
import BookBrainzData from 'bookbrainz-data'; | ||
import Debug from 'debug'; | ||
import Promise from 'bluebird'; | ||
import {get as _get} from 'lodash'; | ||
import {allowOnlyGetMethod} from './helpers/utils'; | ||
import appCleanup from '../common/helpers/appCleanup'; | ||
import bodyParser from 'body-parser'; | ||
import compression from 'compression'; | ||
import config from '../common/helpers/config'; | ||
import express from 'express'; | ||
import logger from 'morgan'; | ||
import path from 'path'; | ||
import redis from 'connect-redis'; | ||
import routes from './routes'; | ||
import session from 'express-session'; | ||
import swaggerRoute from './swagger'; | ||
|
||
|
||
Promise.config({ | ||
longStackTraces: true, | ||
warnings: true | ||
}); | ||
|
||
// Initialize application | ||
const app = express(); | ||
app.locals.orm = BookBrainzData(config.database); | ||
|
||
|
||
app.set('trust proxy', config.site.proxyTrust); | ||
|
||
if (app.get('env') !== 'testing') { | ||
app.use(logger('dev')); | ||
} | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ | ||
extended: false | ||
})); | ||
app.use(compression()); | ||
|
||
|
||
const RedisStore = redis(session); | ||
app.use(session({ | ||
cookie: { | ||
maxAge: _get(config, 'session.maxAge', 2592000000), | ||
secure: _get(config, 'session.secure', false) | ||
}, | ||
resave: false, | ||
saveUninitialized: false, | ||
secret: config.session.secret, | ||
store: new RedisStore({ | ||
host: _get(config, 'session.redis.host', 'localhost'), | ||
port: _get(config, 'session.redis.port', 6379) | ||
}) | ||
})); | ||
|
||
|
||
// Set up routes | ||
routes(app); | ||
|
||
// use swagger-Ui-express for your app documentation endpoint | ||
app.use('/api-docs', swaggerRoute); | ||
|
||
// Allow only get requests for now throw error for any other type of requests | ||
app.all('/*', allowOnlyGetMethod); | ||
|
||
// Catch 404 and forward to error handler | ||
app.use((req, res, next) => { | ||
res.status(404).send({message: `Incorrect endpoint ${req.path}`}); | ||
}); | ||
|
||
|
||
const debug = Debug('bbapi'); | ||
|
||
const DEFAULT_API_PORT = 9098; | ||
app.set('port', process.env.PORT || DEFAULT_API_PORT); // eslint-disable-line no-process-env,max-len | ||
|
||
const server = app.listen(app.get('port'), () => { | ||
debug(`Express server listening on port ${server.address().port}`); | ||
}); | ||
|
||
/* eslint-disable no-console */ | ||
function cleanupFunction() { | ||
return new Promise((resolve, reject) => { | ||
console.log('Cleaning up before closing'); | ||
server.close((err) => { | ||
if (err) { | ||
console.log('Error while closing server connections'); | ||
reject(err); | ||
} | ||
else { | ||
console.log('Closed all server connections. Bye bye!'); | ||
resolve(); | ||
} | ||
}); | ||
// force-kill after X milliseconds. | ||
if (config.site.forceExitAfterMs) { | ||
setTimeout(() => { | ||
reject(new Error(`Cleanup function timed out after ${config.site.forceExitAfterMs} ms`)); | ||
}, config.site.forceExitAfterMs); | ||
} | ||
}); | ||
} | ||
/* eslint-enable no-console */ | ||
|
||
// Run cleanup function | ||
appCleanup(cleanupFunction); | ||
|
||
export default server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2019 Akhilesh Kumar | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
// @flow | ||
|
||
|
||
import * as commonUtils from '../../common/helpers/utils'; | ||
|
||
/** | ||
* This is a middleware function to load entity detail according to given relations | ||
* | ||
* @param {string} modelName - Name of entity model | ||
* @param {string[]} relations - List of entity relations for fetching the related detail | ||
* @param {string} errMessage - Error message, if any error will occur | ||
* @returns {object} an object containing the error message if any error will occur. | ||
* If entity is found succesfully in the database this function set the entity data | ||
* at res.locals.entity and return to next function. | ||
* @example | ||
* const errorMessage = 'Edition not found''; | ||
* makeEntityLoader('Edition', ['defaultAlias.language', 'editionStatus'], errMessage); | ||
* | ||
* @description | ||
* First, check the BBID is valid or not. | ||
* If BBID is valid then extract the entity data from database by using BBID and relations of | ||
* that entity. If entity is found succesfully then set that entity data to the res.locals.entity | ||
* otherwise return an object {message: errMessage} as response with status code 404. | ||
* If the BBID is not valid then return a status code 406 and an object {message: 'BBID is not valid uuid'}. | ||
*/ | ||
|
||
|
||
export function makeEntityLoader(modelName, relations, errMessage) { | ||
return async (req, res, next) => { | ||
const {orm} = req.app.locals; | ||
if (commonUtils.isValidBBID(req.params.bbid)) { | ||
try { | ||
const entityData = await orm.func.entity.getEntity(orm, modelName, req.params.bbid, relations); | ||
res.locals.entity = entityData; | ||
return next(); | ||
} | ||
catch (err) { | ||
return res.status(404).send({message: errMessage}); | ||
} | ||
} | ||
return res.status(406).send({message: 'BBID is not valid uuid'}); | ||
}; | ||
} |
Oops, something went wrong.