forked from coderaiser/cloudcmd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudcmd.js
305 lines (242 loc) · 9.32 KB
/
cloudcmd.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
(function() {
'use strict';
var DIR = __dirname + '/',
main = require(DIR + 'lib/server/main'),
LIBDIR = main.LIBDIR,
SRVDIR = main.SRVDIR,
CLIENTDIR = LIBDIR + 'client',
HTMLDIR = main.HTMLDIR,
JSONDIR = main.JSONDIR,
path = main.path,
fs = main.fs,
files = main.files,
CloudFunc = main.cloudfunc,
AppCache = main.appcache,
Util = main.util,
update = main.update,
server = main.librequire('server'),
Minify = main.minify,
Config = main.config,
INDEX_PATH = HTMLDIR + 'index.html',
CONFIG_PATH = JSONDIR + 'config.json',
KEY = DIR + 'ssl/ssl.key',
CERT = DIR + 'ssl/ssl.crt',
FILE_TMPL = HTMLDIR + 'file.html',
PATH_TMPL = HTMLDIR + 'path.html',
LINK_TMPL = HTMLDIR + 'link.html',
FileTemplate, PathTemplate, LinkTemplate,
FS = CloudFunc.FS;
/* reinit main dir os if we on Win32 should be backslashes */
DIR = main.DIR;
readConfig(init);
/**
* additional processing of index file
*/
function indexProcessing(pData) {
var lPath, lReplace, lKeysPanel,
lData = pData.data,
lAdditional = pData.additional;
if (!Config.appCache)
lData = Util.removeStr(lData, [
/* min */
' manifest=/cloudcmd.appcache',
/* normal */
' manifest="/cloudcmd.appcache"'
]);
if (!Config.showKeysPanel) {
lKeysPanel = '<div class="keyspanel';
lData = lData.replace(lKeysPanel + '"', lKeysPanel +' hidden"');
}
lData = Util.render(lData, {
title : CloudFunc.getTitle(),
fm : lAdditional
});
return lData;
}
/**
* init and process of appcache if it allowed in config
*/
function appCacheProcessing() {
var lFONT_REMOTE = '//themes.googleusercontent.com/static/fonts/droidsansmono/v4/ns-m2xQYezAtqh7ai59hJUYuTAAIFFn5GTWtryCmBQ4.woff',
lFONT_LOCAL = './font/DroidSansMono.woff',
lJQUERY_REMOTE = '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js',
lJQUERY_LOCAL = './lib/client/jquery.js',
lFiles = [{}, {}];
lFiles[0][lFONT_REMOTE] = lFONT_LOCAL;
lFiles[1][lJQUERY_REMOTE] = lJQUERY_LOCAL;
AppCache.addFiles(lFiles);
AppCache.createManifest();
}
/**
* rest interface
* @pParams pConnectionData {request, responce}
*/
function rest(pConnectionData) {
return Util.exec(main.rest, pConnectionData);
}
function init() {
var lServerDir, lArg, lParams, lFiles;
if (update)
update.get();
/* Determining server.js directory
* and chang current process directory
* (usually /) to it.
* argv[1] - is always script name
*/
lServerDir = path.dirname(process.argv[1]) + '/';
if (DIR !== lServerDir) {
Util.log('current dir: ' + DIR);
process.chdir(lServerDir);
}
Util.log('server dir: ' + lServerDir);
/* if command line parameter testing resolved
* setting config to testing, so server
* not created, just init and
* all logs writed to screen */
lArg = process.argv;
lArg = lArg[lArg.length - 1];
if ( lArg === 'test' || lArg === 'test\r') {
Util.log(process.argv);
Config.server = false;
}
if (Config.logs) {
Util.log('log param setted up in config.json\n' +
'from now all logs will be writed to log.txt');
writeLogsToFile();
}
lParams = {
appcache : appCacheProcessing,
rest : rest,
route : route
},
lFiles = [FILE_TMPL, PATH_TMPL, LINK_TMPL];
if (Config.ssl)
lFiles.push(KEY, CERT);
files.read(lFiles, 'utf-8', function(pErrors, pFiles) {
if (pErrors)
Util.log(pErrors);
else {
FileTemplate = pFiles[FILE_TMPL];
PathTemplate = pFiles[PATH_TMPL];
LinkTemplate = pFiles[LINK_TMPL];
if (Config.ssl)
lParams.ssl = {
key : pFiles[KEY],
cert : pFiles[CERT]
};
server.start(lParams);
}
});
}
function readConfig(pCallBack) {
fs.readFile(CONFIG_PATH, function(pError, pData) {
var msg, status, str, readed;
if (pError)
status = 'error';
else {
status = 'ok';
str = pData.toString(),
readed = Util.parseJSON(str);
main.config = Config = readed;
}
msg = CloudFunc.formatMsg('read', 'config', status);
Util.log(msg);
Util.exec(pCallBack);
});
}
/**
* routing of server queries
*/
function route(pParams) {
var lRet = main.checkParams(pParams);
if (lRet) {
var p = pParams;
if ( Util.strCmp(p.name, ['/auth', '/auth/github']) ) {
Util.log('* Routing' +
'-> ' + p.name);
pParams.name = main.HTMLDIR + p.name + '.html';
lRet = main.sendFile( pParams );
}
else if ( Util.isContainStrAtBegin(p.name, FS) || Util.strCmp( p.name, '/') )
lRet = sendContent( pParams );
else
lRet = false;
}
return lRet;
}
function sendContent(pParams) {
var p, lRet = main.checkParams(pParams);
if (lRet) {
p = pParams;
p.name = Util.removeStrOneTime(p.name, CloudFunc.FS) || main.SLASH;
fs.stat(p.name, function(pError, pStat) {
if (!pError)
if ( pStat.isDirectory() )
processContent(pParams);
else
main.sendFile( pParams );
else
main.sendError(pParams, pError);
});
}
return lRet;
}
function processContent(pParams) {
var p,
lRet = main.checkParams(pParams);
if (lRet) {
p = pParams;
main.commander.getDirContent(p.name, function(pError, pJSON) {
var lQuery, isJSON;
if (pError)
main.sendError(pParams, pError);
else {
lQuery = main.getQuery(p.request);
isJSON = Util.isContainStr(lQuery, 'json');
if (!isJSON)
readIndex(pJSON, pParams);
else {
p.data = Util.stringifyJSON(pJSON);
p.name +='.json';
main.sendResponse(p, null, true);
}
}
});
}
}
function readIndex(pJSON, params) {
var p = params;
Util.ifExec(!minify, function(params) {
var name = params && params.name;
fs.readFile(name || INDEX_PATH, 'utf8', function(error, template) {
var lPanel, lList,
config = main.config,
minify = config.minify;
if (error)
main.sendError(p, error);
else {
p.name = INDEX_PATH,
lPanel = CloudFunc.buildFromJSON(pJSON, FileTemplate, PathTemplate, LinkTemplate),
lList = '<div id=left class=panel>' + lPanel + '</div>' +
'<div id=right class=panel>' + lPanel + '</div>';
main.sendResponse(p, indexProcessing({
additional : lList,
data : template,
}), true);
}
});
}, function(callback) {
Minify.optimize(INDEX_PATH, {
callback : callback,
returnName : true
});
});
}
/* function sets stdout to file log.txt */
function writeLogsToFile() {
var stdout = process.stdout,
writeFile = fs.createWriteStream('log.txt'),
write = writeFile.write.bind(writeFile);
stdout.write = write;
}
})();