2023-07-22 16:18:28 +02:00
|
|
|
const axios = require('axios');
|
2023-06-22 16:24:36 +02:00
|
|
|
|
|
|
|
exports.handler = async function(event, context) {
|
2023-07-28 22:46:20 +02:00
|
|
|
const siteURL = (event.queryStringParameters || event.query).url;
|
2023-07-22 16:18:28 +02:00
|
|
|
|
2023-06-22 16:24:36 +02:00
|
|
|
if (!siteURL) {
|
|
|
|
return {
|
|
|
|
statusCode: 400,
|
2023-07-10 00:23:50 +02:00
|
|
|
body: JSON.stringify({ error: 'Missing url query parameter' }),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let parsedURL;
|
|
|
|
try {
|
|
|
|
parsedURL = new URL(siteURL);
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
statusCode: 400,
|
|
|
|
body: JSON.stringify({ error: 'Invalid url query parameter' }),
|
2023-06-22 16:24:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const robotsURL = `${parsedURL.protocol}//${parsedURL.hostname}/robots.txt`;
|
|
|
|
|
|
|
|
try {
|
2023-07-22 16:18:28 +02:00
|
|
|
const response = await axios.get(robotsURL);
|
2023-06-22 16:24:36 +02:00
|
|
|
|
2023-07-22 16:18:28 +02:00
|
|
|
if (response.status === 200) {
|
2023-06-22 16:24:36 +02:00
|
|
|
return {
|
|
|
|
statusCode: 200,
|
2023-07-22 16:18:28 +02:00
|
|
|
body: response.data,
|
2023-06-22 16:24:36 +02:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
statusCode: response.status,
|
2023-07-10 00:23:50 +02:00
|
|
|
body: JSON.stringify({ error: 'Failed to fetch robots.txt', statusCode: response.status }),
|
2023-06-22 16:24:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
statusCode: 500,
|
2023-07-10 00:23:50 +02:00
|
|
|
body: JSON.stringify({ error: `Error fetching robots.txt: ${error.message}` }),
|
2023-06-22 16:24:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|