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.

42 lines
931 B

  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. res.writeHead(404, {})
  20. res.write('No badge available.')
  21. res.end()
  22. return
  23. }
  24. const response = await axios({
  25. method: 'GET',
  26. url: badgeUrl,
  27. responseType: 'stream'
  28. })
  29. res.writeHead(200, { 'content-type': 'image/svg+xml', })
  30. response.data.on('finish', () => {
  31. res.end()
  32. })
  33. response.data.pipe(res)
  34. })
  35. server.listen(process.env.PORT || 2048)