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 #27 from hoodiehq/proxy-to-pouchdb
Proxy to PouchDB instance
- Loading branch information
Showing
8 changed files
with
152 additions
and
99 deletions.
There are no files selected for viewing
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,3 @@ | ||
node_modules | ||
.db | ||
.nyc_output |
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,23 @@ | ||
var Hapi = require('hapi') | ||
var PouchDB = require('pouchdb') | ||
|
||
var server = new Hapi.Server() | ||
|
||
server.connection({ | ||
port: 3000 | ||
}) | ||
|
||
server.register({ | ||
register: require('./'), | ||
options: { | ||
PouchDB: PouchDB.defaults({ | ||
db: require('memdown') | ||
}) | ||
// couchdb: 'http://admin:secret@localhost:5984' | ||
} | ||
}, function (err) { | ||
if (err) return console.log(err) | ||
server.start(function () { | ||
console.log('Server running at:', server.info.uri) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,18 +1,76 @@ | ||
module.exports = hapiCouchDbStore | ||
|
||
hapiCouchDbStore.attributes = { | ||
name: 'couchdb-store' | ||
} | ||
|
||
var normaliseOptions = require('./lib/normalise-options') | ||
var url = require('url') | ||
|
||
var boom = require('boom') | ||
|
||
var validDbName = /^[a-zA-Z%]/ | ||
|
||
function hapiCouchDbStore (server, options, next) { | ||
try { | ||
options = normaliseOptions(options) | ||
} catch (error) { | ||
return next(error) | ||
var xapp = null | ||
if (!options.couchdb) { | ||
xapp = require('express-pouchdb')(options.PouchDB, { | ||
mode: 'minimumForPouchDB' | ||
}) | ||
xapp.disable('x-powered-by') | ||
} else { | ||
server.register(require('h2o2')) | ||
} | ||
server.register({ | ||
register: require('./lib/couchdb-proxy'), | ||
options: options | ||
}, next) | ||
|
||
if (options.hooks) { | ||
Object.keys(options.hooks).forEach(function (type) { | ||
server.ext({ | ||
type: type, | ||
method: options.hooks[type], | ||
options: { | ||
sandbox: 'plugin' | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
server.route({ | ||
method: 'GET', | ||
path: '/{path*}', | ||
handler: handler | ||
}) | ||
|
||
server.route({ | ||
method: ['PUT', 'POST', 'COPY', 'DELETE', 'OPTIONS'], | ||
path: '/{path*}', | ||
config: { | ||
payload: { | ||
output: 'stream', | ||
parse: false | ||
} | ||
}, | ||
handler: handler | ||
}) | ||
|
||
function handler (request, reply) { | ||
var rawUrl = request.raw.req.url | ||
.replace((server.realm.modifiers.route.prefix || '') + '/', '') | ||
var path = rawUrl.split('/') | ||
var dbname = path.shift() | ||
if (!dbname) return reply({couchdb: 'Welcome', ok: true}) | ||
if (!validDbName.test(dbname)) return reply(boom.notFound('database not found')) | ||
var newUrl = '/' + dbname + '/' + path.join('/') | ||
|
||
if (options.couchdb) { | ||
return reply.proxy({ | ||
passThrough: true, | ||
mapUri: function (request, callback) { | ||
callback(null, url.resolve(options.couchdb, newUrl)) | ||
} | ||
}) | ||
} | ||
request.raw.req.url = newUrl | ||
xapp(request.raw.req, request.raw.res) | ||
} | ||
|
||
next() | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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