-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (37 loc) · 1.79 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
//server.js v1.0.1
const config = require ('./config.json');
var http = require('http');
var url = require('url');
var queryString = require('querystring');
var os = require('os');
let containerId = os.hostname();
// app instance settings
config.app_name = process.env.APP_NAME?process.env.APP_NAME:config.app_name;
config.app_version = process.env.APP_VERSION?process.env.APP_VERSION:config.app_version;
config.app_path = process.env.APP_PATH?process.env.APP_PATH:config.app_path;
config.app_version_path = process.env.APP_VERSION_PATH?process.env.APP_VERSION_PATH:config.app_version_path;
config.app_port = process.env.APP_PORT?process.env.APP_PORT:config.app_port;
let app_name = config.app_name;
let version = config.app_version;
let appPath = config.app_path;
let versionPath = config.app_version_path;
let appPort = config.app_port;
console.log(`config: ${JSON.stringify(config, undefined, config.json_indentation)}`);
var server = http.createServer(function(request, response) {
var parts = url.parse(request.url);
var responseText = "";
if (parts.pathname === (versionPath)) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end(version);
} else if (parts.pathname.startsWith(appPath)) {
responseText += app_name + " v" + version + " via hostname/containerId: " + containerId + "\n\n";
responseText += "Full passed URL: '" + request.url + "'";
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end(responseText);
} else {
// error handle unknown path, 200 for demo only should use 404 in production
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end("Endpoint '" + parts.pathname + "' not found... try " + appPath + " or '" + versionPath + "'. Full passed URL: '" + request.url + "'");
}
});
server.listen(appPort);