summaryrefslogtreecommitdiff
path: root/src/app/services/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/services/index.js')
-rw-r--r--src/app/services/index.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/app/services/index.js b/src/app/services/index.js
new file mode 100644
index 0000000..ea0175d
--- /dev/null
+++ b/src/app/services/index.js
@@ -0,0 +1,70 @@
+/**
+ * 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,
+};