-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do auth server again, good practice (#39)
- Loading branch information
Showing
6 changed files
with
41 additions
and
61 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
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
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
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,33 +1,41 @@ | ||
var memdb = require('memdb') | ||
var http = require('http') | ||
var createAppa = require('appa') | ||
var township = require('township') | ||
var path = require('path') | ||
var Server = require('dat.land/server') | ||
var initDb = require('dat.land/server/database/init') | ||
var rimraf = require('rimraf') | ||
|
||
module.exports = createServer | ||
|
||
function createServer (port, cb) { | ||
var app = createAppa({log: {level: 'silent'}}) | ||
var db = memdb() | ||
var ship = township({secret: 'test'}, db) | ||
var config = { | ||
township: { | ||
secret: 'very secret code', | ||
db: path.join(__dirname, '..', 'test-township.db') | ||
}, | ||
db: { | ||
dialect: 'sqlite3', | ||
connection: { filename: path.join(__dirname, '..', 'test-sqlite.db') }, | ||
useNullAsDefault: true | ||
}, | ||
whitelist: false, | ||
port: port || 8888 | ||
} | ||
|
||
app.on('/register', function (req, res, ctx) { | ||
// appa provides `ctx` for us in the way we want out of the box | ||
ship.register(req, res, ctx, function (err, code, data) { | ||
if (err) return app.error(res, code, err.message) | ||
app.send(res, code, data) | ||
}) | ||
}) | ||
initDb(config.db, function (err, db) { | ||
if (err) return cb(err) | ||
|
||
app.on('/login', function (req, res, ctx) { | ||
// appa provides `ctx` for us in the way we want out of the box | ||
ship.login(req, res, ctx, function (err, code, data) { | ||
if (err) return app.error(res, code, err.message) | ||
app.send(res, code, data) | ||
const server = Server(config, db) | ||
server.listen(config.port, function () { | ||
console.log('listening', config.port) | ||
}) | ||
}) | ||
|
||
var server = http.createServer(app) | ||
server.listen(port, function () { | ||
cb(server) | ||
cb(null, server, close) | ||
|
||
function close (cb) { | ||
server.close(function () { | ||
rimraf.sync(config.township.db) | ||
rimraf.sync(config.db.connection.filename) | ||
process.exit() | ||
}) | ||
} | ||
}) | ||
} |