Skip to content

Commit

Permalink
Encapsulate config options of REST API server as own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
epatters committed Aug 3, 2018
1 parent 0b34019 commit 6f405a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
11 changes: 11 additions & 0 deletions api/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// General
export const port = process.env.PORT || 3000;

// CouchDB/Cloudant
export const couchUrl = process.env.COUCH_URL || 'http://localhost:5986';

export const dbName = 'data-science-ontology';
export const dbUrl = `${couchUrl}/${dbName}`;

export const appDbName = 'data-science-ontology-webapp';
export const appDbUrl = `${couchUrl}/${appDbName}`;
30 changes: 14 additions & 16 deletions api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import express from "express";

import * as methods from "./methods";

// Environment variables.
const PORT = process.env.PORT || 3000;
import * as Config from "./config";
import * as Methods from "./methods";

// Helper functions.
const sendJSON = (res: express.Response, data: object | string) => {
Expand All @@ -20,60 +18,60 @@ app.get('/', (req, res) => res.send('Data Science Ontology API'));

app.get('/concept/_random',
(req, res) => {
methods.randomConcept().then(body => sendJSON(res, body));
Methods.randomConcept().then(body => sendJSON(res, body));
});

app.get('/annotation/_random',
(req, res) => {
methods.randomAnnotation().then(body => sendJSON(res, body));
Methods.randomAnnotation().then(body => sendJSON(res, body));
});

app.get('/concept/:id',
(req, res) => {
let { id } = req.params;
methods.getConcept(id).then(body => sendJSON(res, body));
Methods.getConcept(id).then(body => sendJSON(res, body));
});

app.get('/annotation/:lang/:pkg/:id',
(req, res) => {
let { lang, pkg, id } = req.params;
methods.getAnnotation(lang, pkg, id).then(body => sendJSON(res, body));
Methods.getAnnotation(lang, pkg, id).then(body => sendJSON(res, body));
});

app.get('/concepts',
(req, res) => {
methods.listConcepts().then(body => sendJSON(res, body));
Methods.listConcepts().then(body => sendJSON(res, body));
});

app.get('/annotations',
(req, res) => {
methods.listAnnotations().then(body => sendJSON(res, body));
Methods.listAnnotations().then(body => sendJSON(res, body));
});

app.get('/counts',
(req, res) => {
methods.counts().then(body => sendJSON(res, body));
Methods.counts().then(body => sendJSON(res, body));
});

app.get('/search/concept/:text',
(req, res) => {
let { text } = req.params;
methods.searchConcepts(text).then(body => sendJSON(res, body));
Methods.searchConcepts(text).then(body => sendJSON(res, body));
});

app.get('/search/annotation/:text',
(req, res) => {
let { text } = req.params;
methods.searchAnnotations(text).then(body => sendJSON(res, body));
Methods.searchAnnotations(text).then(body => sendJSON(res, body));
});

app.get('/_cache/annotation/:lang/:pkg/:id',
(req, res) => {
let { lang, pkg, id } = req.params;
const _id = `annotation/${lang}/${pkg}/${id}`;
methods.getCache(_id).then(body => sendJSON(res, body));
Methods.getCache(_id).then(body => sendJSON(res, body));
});

// Start the app!
app.listen(PORT, () => console.log(
`Data Science Ontology proxy server listening on port ${PORT}`));
app.listen(Config.port, () => console.log(
`Data Science Ontology proxy server listening on port ${Config.port}`));
17 changes: 6 additions & 11 deletions api/src/methods.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import * as _ from "lodash";
import request from "request-promise-native";

// Constants and environment variables.
const COUCH_URL = process.env.COUCH_URL || 'http://localhost:5986';
const ONTOLOGY_DB = 'data-science-ontology';
const ONTOLOGY_DB_URL = `${COUCH_URL}/${ONTOLOGY_DB}`;
const WEBAPP_DB = 'data-science-ontology-webapp';
const WEBAPP_DB_URL = `${COUCH_URL}/${WEBAPP_DB}`;
import * as Config from "./config";

// Generic CouchDB/Cloudant

function get(_id: string) {
return request(`${ONTOLOGY_DB_URL}/${encodeURIComponent(_id)}`);
return request(`${Config.dbUrl}/${encodeURIComponent(_id)}`);
}

function view(ddoc: string, view: string, params?: object) {
return request({
url: `${ONTOLOGY_DB_URL}/_design/${ddoc}/_view/${view}`,
url: `${Config.dbUrl}/_design/${ddoc}/_view/${view}`,
qs: params,
});
}

function find(options: object) {
return request({
url: `${ONTOLOGY_DB_URL}/_find`,
url: `${Config.dbUrl}/_find`,
method: 'POST',
json: true,
body: options,
Expand All @@ -32,7 +27,7 @@ function find(options: object) {

function search(ddoc: string, index: string, options: object) {
return request({
url: `${ONTOLOGY_DB_URL}/_design/${ddoc}/_search/${index}`,
url: `${Config.dbUrl}/_design/${ddoc}/_search/${index}`,
method: 'POST',
json: true,
body: options,
Expand All @@ -53,7 +48,7 @@ export function getAnnotation(lang: string, pkg: string, id: string) {
}

export function getCache(_id: string) {
return request(`${WEBAPP_DB_URL}/${encodeURIComponent(_id)}`);
return request(`${Config.appDbUrl}/${encodeURIComponent(_id)}`);
}

export function listConcepts() {
Expand Down

0 comments on commit 6f405a7

Please sign in to comment.