mirror of https://github.com/lissy93/dashy
✨ Adds Netlify support for proxying CORS requests
This commit is contained in:
parent
7f968a708a
commit
e3f2b910ec
|
@ -27,6 +27,11 @@
|
||||||
to = "/.netlify/functions/not-supported"
|
to = "/.netlify/functions/not-supported"
|
||||||
status = 301
|
status = 301
|
||||||
force = true
|
force = true
|
||||||
|
[[redirects]]
|
||||||
|
from = "/cors-proxy"
|
||||||
|
to = "/.netlify/functions/netlify-cors"
|
||||||
|
status = 301
|
||||||
|
force = true
|
||||||
|
|
||||||
# For router history mode, ensure pages land on index
|
# For router history mode, ensure pages land on index
|
||||||
[[redirects]]
|
[[redirects]]
|
||||||
|
@ -40,3 +45,4 @@
|
||||||
[headers.values]
|
[headers.values]
|
||||||
# Uncomment to enable Netlify user control. You must have a paid plan.
|
# Uncomment to enable Netlify user control. You must have a paid plan.
|
||||||
# Basic-Auth = "someuser:somepassword anotheruser:anotherpassword"
|
# Basic-Auth = "someuser:somepassword anotheruser:anotherpassword"
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
/* A Netlify cloud function to handle requests to CORS-disabled services */
|
||||||
|
const axios = require('axios');
|
||||||
|
|
||||||
|
exports.handler = (event, context, callback) => {
|
||||||
|
// Get URL from header or GET param
|
||||||
|
const requestUrl = event.queryStringParameters.url
|
||||||
|
|| event.headers['Target-URL']
|
||||||
|
|| event.headers['target-url'];
|
||||||
|
|
||||||
|
// If URL missing, return error
|
||||||
|
if (!requestUrl) {
|
||||||
|
callback(null, {
|
||||||
|
statusCode: 400,
|
||||||
|
body: JSON.stringify({ success: false, msg: 'Missing Target-URL header' }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare request
|
||||||
|
const requestConfig = {
|
||||||
|
method: 'GET',
|
||||||
|
url: requestUrl,
|
||||||
|
json: event.body,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Response headers
|
||||||
|
const headers = {
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
...event.headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make request
|
||||||
|
axios.request(requestConfig)
|
||||||
|
.then((response) => {
|
||||||
|
const body = JSON.stringify(response.data);
|
||||||
|
callback(null, { statusCode: 200, body, headers });
|
||||||
|
}).catch((error) => {
|
||||||
|
callback(null, {
|
||||||
|
statusCode: 400,
|
||||||
|
body: JSON.stringify({ success: false, msg: 'Request failed', error }),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
Loading…
Reference in New Issue