Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize 1 #17

Merged
merged 9 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,47 @@ routes.forEach((route) => {
});
});

app.get("/misc/*", (req, res, next) => {
app.get("/misc/*", async (req, res, next) => {
const baseUrl = "https://raw.githubusercontent.com/kfm5/a/main";
fetchData(req, res, next, baseUrl);
const secondaryUrl = "https://raw.githubusercontent.com/22yeets22/a/main";
await fetchDataFromGithub(req, res, next, baseUrl, secondaryUrl);
});

async function fetchData(req, res, next, baseUrl) {
const reqTarget = `${baseUrl}/${req.params[0]}`;
const asset = await fetch(reqTarget);
async function fetchDataFromGithub(req, res, next,baseUrl, secondaryUrl = null) {
function isAFile(urlString) {
return urlString.trim().split("/").pop().length !== 0;
}

if (asset.ok) {
const data = await asset.arrayBuffer();
res.end(Buffer.from(data));
} else {
const indexReqTarget = `${baseUrl}/${req.params[0]}/index.html`;
const indexAsset = await fetch(indexReqTarget);
if (indexAsset.ok) {
const indexData = await indexAsset.arrayBuffer();
res.end(Buffer.from(indexData));
async function fetchDataOneSource(req, res, next, url) {
if (isAFile(req.params[0])) {
const reqTarget = `${url}/${req.params[0]}`;
const asset = await fetch(reqTarget);
if (asset.ok) {
const data = await asset.arrayBuffer();
res.end(Buffer.from(data));
return true;
}
} else {
res
.status(404)
.send(
"Not found, please clear your cache, or email me at [email protected], If this issue persists, try clicking <a href='index.html'>here</a> and if it works, change your bookmark to the new link."
);
const indexReqTarget = `${url}/${req.params[0]}/index.html`;
const indexAsset = await fetch(indexReqTarget);
if (indexAsset.ok) {
const indexData = await indexAsset.arrayBuffer();
res.end(Buffer.from(indexData));
return true;
}
}
return false;
}

try {
if (await fetchDataOneSource(req, res, next, baseUrl)) return;
if (secondaryUrl) {
if (await fetchDataOneSource(req, res, next, secondaryUrl)) return;
}
res.status(404).send("Resource not found");
} catch (error) {
console.error("Error fetching data, internal server error:", error);
res.status(500).send("Internal Server Error");
}
}

Expand Down
10 changes: 8 additions & 2 deletions static/assets/json/motd.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"motd": {"body": "fun fact 55gms is open source on gothib", "footer": "redissus"},
"qotd": {"body": "life is like a box of chocolates it doesn't last long for fat people", "footer": "[email protected]"}
"motd": {
"body": "fun fact 55gms is open source on gothib",
"footer": "redissus"
},
"qotd": {
"body": "life is like a box of chocolates it doesn't last long for fat people",
"footer": "[email protected]"
}
}
Loading