forked from fontello/fontello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (108 loc) · 3.35 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// stdlib
var Fs = require('fs');
// nodeca
var NLib = require('nlib');
// 3rd-party
var StaticLulz = require('static-lulz');
var FsTools = NLib.Vendor.FsTools;
var Async = NLib.Vendor.Async;
var app = NLib.Application.create({
name: 'fontomas',
root: __dirname
});
nodeca.hooks.init.after('bundles', function (next) {
nodeca.runtime.assets_server = new StaticLulz();
FsTools.walk(nodeca.runtime.assets_path, function (file, stats, next_file) {
// Fill in Static lulz with files and data
Async.waterfall([
Async.apply(Fs.readFile, file),
function (buffer, callback) {
var rel_path = file.replace(nodeca.runtime.assets_path, '');
nodeca.runtime.assets_server.add(rel_path, buffer);
callback();
}
], next_file);
}, next);
});
function find_view(scope, api_path) {
var parts = api_path.split('.');
while (scope && parts.length) {
scope = scope[parts.shift()];
}
return scope;
}
nodeca.hooks.init.after('init-complete', function (next) {
var connect = require('connect'), fontomas = connect(), default_host;
default_host = nodeca.config.listen.host;
if (nodeca.config.listen.port && 80 !== +nodeca.config.listen.port) {
default_host += ':' + nodeca.config.listen.port;
}
fontomas.use("/static/", nodeca.runtime.assets_server.middleware);
fontomas.use(function (req, res) {
var host = req.headers.host || default_host, env, match;
// remove port part if it's 80
if (host && '80' === host.split(':')[1]) {
host = host.split(':')[0];
}
match = nodeca.runtime.router.match('//' + host + req.url.split('?').shift());
if (!match) {
// TODO: Fix not found handling
res.statusCode = 404;
res.end('Not found');
return;
}
env = {
err: {code: null, message: null},
data: null,
view: match.meta.name
};
nodeca.filters.run(match.meta.name, match.params, match.meta.func, function (err) {
var data, view;
if (err && err.redirect) {
res.statusCode = err.redirect[0];
res.setHeader('Location', err.redirect[1]);
res.end();
return;
} else if (err) {
res.statusCode = 500;
res.end(err.toString());
return;
}
view = find_view(nodeca.runtime.views, env.view);
data = env.data || {};
if (!view) {
// TODO: Fix view not found handling
res.statusCode = 500;
res.end('View ' + env.view + ' not found');
return;
}
res.end(view.en(data));
}, env); // nodeca.filters.run
});
nodeca.runtime.fontomas_http = fontomas;
next();
});
// start application
app.run(function (err) {
var server, host, port, attempt = 0;
if (err) {
console.error(err);
process.exit(1);
}
host = nodeca.config.listen.host || 'localhost';
port = nodeca.config.listen.port || 3000;
server = require('http').createServer(nodeca.runtime.fontomas_http);
server.on('error', function (err) {
if (err.code == 'EADDRINUSE') {
if (3 <= attempt) {
console.error("Maximum amount of attempts reached. Can't continue.");
process.exit(1);
return;
}
attempt += 1;
console.log('Address <' + host + ':' + port + '> in use, retrying... ');
setTimeout(function () { server.listen(port, host); }, 1000);
}
});
server.listen(port, host);
});