-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (128 loc) · 4.55 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
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
#!/usr/bin/env node
const httpPort = process.env.PORT || 8080
const jsonLink = process.env.SCRIPTS_LIST_LINK
const accessUsername = process.env.ACCESS_USERNAME
const accessPassword = process.env.ACCESS_PASSWORD
const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.gif': 'image/gif',
'.png': 'image/png',
'other': 'application/octet-stream'
}
var http = require('http')
var url = require('url')
var fs = require('fs')
var path = require('path')
var runner = require('./runner')
var authy = require('./basicauthy')
var scriptsList = {}
let controller = null
var downloadJson = (callback) => {
http.get(jsonLink, (res) => {
var data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('end', () => {
var downloadedObject = JSON.parse(data)
console.log(`[${new Date().toString()}] Downloaded JSON file`)
callback(downloadedObject)
})
})
}
var startWebServer = () => {
var webServer = http.createServer((req, res) => {
var parsedUrl = url.parse(path.normalize(req.url))
var body = ''
console.log(`[${new Date().toString()}] ${req.method} ${req.url}`)
req.on('data', (chunk) => {
if(body.length > 1e6) req.connection.destroy()
body += chunk
})
req.on('end', () => {
authy.makeAuthy(accessUsername, accessPassword, 'This is a protected unit. Unathorised access is not allowed, please use valid credentials.', 'Protected unit', req, res, (req, res) => {
produceResponse(parsedUrl.pathname, body, res)
})
})
})
downloadJson((downloadedObject) => {
scriptsList = downloadedObject
controller = new runner.ScriptsControl(scriptsList)
webServer.listen(httpPort)
console.log(`[${new Date().toString()}] Web Server started at port ${httpPort}`)
})
}
var produceResponse = (pathname, body, res) => {
if(pathname == '/') {
res.writeHead(200, {
'Content-Type': mimeTypes['.html']
})
fs.createReadStream(path.join('static', 'index.html'), 'utf8').pipe(res)
}
else if(pathname == '/script.json') {
res.writeHead(200, {
'Content-Type': mimeTypes['.json'] + '; charset=utf8'
})
// pretty JSON for increased readability
res.end(JSON.stringify(scriptsList, undefined, 4))
}
else if(pathname == '/startScript') {
try {
// instruction object (JSON as POST data)
//
// {
// "start": true | false,
// "scriptName": "nameOfTheScript"
// }
var instruction = JSON.parse(body)
controller.startScript((instruction.start) ? instruction.scriptName : undefined)
var status = controller.status()
status.started = instruction.scriptName
res.writeHead(200, {
'Content-Type': mimeTypes['.json'] + '; charset=utf8'
})
res.end(JSON.stringify(status))
}
catch(err) {
var status = controller.status()
status.notStarted = ''
res.writeHead(200, {
'Content-Type': mimeTypes['.json'] + '; charset=utf8'
})
res.end(JSON.stringify(status))
}
}
else if(pathname == '/stopCurrentlyRunningScript') {
controller.stopCurrentlyRunningScript()
res.writeHead(200, {
'Content-Type': mimeTypes['.json'] + '; charset=utf8'
})
res.end(JSON.stringify(controller.status()))
}
else if(pathname == '/status') {
res.writeHead(200, {
'Content-Type': mimeTypes['.json'] + '; charset=utf8'
})
res.end(JSON.stringify(controller.status()))
}
else {
var fileStream = fs.createReadStream(path.join('static', pathname)).on('error', (err) => {
fourZeroFour(res)
})
var extension = pathname.substring(pathname.lastIndexOf('.')) || 'other'
res.writeHead(200, {
'Content-Type': (mimeTypes[extension] == undefined) ? mimeTypes['other'] : mimeTypes[extension]
})
fileStream.pipe(res)
}
}
var fourZeroFour = (res) => {
res.writeHead(404, {
'Content-Type': 'text/html; charset=utf8'
})
res.end('This is not the resource that you are looking for. It seems like a four zero four error occured.')
}
startWebServer()