Proxy badge lookup for shields.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

  1. const axios = require('axios')
  2. const dotenv = require('dotenv')
  3. const http = require('http')
  4. const getBadgeUrl = require('./getBadgeUrl')
  5. dotenv.config()
  6. const server = http.createServer(async (req, res) => {
  7. const { searchParams: rawSearchParams, } = new URL(req.url, process.env.ORIGIN)
  8. const searchParams = Array
  9. .from(rawSearchParams.entries())
  10. .reduce(
  11. (obj, [key, value, ]) => ({
  12. ...obj,
  13. [key]: value,
  14. }),
  15. {}
  16. )
  17. const badgeUrl = await getBadgeUrl(searchParams)
  18. if (badgeUrl === null) {
  19. console.log(JSON.stringify(searchParams), 404)
  20. res.writeHead(404, {})
  21. res.write('No badge available.')
  22. res.end()
  23. return
  24. }
  25. const response = await axios({
  26. method: 'GET',
  27. url: badgeUrl.toString(),
  28. responseType: 'stream'
  29. })
  30. res.writeHead(200, { 'content-type': 'image/svg+xml', })
  31. response.data.on('finish', () => {
  32. console.log(JSON.stringify(searchParams), 200)
  33. res.end()
  34. })
  35. response.data.pipe(res)
  36. })
  37. server.listen(process.env.PORT || 2048)