UNPKG

1.51 kBJavaScriptView Raw
1var path = require('path');
2var fs = require('fs');
3var util = require("./util");
4var hosts = require("./hosts");
5var Gitana = require('gitana');
6
7var stores = require("../middleware/stores/stores");
8var driver = require("../middleware/driver/driver");
9var driverConfig = require("../middleware/driver-config/driver-config");
10
11exports = module.exports;
12
13exports.bindGitana = function(socket, callback)
14{
15 // determine host of socket
16 var host = hosts.determineHostForSocket(socket);
17 if (!host)
18 {
19 return callback({
20 "message": "Unable to determine host from socket headers"
21 });
22 }
23
24 // retain host on the socket instance
25 socket.host = host;
26 socket.domainHost = host;
27 socket.virtualHost = host;
28
29 // find the stores for this host
30 stores.produce(host, function(err, stores) {
31
32 socket.stores = stores;
33
34 var rootStore = stores.root;
35
36 driverConfig.resolveConfig(socket, rootStore, function(err) {
37
38 if (err) {
39 return callback(err);
40 }
41
42 if (!socket.gitanaConfig)
43 {
44 return callback({
45 "message": "Socket is missing gitanaConfig"
46 });
47 }
48
49 driver.doConnect(socket, socket.gitanaConfig, function(err) {
50
51 if (err) {
52 return callback(err);
53 }
54
55 socket.gitana = this;
56
57 callback();
58 });
59
60 });
61 });
62};
63