|
1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 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)
|