git ssb

1+

dinoworm ๐Ÿ› / vas



Tree: 7e467f6efa8f6320f4c92bbfbe6e905689e0b8eb

Files: 7e467f6efa8f6320f4c92bbfbe6e905689e0b8eb / listen.js

1048 bytesRaw
1const pull = require('pull-stream')
2const defined = require('defined')
3const http = require('http')
4const Stack = require('stack')
5
6const Handler = require('./http/server')
7const Server = require('./server')
8
9const DEFAULT_PORT = 5000
10
11module.exports = listen
12
13function 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
38function defaultCreateHttpServer (handlers, config) {
39 return http.createServer(Stack.apply(null, handlers))
40}
41

Built with git-ssb-web