/** * Configure services and mount them. * @module app/services/index */ import AuthenticationService from "app/services/authentication"; import ShoeService from "app/services/shoe"; import configureDatabase from "app/db/configure"; /** * A cache of registered services * @type {Object} */ const services = {}; /** * Function to get a service by resource name * @param {String} resourceName name of the service to fetch */ function get(resourceName) { return services[resourceName]; } /** * List services * @return {object} the services object */ function list() { return Object.entries(services); } /** * Configure the API endpoints * @param {Express} app Express application * @param {Knex} knex Knex instance (optional) */ async function configure(app, knex) { const { bookshelf } = configureDatabase(knex); /** * Connect a service * @param {Service} RoutableService the service to connect * @return {express.Router} the service's router */ async function connect(RoutableService) { const service = await RoutableService(bookshelf); services[service.resource] = service; return service.router; } app.use("/api/v1/auth", await connect(AuthenticationService)); app.use("/api/v1/shoe", await connect(ShoeService)); app.get("/api/v1/", describeApplication); // app.get("/", describeApplication); } /** * Canary middleware to indicate that the API is working */ function describeApplication(request, response) { response.json({ application: "Shoebox" }); } export default { configure, get, list, };