109 lines
2.7 KiB
JavaScript
109 lines
2.7 KiB
JavaScript
require("dotenv").config();
|
|
|
|
const express = require("express");
|
|
const cors = require("cors");
|
|
const helmet = require("helmet");
|
|
const morgan = require("morgan");
|
|
const compression = require("compression");
|
|
const rateLimit = require("express-rate-limit");
|
|
const path = require("path");
|
|
|
|
const { initDatabase } = require("./database/init");
|
|
const authRoutes = require("./routes/auth");
|
|
const reportsRoutes = require("./routes/reports");
|
|
const usersRoutes = require("./routes/users");
|
|
const storesRoutes = require("./routes/stores");
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 5000;
|
|
|
|
// Security middleware
|
|
app.use(helmet());
|
|
app.use(
|
|
cors({
|
|
origin: process.env.FRONTEND_URL || "http://localhost:3000",
|
|
credentials: true,
|
|
})
|
|
);
|
|
|
|
console.log("Loaded ENV PORT:", process.env.PORT);
|
|
console.log("Loaded ENV FRONTEND_URL:", process.env.FRONTEND_URL);
|
|
|
|
// Compression
|
|
app.use(compression());
|
|
|
|
// Rate limiting
|
|
const limiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
max: 1000, // limit each IP to 1000 requests per windowMs
|
|
message: "Too many requests from this IP",
|
|
});
|
|
app.use(limiter);
|
|
|
|
// Middleware
|
|
app.use(morgan("combined"));
|
|
app.use(express.json({ limit: "10mb" }));
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Routes
|
|
app.use("/api/auth", authRoutes);
|
|
app.use("/api/reports", reportsRoutes);
|
|
app.use("/api/users", usersRoutes);
|
|
app.use("/api/stores", storesRoutes);
|
|
|
|
// Health check
|
|
app.get("/health", (req, res) => {
|
|
res.status(200).json({
|
|
status: "OK",
|
|
timestamp: new Date().toISOString(),
|
|
uptime: process.uptime(),
|
|
});
|
|
});
|
|
|
|
// Error handling middleware
|
|
app.use((err, req, res, next) => {
|
|
console.error("Error:", err);
|
|
res.status(500).json({
|
|
error: "Internal Server Error",
|
|
message:
|
|
process.env.NODE_ENV === "development"
|
|
? err.message
|
|
: "Something went wrong",
|
|
});
|
|
});
|
|
|
|
// 404 handler
|
|
app.use((req, res) => {
|
|
res.status(404).json({ error: "Route not found" });
|
|
});
|
|
|
|
// Initialize database and start server
|
|
async function startServer() {
|
|
try {
|
|
await initDatabase();
|
|
console.log("Database initialized successfully");
|
|
|
|
const server = app.listen(PORT, "0.0.0.0", () => {
|
|
console.log(`🚀 Server running on port ${PORT}`);
|
|
console.log(`🔗 API URL: http://localhost:${PORT}/api`);
|
|
console.log(`📊 Health check: http://localhost:${PORT}/health`);
|
|
});
|
|
|
|
const shutdown = () => {
|
|
console.log("Shutdown signal received, closing server...");
|
|
server.close(() => {
|
|
console.log("Server closed gracefully");
|
|
process.exit(0);
|
|
});
|
|
};
|
|
|
|
process.on("SIGTERM", shutdown);
|
|
process.on("SIGINT", shutdown);
|
|
} catch (error) {
|
|
console.error("Failed to start server:", error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
startServer();
|