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

Commit

Permalink
endpoint to import documents into the database
Browse files Browse the repository at this point in the history
  • Loading branch information
John O'Malley committed Oct 22, 2016
1 parent 2631612 commit 7337a80
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jspm_packages
# Optional REPL history
.node_repl_history

/public
.idea
.DS_Store

Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ app.get('/health', (req, res) => res.json({ ok: true }))

app.use('/services', require('./services/api'))

// app.use('/import', require('./services/import'))
app.use('/import', require('./services/import'))

app.use('/font-awesome', express.static('node_modules/font-awesome'))

Expand Down
1 change: 1 addition & 0 deletions services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const router = express.Router()

// example service
const timeFormat = 'YYYY-MMM-DD hh:mm:ss A Z'

router.get('/now', (req, res, next) => {
agent.get('http://www.timeapi.org/utc/now')
.accept('json')
Expand Down
6 changes: 6 additions & 0 deletions services/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
couchUrl: process.env.COUCH_URL || 'http://localhost:5984',
searchHost: 'search-ocelot-6tqsxtl7pweckdfvle6ty47yiu.us-east-1.es.amazonaws.com',
searchUserAccessKeyId: process.env.SEARCH_USER_ACCESS_KEY_ID,
searchUserSecretAccessKey: process.env.SEARCH_USER_SECRET_ACCESS_KEY
}
8 changes: 8 additions & 0 deletions services/docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"name": "alice"
},
{
"name": "bob"
}
]
14 changes: 14 additions & 0 deletions services/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const agent = require('../src/agent')
const { couchUrl } = require('./config')
const express = require('express')

const router = express.Router()

router.post('/', (req, res, next) => {
agent.post(`${couchUrl}/foobar/_bulk_docs`)
.send({ docs: require('./docs.json') })
.then(({ body }) => res.json(body))
.catch(next)
})

module.exports = router
41 changes: 41 additions & 0 deletions services/search/searchHttps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const _ = require('underscore')
const aws4 = require('aws4')
const https = require('https')

module.exports = ({ host, credentials }) => {
return ({ path, method, body, headers = {}, json = true }) => {
const options = {
host,
path,
method: method || (body ? 'POST' : 'GET'),
headers
}
if (_(body).isObject()) {
options.body = JSON.stringify(body)
headers['Content-Type'] = 'application/json; charset=utf-8'
} else if (_(body).isString()) {
options.body = body
}

const signed = aws4.sign(options, credentials)

return new Promise((resolve, reject) => {
const request = https.request(signed, (res) => {
let responseBody = ''
res.on('data', chunk => responseBody += chunk)

const result = () => json && responseBody ? JSON.parse(responseBody) : responseBody

res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(result())
} else {
reject({ status: res.statusCode, body: responseBody })
}
})
})
request.on('error', reject)
request.end(signed.body || '')
})
}
}

0 comments on commit 7337a80

Please sign in to comment.