Update artifacts/api-server/src/app.ts
Browse files
artifacts/api-server/src/app.ts
CHANGED
|
@@ -6,50 +6,16 @@ import http from "node:http";
|
|
| 6 |
import compression from "compression";
|
| 7 |
import cors from "cors";
|
| 8 |
import pinoHttp from "pino-http";
|
| 9 |
-
import { stackServerApp } from "@stackframe/stack";
|
| 10 |
import router from "./routes";
|
| 11 |
import { logger } from "./lib/logger";
|
| 12 |
|
| 13 |
const app: Express = express();
|
| 14 |
|
| 15 |
-
app.use(
|
| 16 |
-
|
| 17 |
-
logger,
|
| 18 |
-
serializers: {
|
| 19 |
-
req(req) {
|
| 20 |
-
return { id: req.id, method: req.method, url: req.url?.split("?")[0] };
|
| 21 |
-
},
|
| 22 |
-
res(res) {
|
| 23 |
-
return { statusCode: res.statusCode };
|
| 24 |
-
},
|
| 25 |
-
},
|
| 26 |
-
}),
|
| 27 |
-
);
|
| 28 |
-
|
| 29 |
-
app.use(compression({
|
| 30 |
-
filter: (req, res) => {
|
| 31 |
-
if (res.getHeader("Content-Type")?.toString().includes("text/event-stream")) return false;
|
| 32 |
-
return compression.filter(req, res);
|
| 33 |
-
}
|
| 34 |
-
}));
|
| 35 |
app.use(cors());
|
| 36 |
-
|
| 37 |
app.use(express.json({ limit: "5mb" }));
|
| 38 |
app.use(express.urlencoded({ extended: true }));
|
| 39 |
-
|
| 40 |
-
// โ
Stack Auth middleware ุจุฏู Clerk
|
| 41 |
-
app.use(async (req, res, next) => {
|
| 42 |
-
const authHeader = req.headers.authorization;
|
| 43 |
-
if (authHeader?.startsWith("Bearer ")) {
|
| 44 |
-
const token = authHeader.split(" ")[1];
|
| 45 |
-
try {
|
| 46 |
-
const user = await stackServerApp.getUser(token);
|
| 47 |
-
(req as any).auth = { userId: user?.id };
|
| 48 |
-
} catch { /* ignore */ }
|
| 49 |
-
}
|
| 50 |
-
next();
|
| 51 |
-
});
|
| 52 |
-
|
| 53 |
app.use("/api", router);
|
| 54 |
|
| 55 |
// ๐ฆ ุชุญู
ูู ุงูู
ุดุฑูุน ูู ZIP
|
|
@@ -58,49 +24,19 @@ app.get("/api/download-project", async (_req: Request, res: Response) => {
|
|
| 58 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 59 |
const projectRoot = path.join(__dirname, "..", "..", "..");
|
| 60 |
const zipFile = path.join(projectRoot, "project.zip");
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
`cd "${projectRoot}" && zip -r project.zip . -x "node_modules/*" "**/node_modules/*" ".git/*" "*.zip"`,
|
| 64 |
-
{ stdio: "ignore" }
|
| 65 |
-
);
|
| 66 |
-
|
| 67 |
-
if (existsSync(zipFile)) {
|
| 68 |
-
res.download(zipFile);
|
| 69 |
-
} else {
|
| 70 |
-
res.status(404).json({ error: "ูู
ูุชู
ุฅูุดุงุก ุงูู
ูู" });
|
| 71 |
-
}
|
| 72 |
});
|
| 73 |
|
| 74 |
-
// ุฎุฏู
ุฉ ุงูู
ููุงุช ุงูุซุงุจุชุฉ ูููุงุฌูุฉ ุงูุฃู
ุงู
ูุฉ
|
| 75 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 76 |
const staticDir = path.join(__dirname, "..", "..", "fb-publisher", "dist", "public");
|
| 77 |
|
| 78 |
if (existsSync(staticDir) && existsSync(path.join(staticDir, "index.html"))) {
|
| 79 |
console.log(`โ
Serving static files from: ${staticDir}`);
|
| 80 |
app.use(express.static(staticDir));
|
| 81 |
-
app.use((_req: Request, res: Response) => {
|
| 82 |
-
res.sendFile(path.join(staticDir, "index.html"));
|
| 83 |
-
});
|
| 84 |
} else {
|
| 85 |
console.warn(`โ ๏ธ Static dir not found at: ${staticDir}`);
|
| 86 |
-
const vitePort = process.env.VITE_DEV_PORT ?? "18305";
|
| 87 |
-
app.use((req: Request, res: Response) => {
|
| 88 |
-
const options = {
|
| 89 |
-
hostname: "127.0.0.1",
|
| 90 |
-
port: Number(vitePort),
|
| 91 |
-
path: req.url,
|
| 92 |
-
method: req.method,
|
| 93 |
-
headers: { ...req.headers, host: `localhost:${vitePort}` },
|
| 94 |
-
};
|
| 95 |
-
const proxyReq = http.request(options, (proxyRes) => {
|
| 96 |
-
res.writeHead(proxyRes.statusCode ?? 200, proxyRes.headers as Record<string, string>);
|
| 97 |
-
proxyRes.pipe(res, { end: true });
|
| 98 |
-
});
|
| 99 |
-
proxyReq.on("error", () => {
|
| 100 |
-
res.status(503).send("Vite dev server not running on port " + vitePort);
|
| 101 |
-
});
|
| 102 |
-
req.pipe(proxyReq, { end: true });
|
| 103 |
-
});
|
| 104 |
}
|
| 105 |
|
| 106 |
export default app;
|
|
|
|
| 6 |
import compression from "compression";
|
| 7 |
import cors from "cors";
|
| 8 |
import pinoHttp from "pino-http";
|
|
|
|
| 9 |
import router from "./routes";
|
| 10 |
import { logger } from "./lib/logger";
|
| 11 |
|
| 12 |
const app: Express = express();
|
| 13 |
|
| 14 |
+
app.use(pinoHttp({ logger, serializers: { req(req) { return { id: req.id, method: req.method, url: req.url?.split("?")[0] }; }, res(res) { return { statusCode: res.statusCode }; } } }));
|
| 15 |
+
app.use(compression({ filter: (req, res) => { if (res.getHeader("Content-Type")?.toString().includes("text/event-stream")) return false; return compression.filter(req, res); } }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
app.use(cors());
|
|
|
|
| 17 |
app.use(express.json({ limit: "5mb" }));
|
| 18 |
app.use(express.urlencoded({ extended: true }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
app.use("/api", router);
|
| 20 |
|
| 21 |
// ๐ฆ ุชุญู
ูู ุงูู
ุดุฑูุน ูู ZIP
|
|
|
|
| 24 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 25 |
const projectRoot = path.join(__dirname, "..", "..", "..");
|
| 26 |
const zipFile = path.join(projectRoot, "project.zip");
|
| 27 |
+
execSync(`cd "${projectRoot}" && zip -r project.zip . -x "node_modules/*" "**/node_modules/*" ".git/*" "*.zip"`, { stdio: "ignore" });
|
| 28 |
+
if (existsSync(zipFile)) { res.download(zipFile); } else { res.status(404).json({ error: "ูู
ูุชู
ุฅูุดุงุก ุงูู
ูู" }); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
});
|
| 30 |
|
|
|
|
| 31 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 32 |
const staticDir = path.join(__dirname, "..", "..", "fb-publisher", "dist", "public");
|
| 33 |
|
| 34 |
if (existsSync(staticDir) && existsSync(path.join(staticDir, "index.html"))) {
|
| 35 |
console.log(`โ
Serving static files from: ${staticDir}`);
|
| 36 |
app.use(express.static(staticDir));
|
| 37 |
+
app.use((_req: Request, res: Response) => { res.sendFile(path.join(staticDir, "index.html")); });
|
|
|
|
|
|
|
| 38 |
} else {
|
| 39 |
console.warn(`โ ๏ธ Static dir not found at: ${staticDir}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
export default app;
|