Skip to content

Commit

Permalink
Merge pull request #1473 from NetsBlox/set-cloud-via-qs
Browse files Browse the repository at this point in the history
Configure the cloud to connect to via qs
  • Loading branch information
brollb authored Dec 12, 2023
2 parents 3fb1bf8 + 5568192 commit f0d48e3
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions utils/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const isDevMode = ENV !== "production";
writeCloudFile(CLOUD_URL);

const file = new nodeStatic.Server(path.join(__dirname, ".."));
const getExampleThumbnail = cached(async function (name) {
console.log("getting thumbnail url for", name);
const getExampleThumbnail = cached(async function (cloudUrl, name) {
const filepath = path.join(
__dirname,
"..",
Expand All @@ -33,7 +32,7 @@ const getExampleThumbnail = cached(async function (name) {
"<thumbnail>data:image/png;base64,",
);

return CLOUD_URL +
return cloudUrl +
`/projects/thumbnail?xml=${encodeURIComponent(thumbnailXml)}`;
});
const server = http.createServer(async (req, res) => {
Expand All @@ -42,6 +41,7 @@ const server = http.createServer(async (req, res) => {
.map((url) => url.replace(/#.*$/, ""));
const isIndexHtml = url === "/" || url === "/index.html";
console.log(req.url);

if (isIndexHtml) {
// dynamically generate the index.html file
const query = Object.fromEntries(
Expand All @@ -50,39 +50,52 @@ const server = http.createServer(async (req, res) => {
return [key, decodeURIComponent(value)];
}),
);
const cloudUrl = query.cloud || CLOUD_URL;

const metaInfo = {
title: "NetsBlox",
description: "Add project notes here...",
cloud: CLOUD_URL,
cloud: cloudUrl,
isDevMode,
};

if (query.action === "present") {
const url = CLOUD_URL +
`/projects/user/${query.Username}/${query.ProjectName}/metadata`;
const response = await fetch(url);
if (response.ok) {
const metadata = await response.json();
try {
const metadata = await getPublicProjectMetadata(cloudUrl, query);
// TODO: parse the notes? These should probably be saved separately
metaInfo.title = metadata.name;
if (metadata) {
metaInfo.title = metadata.name;
metaInfo.image = {
url: cloudUrl + `/projects/id/${metadata.id}/thumbnail`,
width: 640,
height: 480,
};
}
} catch (err) {
console.warn(
`Unable to fetch public project metadata from ${cloudUrl}: ${err.message}`,
);
}
} else if (query.action === "example" && query.ProjectName) {
try {
const url = await getExampleThumbnail(cloudUrl, query.ProjectName);
metaInfo.image = {
url: CLOUD_URL + `/projects/id/${metadata.id}/thumbnail`,
url,
width: 640,
height: 480,
};
} catch (err) {
console.warn(
`Unable to fetch example "${query.ProjectName}"`,
);
}
} else if (query.action === "example" && query.ProjectName) {
metaInfo.image = {
url: await getExampleThumbnail(query.ProjectName),
width: 640,
height: 480,
};
}

const userAgent = req.headers["user-agent"];
if (userAgent) {
addScraperSettings(userAgent, metaInfo);
}

res.writeHead(200);
res.end(indexTpl(metaInfo));
} else {
Expand All @@ -92,19 +105,25 @@ const server = http.createServer(async (req, res) => {
server.listen(port);
console.log("listening on port", port);

async function getPublicProjectMetadata(cloudUrl, query) {
const url = cloudUrl +
`/projects/user/${query.Username}/${query.ProjectName}/metadata`;
const response = await fetch(url);
if (response.ok) {
return await response.json();
}
}

// Cached function
function cached(fn) {
const cacheStore = {};
assert.equal(
fn.length,
1,
"Only functions accepting a single argument can be cached for now.",
);
return async function (arg) {
if (!cacheStore[arg]) {
cacheStore[arg] = await fn(arg);

return async function () {
const key = [...arguments].join("\t");
if (!cacheStore[key]) {
cacheStore[key] = await fn(...arguments);
}
return cacheStore[arg];
return cacheStore[key];
};
}

Expand Down

0 comments on commit f0d48e3

Please sign in to comment.