-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
101 lines (90 loc) · 2.81 KB
/
app.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
97
98
99
100
101
var express = require('express'),
routes = require('./routes'),
http = require('http'),
path = require('path'),
fs = require('fs'),
util = require('util'),
cache = require('web-cache');
var app = express();
var packageJson = require(path.join(__dirname, "package.json"));
var irisVersion = packageJson.version;
var ONE_YEAR = 31557600000;
var ONE_DAY = 86400;
var RANDOM_NEIGHBORHOOD_NODES = 20;
var MAX_ITEMS = 300;
var defaultEnv = process.env.NODE_ENV || "development";
var optimist = require('optimist')
.usage("Run the datavis app\nUsage: $0 [options]")
.boolean(["cache"])
.default("port", 4747)
.default("env", defaultEnv)
.describe("cache", "Turn on caching")
.describe("env", "The run environment {development,production}")
.describe("port", "The network port")
.check(function (opts) {
if (opts.env && !validEnvironment(opts.env))
throw new Error("Invalid environment (--env): " + opts.env);
return true;
});
var argv = optimist.argv;
function validEnvironment(val) {
var Environments = "production development".split(" ");
for (var i in Environments)
if (Environments[i] == val) return true;
return false;
}
if (argv.help) {
console.log(optimist.help());
process.exit(0);
}
var PUBLIC_DIR = path.join(__dirname,
argv.env == 'production' ? 'build' : 'public'
);
fs.exists("logs", function (exists) {
if (!exists) fs.mkdir("logs");
});
fs.exists(PUBLIC_DIR, function (exists) {
if (!exists) {
console.error(
"Public directory '%s' does not exist!\n" +
"Make sure you ran 'make build'", PUBLIC_DIR);
process.exit(1);
}
});
// Configure the app
app.set('port', argv.port);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static(PUBLIC_DIR));
if (argv.env === 'production') {
var stream = fs.createWriteStream('logs/access.log', { flags: 'a' });
app.use(express.logger({ stream: stream }));
} else {
app.use(express.logger('dev'));
app.use(express.errorHandler());
}
app.use(express.compress());
if (argv.cache) {
console.log("Using web cache.");
app.use(cache.middleware({
path: '/',
expire: ONE_DAY
}));
}
app.use(express.json());
app.use(express.urlencoded());
app.use(app.router);
app.use(require("less-middleware")(PUBLIC_DIR, {
destDir: PUBLIC_DIR + "/css",
parser: { optimization: 2 },
preprocess: {
path: function (p) { return p.replace(/\/css\//, "/less/"); }
},
compiler: { compress: true },
}));
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
app.get('/', function (req, res) {
routes.index(req, res, { version: irisVersion });
});