cleanup
This commit is contained in:
parent
064c6b4333
commit
5b677ea480
37
server.js
37
server.js
@ -8,7 +8,6 @@ require("dotenv").config();
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
|
||||
// Middleware
|
||||
app.use(helmet());
|
||||
app.use(
|
||||
cors({
|
||||
@ -20,7 +19,6 @@ app.use(
|
||||
);
|
||||
app.use(express.json());
|
||||
|
||||
// Auth middleware
|
||||
const authenticateAdmin = (req, res, next) => {
|
||||
const adminPassword = req.headers.authorization?.replace("Bearer ", "");
|
||||
if (adminPassword !== process.env.ADMIN_PASSWORD) {
|
||||
@ -29,11 +27,9 @@ const authenticateAdmin = (req, res, next) => {
|
||||
next();
|
||||
};
|
||||
|
||||
// File paths
|
||||
const DISCOUNT_CODES_PATH = path.join(__dirname, "data", "discountCodes.json");
|
||||
const PRODUCTS_PATH = path.join(__dirname, "data", "products.js");
|
||||
|
||||
// In-memory cache
|
||||
let cache = {
|
||||
discountCodes: null,
|
||||
products: null,
|
||||
@ -43,7 +39,6 @@ let cache = {
|
||||
},
|
||||
};
|
||||
|
||||
// Helper functions
|
||||
const clearCache = () => {
|
||||
cache.discountCodes = null;
|
||||
cache.products = null;
|
||||
@ -78,17 +73,15 @@ const writeDiscountCodes = async (codes) => {
|
||||
const readProducts = async () => {
|
||||
try {
|
||||
const data = await fs.readFile(PRODUCTS_PATH, "utf8");
|
||||
// Extract the products array from the JS file
|
||||
const match = data.match(
|
||||
/export const products: Product\[\] = (\[[\s\S]*?\]);/,
|
||||
);
|
||||
if (match) {
|
||||
// Convert JS object notation to JSON
|
||||
const productsString = match[1]
|
||||
.replace(/(\w+):/g, '"$1":') // Quote keys
|
||||
.replace(/'/g, '"') // Single quotes to double quotes
|
||||
.replace(/,\s*}/g, "}") // Remove trailing commas
|
||||
.replace(/,\s*]/g, "]"); // Remove trailing commas in arrays
|
||||
.replace(/(\w+):/g, '"$1":')
|
||||
.replace(/'/g, '"')
|
||||
.replace(/,\s*}/g, "}")
|
||||
.replace(/,\s*]/g, "]");
|
||||
|
||||
return JSON.parse(productsString);
|
||||
}
|
||||
@ -104,8 +97,8 @@ const writeProducts = async (products) => {
|
||||
const jsContent = `import type { Product } from "../types/product";
|
||||
|
||||
export const products: Product[] = ${JSON.stringify(products, null, 2)
|
||||
.replace(/"([^"]+)":/g, "$1:") // Remove quotes from keys
|
||||
.replace(/"/g, '"')}; // Keep string quotes
|
||||
.replace(/"([^"]+)":/g, "$1:")
|
||||
.replace(/"/g, '"')};
|
||||
`;
|
||||
await fs.writeFile(PRODUCTS_PATH, jsContent, "utf8");
|
||||
return true;
|
||||
@ -115,14 +108,11 @@ export const products: Product[] = ${JSON.stringify(products, null, 2)
|
||||
}
|
||||
};
|
||||
|
||||
// Routes
|
||||
|
||||
// Health check
|
||||
app.get("/health", (req, res) => {
|
||||
res.json({ status: "ok", timestamp: new Date().toISOString() });
|
||||
});
|
||||
|
||||
// Get discount codes
|
||||
app.get("/api/discount-codes", async (req, res) => {
|
||||
try {
|
||||
if (!cache.discountCodes) {
|
||||
@ -138,7 +128,6 @@ app.get("/api/discount-codes", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Add discount code
|
||||
app.post("/api/discount-codes", authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { code, percentage, expiration, description } = req.body;
|
||||
@ -167,7 +156,6 @@ app.post("/api/discount-codes", authenticateAdmin, async (req, res) => {
|
||||
return res.status(500).json({ error: "Failed to save discount code" });
|
||||
}
|
||||
|
||||
// Clear cache to force refresh
|
||||
clearCache();
|
||||
|
||||
res.json({ success: true, code: newCode });
|
||||
@ -177,7 +165,6 @@ app.post("/api/discount-codes", authenticateAdmin, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Delete discount code
|
||||
app.delete("/api/discount-codes/:code", authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { code } = req.params;
|
||||
@ -195,7 +182,6 @@ app.delete("/api/discount-codes/:code", authenticateAdmin, async (req, res) => {
|
||||
return res.status(500).json({ error: "Failed to delete discount code" });
|
||||
}
|
||||
|
||||
// Clear cache to force refresh
|
||||
clearCache();
|
||||
|
||||
res.json({ success: true });
|
||||
@ -205,7 +191,6 @@ app.delete("/api/discount-codes/:code", authenticateAdmin, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Get products
|
||||
app.get("/api/products", async (req, res) => {
|
||||
try {
|
||||
if (!cache.products) {
|
||||
@ -221,7 +206,6 @@ app.get("/api/products", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Update product price
|
||||
app.put("/api/products/:productId", authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { productId } = req.params;
|
||||
@ -245,7 +229,6 @@ app.put("/api/products/:productId", authenticateAdmin, async (req, res) => {
|
||||
return res.status(500).json({ error: "Failed to update product" });
|
||||
}
|
||||
|
||||
// Clear cache to force refresh
|
||||
clearCache();
|
||||
|
||||
res.json({ success: true, product: products[productIndex] });
|
||||
@ -255,7 +238,6 @@ app.put("/api/products/:productId", authenticateAdmin, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Get cache status (for debugging)
|
||||
app.get("/api/cache-status", (req, res) => {
|
||||
res.json({
|
||||
cache: {
|
||||
@ -271,24 +253,20 @@ app.get("/api/cache-status", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Clear cache endpoint (for debugging)
|
||||
app.post("/api/clear-cache", authenticateAdmin, (req, res) => {
|
||||
clearCache();
|
||||
res.json({ success: true, message: "Cache cleared" });
|
||||
});
|
||||
|
||||
// Error handling middleware
|
||||
app.use((error, req, res, next) => {
|
||||
console.error("Unhandled error:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
});
|
||||
|
||||
// 404 handler
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({ error: "Not found" });
|
||||
});
|
||||
|
||||
// Ensure data directory exists
|
||||
const initializeDataDirectory = async () => {
|
||||
const dataDir = path.join(__dirname, "data");
|
||||
try {
|
||||
@ -296,14 +274,12 @@ const initializeDataDirectory = async () => {
|
||||
} catch {
|
||||
await fs.mkdir(dataDir, { recursive: true });
|
||||
|
||||
// Create initial discount codes file if it doesn't exist
|
||||
try {
|
||||
await fs.access(DISCOUNT_CODES_PATH);
|
||||
} catch {
|
||||
await fs.writeFile(DISCOUNT_CODES_PATH, "{}", "utf8");
|
||||
}
|
||||
|
||||
// Create initial products file if it doesn't exist
|
||||
try {
|
||||
await fs.access(PRODUCTS_PATH);
|
||||
} catch {
|
||||
@ -355,7 +331,6 @@ export const products: Product[] = [
|
||||
}
|
||||
};
|
||||
|
||||
// Start server
|
||||
const startServer = async () => {
|
||||
try {
|
||||
await initializeDataDirectory();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user