Skip to content

Instantly share code, notes, and snippets.

@joehand
Last active December 2, 2016 23:21
Show Gist options
  • Save joehand/bd77e75f97e92ba61e36fc8af16df2f8 to your computer and use it in GitHub Desktop.
Save joehand/bd77e75f97e92ba61e36fc8af16df2f8 to your computer and use it in GitHub Desktop.
township server
var http = require('http')
var memdb = require('memdb')
var appa = require('appa')
var township = require('township')
module.exports = function (cb) {
var creds = {
host: 'http://127.0.0.1',
port: 9966,
requiredScopes: {
useAPI: 'api:access'
},
email: {
fromEmail: 'hi@example.com',
postmarkAPIKey: 'your api key'
},
algorithm: 'ES512'
}
creds.publicKey = `-----BEGIN PUBLIC KEY-----
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQAvmJlA/DZl3SVKNl0OcyVbsMTOmTM
qU0Avhmcl6r8qxkBgjwArIxQr7G7v8m0LOeFIklnmF3sYAwA+8llHGFReV8ASW4w
5AUC8ngZThaH9xk6DQscaMmoEFPN5thWpNcwMgUFYovBtPLwtAZjYr9Se+UT/5k4
VltW7ko6SHbCfMgUUbU=
-----END PUBLIC KEY-----`
creds.privateKey = `-----BEGIN EC PRIVATE KEY-----
MIHbAgEBBEFmz7VMXRtCPTlBETqMMx/mokyA3xPXra2SkcA7Xh0N6sgne1rgSZNU
ngT6TR3XLfBOt5+p5GRW6p1FVtn+vtPyRKAHBgUrgQQAI6GBiQOBhgAEAL5iZQPw
2Zd0lSjZdDnMlW7DEzpkzKlNAL4ZnJeq/KsZAYI8AKyMUK+xu7/JtCznhSJJZ5hd
7GAMAPvJZRxhUXlfAEluMOQFAvJ4GU4Wh/cZOg0LHGjJqBBTzebYVqTXMDIFBWKL
wbTy8LQGY2K/UnvlE/+ZOFZbVu5KOkh2wnzIFFG1
-----END EC PRIVATE KEY-----`
var app = createApp(creds)
var server = http.createServer(app)
server.listen(creds.port, function (err) {
if (err) return cb(err)
cb(null, server, creds.host + ':' + creds.port)
})
}
function createApp (config) {
var app = appa({log: {level: 'silent'}})
var db = memdb()
app.db = db
var ship = township(config, db)
app.on('/register', function (req, res, ctx) {
ship.register(req, res, ctx, function (err, code, data) {
if (err) return app.error(res, code, err.message)
app.send(res, code, data)
})
})
app.on('/login', function (req, res, ctx) {
ship.login(req, res, ctx, function (err, code, data) {
if (err) return app.error(res, code, err.message)
app.send(res, code, data)
})
})
app.on('/updatepassword', function (req, res, ctx) {
ship.updatePassword(req, res, ctx, function (err, code, data) {
if (err) return app.error(res, code, err.message)
app.send(res, code, data)
})
})
app.ship = ship
return app
}
var json = {
'email': 'foo@example.com',
'password': 'foobar'
}
nets({url: address + '/register', method: 'POST', json: json}, function (err, resp, body) {
t.ifErr(err)
t.equals(resp.statusCode, 201, '201 created user')
t.ok(body.token, 'got token in response')
t.end()
})
@joehand
Copy link
Author

joehand commented Dec 2, 2016

Error from township tests:

/Users/joe/node_modules/township-client/node_modules/response/index.js:161
  return JSON.stringify(view)
              ^

TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at Object.typemap.json (/Users/joe/node_modules/township-client/node_modules/response/index.js:161:15)
    at Function._response [as json] (/Users/joe/node_modules/township-client/node_modules/response/index.js:168:51)
    at Function.send (/Users/joe/node_modules/township-client/node_modules/appa/send.js:9:19)
    at /Users/joe/node_modules/township-client/tests/server.js:53:11
    at /Users/joe/node_modules/township-client/node_modules/township/index.js:63:11
    at /Users/joe/node_modules/township-client/node_modules/township-access/index.js:20:12
    at Immediate.<anonymous> (/Users/joe/node_modules/township-client/node_modules/levelup/lib/levelup.js:258:9)
    at runCallback (timers.js:637:20)
    at tryOnImmediate (timers.js:610:5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment