1 | var path = require('path');
|
2 | var fs = require('fs');
|
3 | var util = require("./util");
|
4 | var hosts = require("./hosts");
|
5 | var Gitana = require('gitana');
|
6 |
|
7 | var stores = require("../middleware/stores/stores");
|
8 | var driver = require("../middleware/driver/driver");
|
9 | var driverConfig = require("../middleware/driver-config/driver-config");
|
10 |
|
11 | exports = module.exports;
|
12 |
|
13 | exports.bindGitana = function(socket, callback)
|
14 | {
|
15 |
|
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 |
|
25 | socket.host = host;
|
26 | socket.domainHost = host;
|
27 | socket.virtualHost = host;
|
28 |
|
29 |
|
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 |
|