Add production-ready deployment configuration for Raspberry Pi cluster with comprehensive documentation and automation scripts. Kubernetes Manifests (deploy/k8s/): - namespace.yaml - Dedicated namespace for the application - configmap.yaml - Environment configuration (MongoDB URI, ports, URLs) - secrets.yaml.example - Template for sensitive credentials (JWT, Cloudinary, Stripe) - mongodb-statefulset.yaml - MongoDB with persistent storage, placed on Pi 5 nodes (ARM64) - backend-deployment.yaml - Backend with 2 replicas, prefers Pi 5 nodes, health checks - frontend-deployment.yaml - Frontend with 2 replicas, can run on any node, nginx-based - ingress.yaml - Traefik/NGINX ingress for API, Socket.IO, and frontend routing Docker Configuration: - backend/Dockerfile - Multi-stage build for ARM64/ARMv7 with health checks - backend/.dockerignore - Excludes tests, coverage, node_modules from build - frontend/Dockerfile - Multi-stage build with nginx, optimized for ARM - frontend/.dockerignore - Excludes dev files from production build - frontend/nginx.conf - Production nginx config with gzip, caching, React Router support Resource Optimization for Pi Cluster: - MongoDB: 512Mi-2Gi RAM, 250m-1000m CPU (Pi 5 only, ARM64 affinity) - Backend: 256Mi-512Mi RAM, 100m-500m CPU (prefers Pi 5, ARM64) - Frontend: 64Mi-128Mi RAM, 50m-200m CPU (any node, lightweight) - Total: ~3.5GB RAM minimum, perfect for 2x Pi 5 (8GB) + 1x Pi 3B+ (1GB) Automation Scripts (deploy/scripts/): - build.sh - Build multi-arch images (ARM64/ARMv7) and push to registry - deploy.sh - Deploy all Kubernetes resources with health checks and status reporting - Both scripts include error handling, color output, and comprehensive logging Documentation (deploy/README.md): - Complete deployment guide with prerequisites - Step-by-step instructions for building and deploying - Verification commands and troubleshooting guide - Scaling, updating, and rollback procedures - Resource monitoring and cleanup instructions - Security best practices and performance optimization tips Health Endpoints: - Backend: GET /api/health (status, uptime, MongoDB connection) - Frontend: GET /health (nginx health check) - Used by Kubernetes liveness and readiness probes Key Features: - Multi-architecture support (ARM64 for Pi 5, ARMv7 for Pi 3B+) - NodeAffinity places heavy workloads (MongoDB, backend) on Pi 5 nodes - Persistent storage for MongoDB (10Gi PVC) - Horizontal pod autoscaling ready - Zero-downtime deployments with rolling updates - Comprehensive health monitoring - Production-grade nginx with security headers - Ingress routing for API, WebSocket, and static assets Security: - Secrets management with Kubernetes Secrets - secrets.yaml excluded from Git (.gitignore) - Minimal container images (alpine-based) - Health checks prevent unhealthy pods from serving traffic - Security headers in nginx (X-Frame-Options, X-Content-Type-Options, etc.) Usage: 1. Build images: ./deploy/scripts/build.sh 2. Configure secrets: cp deploy/k8s/secrets.yaml.example deploy/k8s/secrets.yaml 3. Deploy: ./deploy/scripts/deploy.sh 4. Monitor: kubectl get all -n adopt-a-street 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
152 lines
4.2 KiB
JavaScript
152 lines
4.2 KiB
JavaScript
require("dotenv").config();
|
|
const express = require("express");
|
|
const mongoose = require("mongoose");
|
|
const cors = require("cors");
|
|
const http = require("http");
|
|
const socketio = require("socket.io");
|
|
const helmet = require("helmet");
|
|
const rateLimit = require("express-rate-limit");
|
|
const { errorHandler } = require("./middleware/errorHandler");
|
|
const socketAuth = require("./middleware/socketAuth");
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const io = socketio(server, {
|
|
cors: {
|
|
origin: process.env.FRONTEND_URL || "http://localhost:3000",
|
|
methods: ["GET", "POST"],
|
|
credentials: true,
|
|
},
|
|
});
|
|
const port = process.env.PORT || 5000;
|
|
|
|
// Security Headers - Helmet
|
|
app.use(helmet());
|
|
|
|
// CORS Configuration
|
|
app.use(
|
|
cors({
|
|
origin: process.env.FRONTEND_URL || "http://localhost:3000",
|
|
credentials: true,
|
|
}),
|
|
);
|
|
|
|
// Body Parser
|
|
app.use(express.json());
|
|
|
|
// Rate Limiting for Auth Routes (5 requests per 15 minutes)
|
|
const authLimiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
max: 5, // 5 requests per windowMs
|
|
message: {
|
|
success: false,
|
|
error: "Too many authentication attempts, please try again later",
|
|
},
|
|
standardHeaders: true,
|
|
legacyHeaders: false,
|
|
});
|
|
|
|
// General API Rate Limiting (100 requests per 15 minutes)
|
|
const apiLimiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
max: 100, // 100 requests per windowMs
|
|
message: {
|
|
success: false,
|
|
error: "Too many requests, please try again later",
|
|
},
|
|
standardHeaders: true,
|
|
legacyHeaders: false,
|
|
});
|
|
|
|
// MongoDB Connection
|
|
mongoose
|
|
.connect(process.env.MONGO_URI, {
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true,
|
|
})
|
|
.then(() => console.log("MongoDB connected"))
|
|
.catch((err) => console.log("MongoDB connection error:", err));
|
|
|
|
// Socket.IO Authentication Middleware
|
|
io.use(socketAuth);
|
|
|
|
// Socket.IO Setup with Authentication
|
|
io.on("connection", (socket) => {
|
|
console.log(`Client connected: ${socket.user.id}`);
|
|
|
|
socket.on("joinEvent", (eventId) => {
|
|
socket.join(`event_${eventId}`);
|
|
console.log(`User ${socket.user.id} joined event ${eventId}`);
|
|
});
|
|
|
|
socket.on("joinPost", (postId) => {
|
|
socket.join(`post_${postId}`);
|
|
console.log(`User ${socket.user.id} joined post ${postId}`);
|
|
});
|
|
|
|
socket.on("eventUpdate", (data) => {
|
|
io.to(`event_${data.eventId}`).emit("update", data.message);
|
|
});
|
|
|
|
socket.on("disconnect", () => {
|
|
console.log(`Client disconnected: ${socket.user.id}`);
|
|
});
|
|
});
|
|
|
|
// Make io available to routes
|
|
app.set("io", io);
|
|
|
|
// Routes
|
|
const authRoutes = require("./routes/auth");
|
|
const streetRoutes = require("./routes/streets");
|
|
const taskRoutes = require("./routes/tasks");
|
|
const postRoutes = require("./routes/posts");
|
|
const commentsRoutes = require("./routes/comments");
|
|
const eventRoutes = require("./routes/events");
|
|
const rewardRoutes = require("./routes/rewards");
|
|
const reportRoutes = require("./routes/reports");
|
|
const badgesRoutes = require("./routes/badges");
|
|
const aiRoutes = require("./routes/ai");
|
|
const paymentRoutes = require("./routes/payments");
|
|
const userRoutes = require("./routes/users");
|
|
|
|
// Apply rate limiters
|
|
app.use("/api/auth/register", authLimiter);
|
|
app.use("/api/auth/login", authLimiter);
|
|
app.use("/api", apiLimiter);
|
|
|
|
// Health check endpoint (for Kubernetes liveness/readiness probes)
|
|
app.get("/api/health", (req, res) => {
|
|
res.status(200).json({
|
|
status: "healthy",
|
|
timestamp: new Date().toISOString(),
|
|
uptime: process.uptime(),
|
|
mongodb: mongoose.connection.readyState === 1 ? "connected" : "disconnected",
|
|
});
|
|
});
|
|
|
|
// Routes
|
|
app.use("/api/auth", authRoutes);
|
|
app.use("/api/streets", streetRoutes);
|
|
app.use("/api/tasks", taskRoutes);
|
|
app.use("/api/posts", postRoutes);
|
|
app.use("/api/posts", commentsRoutes); // Comments are nested under posts
|
|
app.use("/api/events", eventRoutes);
|
|
app.use("/api/rewards", rewardRoutes);
|
|
app.use("/api/reports", reportRoutes);
|
|
app.use("/api/badges", badgesRoutes);
|
|
app.use("/api/ai", aiRoutes);
|
|
app.use("/api/payments", paymentRoutes);
|
|
app.use("/api/users", userRoutes);
|
|
|
|
app.get("/", (req, res) => {
|
|
res.send("Street Adoption App Backend");
|
|
});
|
|
|
|
// Error Handler Middleware (must be last)
|
|
app.use(errorHandler);
|
|
|
|
server.listen(port, () => {
|
|
console.log(`Server running on port ${port}`);
|
|
});
|