-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (100 loc) · 2.94 KB
/
Copy pathindex.js
File metadata and controls
109 lines (100 loc) · 2.94 KB
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
import http from 'http'
import busboy from 'busboy'
import './instrument.js'
import { getServerStatus, getAllServersStatus, restartServer } from './management.js'
import { wantsNewArtifacts, onFileUpload, doesServerExist } from './deployments.js'
const host = process.env.SERVER_HOST ?? '0.0.0.0'
const port = process.env.SERVER_PORT ?? 8564
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${host}`)
const params = url.searchParams
if (url.pathname === '/status') {
try {
let ret = {}
if (params.has('server')) {
ret[params.get('server')] = await getServerStatus(params.get('server'))
} else {
ret = await getAllServersStatus()
}
res.statusCode = 200
res.end(JSON.stringify(ret))
} catch (e) {
res.statusCode = 500
res.end(JSON.stringify({ message: e.message }))
}
return
}
if (url.pathname === '/restart' && params.has('server')) {
try {
await restartServer(params.get('server'))
res.statusCode = 200
res.end(JSON.stringify({ message: 'Success' }))
} catch (e) {
res.statusCode = 500
res.end(JSON.stringify({ message: e.message }))
}
return
}
if (
url.pathname === '/build/check' &&
params.has('server') &&
params.has('buildstamp') &&
params.has('byond') &&
params.has('rustg')
) {
const server = params.get('server')
const serverExists = await doesServerExist(server)
if (!serverExists) {
res.statusCode = 400
res.end(JSON.stringify({ message: 'Server does not exist' }))
return
}
const wants = await wantsNewArtifacts(
server,
parseInt(params.get('buildstamp')),
params.get('byond'),
params.get('rustg')
)
res.statusCode = 200
res.end(JSON.stringify({ outdated: wants }))
return
}
if (url.pathname === '/build/upload' && req.method === 'POST' && params.has('server')) {
const server = params.get('server')
const serverExists = await doesServerExist(server)
if (!serverExists) {
res.statusCode = 400
res.end(JSON.stringify({ message: 'Server does not exist' }))
return
}
const validMimes = ['application/gzip', 'application/zip']
const validNames = ['build', 'byond', 'rustg']
const bb = busboy({ headers: req.headers })
bb.on('file', (name, file, info) => {
if (!validMimes.includes(info.mimeType)) {
file.resume()
return
}
try {
if (validNames.includes(name)) {
onFileUpload(name, file, info, server)
} else {
file.resume()
}
} catch (e) {
console.error(e.message)
}
})
bb.on('close', () => {
res.statusCode = 200
res.end(JSON.stringify({ message: 'Success' }))
})
req.pipe(bb)
return
}
res.statusCode = 404
res.end()
})
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`)
})