|
12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- const axios = require('axios')
- const dotenv = require('dotenv')
- const http = require('http')
-
- const getBadgeUrl = require('./getBadgeUrl')
-
- dotenv.config()
-
- const server = http.createServer(async (req, res) => {
- const { searchParams: rawSearchParams, } = new URL(req.url, process.env.ORIGIN)
- const searchParams = Array
- .from(rawSearchParams.entries())
- .reduce(
- (obj, [key, value, ]) => ({
- ...obj,
- [key]: value,
- }),
- {}
- )
- const badgeUrl = await getBadgeUrl(searchParams)
-
- if (badgeUrl === null) {
- console.log(JSON.stringify(searchParams), 404)
- res.writeHead(404, {})
- res.write('No badge available.')
- res.end()
- return
- }
-
- const response = await axios({
- method: 'GET',
- url: badgeUrl,
- responseType: 'stream'
- })
- res.writeHead(200, { 'content-type': 'image/svg+xml', })
- response.data.on('finish', () => {
- console.log(JSON.stringify(searchParams), 200)
- res.end()
- })
- response.data.pipe(res)
- })
-
- server.listen(process.env.PORT || 2048)
|