-
Notifications
You must be signed in to change notification settings - Fork 152
/
subway.js
executable file
·96 lines (83 loc) · 3.5 KB
/
subway.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env node
/* The Subway IRC client
* Written By: David Petersen (thedjpetersen)
*
* This is a persistent web based IRC client that is targeted
* to those new to IRC.
*/
// Basic dependencies. We use connect/http to server our content.
// Bower manages our 3rd party dependcies allowing us to upgrade them
// easily(just update the bower.json file). The async module manages our flow -
// instead of nesting callbacks we try to follow the waterfall pattern
// to have at least the appearance of synchronous code. This aids our readabilty.
var express = require("express"),
http = require("http"),
bower = require("bower"),
async = require("async");
var server_settings = require("./settings/server");
//Get current environment
var env = process.env.IRC_ENV || "dev";
// These are our local lib files. The initialize function in our plugin module
// fetches the different plugins(github gists) we have and downloads them to
// the plugin_cache directory.
//
// The static module includes all of our grunt configuration.
// This takes care of any code preprocessing(grunt/stylus/Jade)
// as well code concatenation/minfication.
//
// The connection module handles any interaction between the client and the server
// all incoming IRC commands are hanndled here. It was also handle IRC logging
// and any other info that needs to be sent to the client(plugin info or settings)
var init_plugins = require("./lib/plugins").initialize,
static = require("./lib/static"),
connection = require("./lib/connection");
var cwd = __dirname;
// We use the async module waterfall to set through different steps to start up
// the subway client. Each one needs to happen in series
async.waterfall([
function(callback) {
// This method fetches different plugins from the github gists
// and saves them to the plugin_cache directory
console.log("Fetching Subway plugins...");
// TODO
// resolve Fatal error: getaddrinfo ENOTFOUND
// when we don't have active internet connection
init_plugins(callback);
},
function(callback) {
// We download all of our third party dependencies or upgrade any if
// if the configuration has changed. These are all client side depdencies
// like jQuery, Backbone or React.
console.log("Downloading dependencies...");
bower.commands.install().on("end", function(results){
callback(null, results);
});
},
function(results, callback) {
// We compile any preprocessed code that we need to like React components
// and Stylus stylesheets
console.log("Compiling static resources...");
static(function() {
callback(null);
});
}
], function(err, result) {
// All static content is placed in the tmp ./tmp directory
// we use this directory as the root of our server
var app = express()
.use(express.urlencoded())
.use(express.cookieParser(server_settings.cookie_secret || "subway_secret"))
.use(express.static(cwd + "/tmp"));
app.configure(function() {
app.set("views", __dirname + "/tmp");
});
app.engine("ejs", require("ejs").renderFile);
var http = require("http").Server(app);
var io = require("socket.io")(http);
// We can get the port of the server from the command line
// or from the server settings
http.listen(server_settings[env] ? server_settings[env].port : server_settings.dev.port);
// We pass off our socket.io listener to the connection module
// so it can handle incoming events and emit different actions
connection(io, app);
});