-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
45 lines (34 loc) · 1.21 KB
/
index.ts
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
import express from 'express'
import http from 'http'
import { generateThingy } from './main'
const app = express()
app.get('/', async (req, res) => {
// Fetch the query
const { username, year } = req.query as Record<string, string>
// if no username is sent in the request return this simple HTML lul
if (!username) {
res.status(400).send('Error: missing username parameter in query')
return
}
// transform the year intoo a number
const yearN = year ? isNaN(parseInt(year)) ? undefined : parseInt(year) : undefined
// Generate the STL
const bfr = await generateThingy(username, yearN)
if (!bfr) {
res.status(500)
.send('It seems the generator could not generate, Please open an issue on Github https://github.com/aviortheking/codestats-skyline/issues/new')
return
}
// force download the STL with a good name
res
.setHeader('Content-Disposition', `attachment; filename=${username}-${year ? year : 'full'}.stl`)
.status(200)
.send(bfr)
})
// Create the server
const server = http
.createServer(app)
// start the server on the specified port or the 3000
const port = process.env.PORT ? parseInt(process.env.PORT) : 3000
server.listen(port, '0.0.0.0')
console.log(`Server started on port ${port}!`)