updateweb
This commit is contained in:
@@ -5,6 +5,7 @@ const path = require("path");
|
||||
const fs = require("fs");
|
||||
const helmet = require("helmet");
|
||||
const cors = require("cors");
|
||||
const compressionMiddleware = require("./middleware/compression");
|
||||
const { pool, healthCheck } = require("./config/database");
|
||||
const logger = require("./config/logger");
|
||||
const { apiLimiter, authLimiter } = require("./config/rateLimiter");
|
||||
@@ -23,6 +24,13 @@ const baseDir = getBaseDir();
|
||||
|
||||
logger.info(`📁 Serving from: ${baseDir}`);
|
||||
|
||||
// Start cache cleanup scheduler
|
||||
const { startCleanup, stopCleanup } = require("./middleware/cache");
|
||||
startCleanup();
|
||||
|
||||
// Compression middleware - should be early in the chain
|
||||
app.use(compressionMiddleware);
|
||||
|
||||
// Security middleware
|
||||
app.use(
|
||||
helmet({
|
||||
@@ -107,9 +115,41 @@ const productImageFallback = (req, res, next) => {
|
||||
|
||||
app.use("/assets/images/products", productImageFallback);
|
||||
|
||||
app.use(express.static(path.join(baseDir, "public")));
|
||||
app.use("/assets", express.static(path.join(baseDir, "assets")));
|
||||
app.use("/uploads", express.static(path.join(baseDir, "uploads")));
|
||||
// Root redirect - serve the original HTML site
|
||||
app.get("/", (req, res) => {
|
||||
res.sendFile(path.join(baseDir, "public", "home.html"));
|
||||
});
|
||||
|
||||
// Redirect /index to /home
|
||||
app.get("/index", (req, res) => {
|
||||
res.redirect("/home");
|
||||
});
|
||||
|
||||
app.use(
|
||||
express.static(path.join(baseDir, "public"), {
|
||||
index: false,
|
||||
maxAge: "1d", // Cache static files for 1 day
|
||||
etag: true,
|
||||
lastModified: true,
|
||||
})
|
||||
);
|
||||
app.use(
|
||||
"/assets",
|
||||
express.static(path.join(baseDir, "assets"), {
|
||||
maxAge: "7d", // Cache assets for 7 days
|
||||
etag: true,
|
||||
lastModified: true,
|
||||
immutable: true,
|
||||
})
|
||||
);
|
||||
app.use(
|
||||
"/uploads",
|
||||
express.static(path.join(baseDir, "uploads"), {
|
||||
maxAge: "1d", // Cache uploads for 1 day
|
||||
etag: true,
|
||||
lastModified: true,
|
||||
})
|
||||
);
|
||||
|
||||
// Session middleware
|
||||
app.use(
|
||||
@@ -158,11 +198,52 @@ const uploadRoutes = require("./routes/upload");
|
||||
|
||||
// Admin redirect - handle /admin to redirect to login (must be before static files)
|
||||
app.get("/admin", (req, res) => {
|
||||
res.redirect("/admin/login.html");
|
||||
res.redirect("/admin/login");
|
||||
});
|
||||
|
||||
app.get("/admin/", (req, res) => {
|
||||
res.redirect("/admin/login.html");
|
||||
res.redirect("/admin/login");
|
||||
});
|
||||
|
||||
// URL Rewriting Middleware - Remove .html extension (must be before static files)
|
||||
app.use((req, res, next) => {
|
||||
// Skip API routes, static assets with extensions (except .html)
|
||||
if (
|
||||
req.path.startsWith("/api/") ||
|
||||
req.path.startsWith("/uploads/") ||
|
||||
req.path.startsWith("/assets/") ||
|
||||
(req.path.includes(".") && !req.path.endsWith(".html"))
|
||||
) {
|
||||
return next();
|
||||
}
|
||||
|
||||
// Check if path is for admin area
|
||||
if (req.path.startsWith("/admin/")) {
|
||||
const cleanPath = req.path.replace(/\.html$/, "").replace(/^\/admin\//, "");
|
||||
const htmlPath = path.join(baseDir, "admin", cleanPath + ".html");
|
||||
|
||||
if (fs.existsSync(htmlPath)) {
|
||||
return res.sendFile(htmlPath);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if path is for public pages (root level pages)
|
||||
if (!req.path.includes("/admin/")) {
|
||||
let cleanPath = req.path.replace(/^\//, "").replace(/\.html$/, "");
|
||||
|
||||
// Handle root path
|
||||
if (cleanPath === "" || cleanPath === "index") {
|
||||
cleanPath = "home";
|
||||
}
|
||||
|
||||
const htmlPath = path.join(baseDir, "public", cleanPath + ".html");
|
||||
|
||||
if (fs.existsSync(htmlPath)) {
|
||||
return res.sendFile(htmlPath);
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
// Apply rate limiting to API routes
|
||||
@@ -177,16 +258,23 @@ app.use("/api/admin/users", usersRoutes);
|
||||
app.use("/api/admin", uploadRoutes);
|
||||
app.use("/api", publicRoutes);
|
||||
|
||||
// Admin static files (must be after redirect routes)
|
||||
app.use("/admin", express.static(path.join(baseDir, "admin")));
|
||||
// Admin static files (must be after URL rewriting)
|
||||
app.use(
|
||||
"/admin",
|
||||
express.static(path.join(baseDir, "admin"), {
|
||||
maxAge: "1d",
|
||||
etag: true,
|
||||
lastModified: true,
|
||||
})
|
||||
);
|
||||
|
||||
// Favicon route
|
||||
app.get("/favicon.ico", (req, res) => {
|
||||
res.sendFile(path.join(baseDir, "public", "favicon.svg"));
|
||||
});
|
||||
|
||||
// Root redirect to home page
|
||||
app.get("/", (req, res) => {
|
||||
// Old site (if needed for reference)
|
||||
app.get("/old", (req, res) => {
|
||||
res.sendFile(path.join(baseDir, "public", "index.html"));
|
||||
});
|
||||
|
||||
@@ -248,6 +336,9 @@ const server = app.listen(PORT, "0.0.0.0", () => {
|
||||
const gracefulShutdown = (signal) => {
|
||||
logger.info(`${signal} received, shutting down gracefully...`);
|
||||
|
||||
// Stop cache cleanup
|
||||
stopCleanup();
|
||||
|
||||
server.close(() => {
|
||||
logger.info("HTTP server closed");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user