-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathbins.js
46 lines (36 loc) · 1.45 KB
/
bins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict'
var debug = require('debug-log')('mockbin')
var express = require('express')
var mw = require('../middleware')
var redis = require('redis')
var routes = require('./bins/')
var url = require('url')
module.exports = function bins (dsn_str) {
// parse redis dsn
var dsn = url.parse(dsn_str)
// connect to redis
this.client = redis.createClient(dsn.port, dsn.hostname, {
auth_pass: dsn.auth ? dsn.auth.split(':').pop() : false
})
this.client.on('error', function (err) {
debug('redis error:', err)
})
var router = express.Router()
var defaults = [mw.forwarded, mw.errorHandler, mw.bodyParser, null, mw.cors, mw.negotiateContent]
var endpoints = [
{ action: 'get', path: '/create', route: routes.form.bind(this) },
{ action: 'post', path: '/create', route: routes.create.bind(this) },
{ action: 'get', path: '/:uuid/view', route: routes.view.bind(this) },
{ action: 'get', path: '/:uuid/sample', route: routes.sample.bind(this) },
{ action: 'get', path: '/:uuid/log', route: routes.log.bind(this) },
{ action: 'all', path: '/:uuid/delay/:ms?', route: routes.delayed_run.bind(this) },
{ action: 'all', path: '/:uuid*', route: routes.run.bind(this) }
]
endpoints.forEach(function (endpoint) {
// add route to middleware
defaults.splice(3, 1, endpoint.route)
// assign router to action at path
router[endpoint.action].apply(router, [endpoint.path].concat(defaults))
})
return router
}