Skip to content

Instantly share code, notes, and snippets.

@vaidd4
Last active April 25, 2019 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaidd4/f7e499ae688c9a682c2ccfcb5b4da79d to your computer and use it in GitHub Desktop.
Save vaidd4/f7e499ae688c9a682c2ccfcb5b4da79d to your computer and use it in GitHub Desktop.
eXtended Blih API project
/**
* This is a proxy for making w3c compliant requests to the EPITECH's BLIH API.
* It allows to make such calls fron any w3c compliant source (e.g. XHR requests).
* The proxy uses https for security reasons.
* To check BLIH API status simply make a GET request on '/'.
*
* TODO:
* - Add endpoint showing every endpoint available (API documentation).
*/
const HOST_URL = 'blih.epitech.eu'
const PORT = 3042
const fs = require('fs')
const https = require('https')
const path = require('path')
// NOTE: These files need to be generated for the server to run with https
const oOptions = {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'))
}
function decode (sBase64) {
return Buffer.from(sBase64, 'base64').toString()
}
function getDataFromStream (stream) {
return (new Promise((resolve, reject) => {
let data = ''
stream.on('data', chunk => (data += chunk))
stream.on('end', () => resolve(data))
stream.on('error', err => reject(err))
})).then(d => [null, d]).catch(e => [e])
}
function sendError (res, code, message, error) {
res.statusCode = code
if (code === 500) console.error(error)
res.end(message)
}
async function fHandler (req, res) {
console.log(`${req.method}: ${req.url}`)
let [err, data] = await getDataFromStream(req)
if (err) {
return sendError(res, 500, 'Server Error', err)
}
if (!data && !req.headers['authorization']) {
return sendError(res, 400, 'No Data')
}
if (req.headers['authorization']) {
try {
data = decode(req.headers['authorization'].split(' ')[1])
} catch (e) {
return sendError(res, 400, 'Bad Authorisation')
}
}
const blihRequest = https.request({
hostname: HOST_URL,
path: req.url,
method: req.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}, blihRequestCallback)
blihRequest.on('error', err => sendError(res, 500, 'Request Error', err))
blihRequest.write(data)
blihRequest.end()
async function blihRequestCallback (response) {
const [err, body] = await getDataFromStream(response)
if (err || !body) {
return sendError(res, 500, 'Response Error', err)
}
res.writeHead(response.statusCode, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Authorization',
'Content-Type': response.headers['content-type']
})
res.write(body)
res.end()
}
}
https.createServer(oOptions, fHandler).listen(PORT)
console.log(`listen on https://localhost:${PORT}`)
/**
* [JS Standard](https://standardjs.com/demo.html?gist=f7e499ae688c9a682c2ccfcb5b4da79d)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment