Files: 7e467f6efa8f6320f4c92bbfbe6e905689e0b8eb / listen.js
1048 bytesRaw
1 | const pull = require('pull-stream') |
2 | const defined = require('defined') |
3 | const http = require('http') |
4 | const Stack = require('stack') |
5 | |
6 | const Handler = require('./http/server') |
7 | const Server = require('./server') |
8 | |
9 | const DEFAULT_PORT = 5000 |
10 | |
11 | module.exports = listen |
12 | |
13 | function listen (api, config, options) { |
14 | options = defined(options, {}) |
15 | |
16 | const port = defined(options.port, DEFAULT_PORT) |
17 | const createHttpServer = defined(options.createHttpServer, defaultCreateHttpServer) |
18 | const onListen = options.onListen |
19 | |
20 | const server = Server(api, config, options) |
21 | |
22 | const handlers = [ |
23 | (req, res, next) => { |
24 | server.authenticate(req, (err, id) => { |
25 | if (err) console.error(err) // should we handle this error? |
26 | req.id = id; next() |
27 | }) |
28 | } |
29 | ].concat(server.handlers).concat([ |
30 | Handler(server, options) |
31 | ]) |
32 | |
33 | const httpServer = createHttpServer(handlers, config) |
34 | |
35 | return httpServer.listen(port, onListen) |
36 | } |
37 | |
38 | function defaultCreateHttpServer (handlers, config) { |
39 | return http.createServer(Stack.apply(null, handlers)) |
40 | } |
41 |
Built with git-ssb-web