-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
187 lines (177 loc) · 4.98 KB
/
server.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
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
var DOCKER_HOST = "localhost";
var DOCKER_PORT = 6732;
var LISTEN_PORT = 8088;
var HOME_PATH = 'app';
var env_arguments = process.argv.splice(2);
if(env_arguments.length >= 1) {
DOCKER_HOST = env_arguments[0];
}
if(env_arguments.length >= 2) {
DOCKER_PORT = env_arguments[1];
}
if(env_arguments.length >= 3) {
HOME_PATH = env_arguments[2];
}
// Mine types
var mine = {
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml",
"map": "text/plain",
"woff": "application/x-woff",
"woff2": "application/font-woff2",
"ttf": "application/x-ttf"
};
//helper function handles file verification
function getFile(filePath, res, page404, static, mine){
//does the requested file exist?
mine = mine ? mine : 'text/html';
fs.exists(filePath, function(exists){
//if it does...
if(exists){
//read the file, run the anonymous function
fs.readFile(filePath, function(err, contents){
if(!err){
//if there was no error
//send the contents with the default 200/ok header
res.writeHead(200, {'Content-Type': mine});
res.end(contents);
} else {
//for our own troubleshooting
res.writeHead(500, mine);
res.end('Server error.');
};
});
} else if (!static){
//if the requested file was not found
//serve-up our custom 404 page
fs.readFile(page404, function(err, contents){
//if there was no error
if(!err){
//send the contents with a 404/not found header
res.writeHead(404, {'Content-Type': mine});
res.end(contents);
} else {
//for our own troubleshooting
res.writeHead(500, mine);
res.end('Server error.');
};
});
} else {
res.writeHead(404, mine);
res.end("Failed to get file");
};
});
};
var hander = function(req, res) {
if(req.url === '/') {
var fileName = HOME_PATH + '/index.html';
getFile(fileName, res, HOME_PATH + '/404.html');
} else {
var pathname = url.parse(req.url).pathname;
var ext = path.extname(pathname);
if(ext) {
ext = ext ? ext.slice(1) : 'unknown';
var realpath = __dirname;
if(mine[ext] && pathname.indexOf('/bower_components') === 0) {
realpath += pathname;
getFile(realpath, res, HOME_PATH + '/404.html', true, mine[ext]);
} else if (mine[ext]){
realpath += '/' + HOME_PATH + pathname;
getFile(realpath, res, HOME_PATH + '/404.html', true, mine[ext]);
} else {
res.writeHead(404, 'text/plain');
res.end();
}
} else {
requestListener(req, res);
}
}
};
var requestListener = function (req, res) {
var baseURL = DOCKER_HOST,
ops = {
host: baseURL,
port: DOCKER_PORT,
path: req.url,
method: req.method,
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
};
if(req.url == "/console") {
res.writeHead(200, "application/json");
res.end('{"host": "' + DOCKER_HOST + '", "port": "' + DOCKER_PORT + '"}');
return;
}
if(req.headers['x-registy-auth']) {
ops.headers['x-registy-auth'] = req.headers['x-registy-auth'];
}
var resHandle = function (response) {
var statusCode = response.statusCode;
var str = '';
if(ops.headers['x-registy-auth']) {
res.writeHead(statusCode, {
'Content-type':'application/plain',
'Transfer-Encoding': 'chunked',
'Connection': 'Transfer-Encoding'
});
} else {
res.writeHead(statusCode, 'application/json');
}
response.on('data', function (chunk, status) {
str = chunk.toString('utf8');
if(ops.headers['x-registy-auth']) {
if(str[str.length - 1] != '\n') {
str += '\n';
}
}
res.write(str);
});
response.on('end', function () {
res.end();
});
};
var handerRequest = function(options) {
var data = options.data;
delete options.data;
var requester = http.request(options, resHandle);
requester.on('error', function(err) {
res.writeHead(500, 'text/plain');
res.end('Failed to connect to ' + baseURL);
});
if(data) {
requester.write(data);
}
requester.end();
};
var resData = '';
req.on('data', function(chunk) {
resData += chunk.toString('utf8');
});
req.on('end', function() {
ops.data = resData;
handerRequest(ops);
});
};
var server = http.createServer(hander);
server.listen(LISTEN_PORT);