optomize index.ts

This commit is contained in:
Scott Gould 2024-09-25 14:05:17 -04:00
parent b280358b65
commit 367b0b8fe6
No known key found for this signature in database

View file

@ -2,19 +2,20 @@
import compression from"compression"; import compression from"compression";
import express, { Request, Response }from"express"; import express, { Request, Response }from"express";
import fs from"node:fs"; import fs from"node:fs/promises";
import fetch from"node-fetch"; import fetch from"node-fetch";
import path from"node:path"; import path from"node:path";
import{ observe, uptime }from"./stats.js"; import{ observe, uptime }from"./stats.js";
import{ getApiUrls, inviteResponse }from"./utils.js"; import{ getApiUrls, inviteResponse }from"./utils.js";
import{ fileURLToPath }from"node:url"; import{ fileURLToPath }from"node:url";
const devmode = (process.env.NODE_ENV || 'development')==='development'; import process from"node:process";
const devmode = (process.env.NODE_ENV || "development") === "development";
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
interface Instance { interface Instance {
name: string; name: string;
[key: string]: any; [key: string]: any;
} }
const app = express(); const app = express();
@ -55,9 +56,9 @@ async function updateInstances(): Promise<void>{
updateInstances(); updateInstances();
app.use("/getupdates", (_req: Request, res: Response)=>{ app.use("/getupdates", async (_req: Request, res: Response)=>{
try{ try{
const stats = fs.statSync(path.join(__dirname, "webpage")); const stats = await fs.stat(path.join(__dirname, "webpage"));
res.send(stats.mtimeMs.toString()); res.send(stats.mtimeMs.toString());
}catch(error){ }catch(error){
console.error("Error getting updates:", error); console.error("Error getting updates:", error);
@ -103,33 +104,40 @@ app.use("/", async (req: Request, res: Response)=>{
} }
const filePath = path.join(__dirname, "webpage", req.path); const filePath = path.join(__dirname, "webpage", req.path);
if(fs.existsSync(filePath)){ try{
await fs.access(filePath);
if(devmode){ if(devmode){
const filePath2 = path.join(__dirname, "../src/webpage", req.path); const filePath2 = path.join(__dirname, "../src/webpage", req.path);
if(fs.existsSync(filePath2)){ try{
await fs.access(filePath2);
res.sendFile(filePath2); res.sendFile(filePath2);
return; return;
} }catch{}
} }
res.sendFile(filePath); res.sendFile(filePath);
}else if(fs.existsSync(`${filePath}.html`)){ }catch{
if(devmode){ try{
const filePath2 = path.join(__dirname, "../src/webpage", req.path); await fs.access(`${filePath}.html`);
if(fs.existsSync(filePath2+".html")){ if(devmode){
res.sendFile(filePath2+".html"); const filePath2 = path.join(__dirname, "../src/webpage", req.path);
return; try{
await fs.access(filePath2 + ".html");
res.sendFile(filePath2 + ".html");
return;
}catch{}
} }
} res.sendFile(`${filePath}.html`);
res.sendFile(`${filePath}.html`); }catch{
}else{ if(req.path.startsWith("/src/webpage")){
if(req.path.startsWith("/src/webpage")){ const filePath2 = path.join(__dirname, "..", req.path);
const filePath2 = path.join(__dirname, "..", req.path); try{
if(fs.existsSync(`${filePath2}`)){ await fs.access(filePath2);
res.sendFile(filePath2); res.sendFile(filePath2);
return; return;
}catch{}
} }
res.sendFile(path.join(__dirname, "webpage", "index.html"));
} }
res.sendFile(path.join(__dirname, "webpage", "index.html"));
} }
}); });
@ -138,4 +146,4 @@ app.listen(PORT, ()=>{
console.log(`Server running on port ${PORT}`); console.log(`Server running on port ${PORT}`);
}); });
export{ getApiUrls }; export{ getApiUrls };