index.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # The server module...
  2. connect = require 'connect'
  3. Model = require './model'
  4. createDb = require './db'
  5. rest = require './rest'
  6. socketio = require './socketio'
  7. browserChannel = require './browserchannel'
  8. # Create an HTTP server and attach whatever frontends are specified in the options.
  9. #
  10. # The model will be created based on options if it is not specified.
  11. module.exports = create = (options, model = createModel(options)) ->
  12. attach(connect(), options, model)
  13. # Create an OT document model attached to a database.
  14. create.createModel = createModel = (options) ->
  15. dbOptions = options?.db
  16. db = createDb dbOptions
  17. new Model db, options
  18. # Attach the OT server frontends to the provided Node HTTP server. Use this if you
  19. # already have a http.Server or https.Server and want to make some URL paths do OT.
  20. #
  21. # The options object specifies options for everything. If settings are missing,
  22. # defaults will be provided.
  23. #
  24. # Set options.rest == null or options.socketio == null to turn off that frontend.
  25. create.attach = attach = (server, options, model = createModel(options)) ->
  26. options ?= {}
  27. options.staticpath ?= '/share'
  28. server.model = model
  29. server.on 'close', -> model.closeDb()
  30. server.use options.staticpath, connect.static("#{__dirname}/../../webclient") if options.staticpath != null
  31. createAgent = require('./useragent') model, options
  32. # The client frontend doesn't get access to the model at all, to make sure security stuff is
  33. # done properly.
  34. server.use rest(createAgent, options.rest) if options.rest != null
  35. # Socketio frontend is now disabled by default.
  36. socketio.attach(server, createAgent, options.socketio or {}) if options.socketio?
  37. if options.browserChannel != null
  38. options.browserChannel ?= {}
  39. #options.browserChannel.base ?= '/sjs'
  40. options.browserChannel.server = server
  41. server.use browserChannel(createAgent, options.browserChannel)
  42. server