A Scalable backend service for monitoring website availability and alerting users when their websites go down.
This service allows users to register websites for monitoring at specified intervals. When a website becomes unavailable, the system automatically sends email notifications to the user. The application provides a complete REST API for user management and monitor configuration.
- User Authentication: Register, login, refresh tokens, and logout
- Website Monitoring: Create, view, and delete website monitors
- Real-time Monitoring: Scheduled pinging of websites at user-defined intervals
- Status Tracking: Record and track website availability over time
- Email Notifications: Automatic alerts when a monitored website goes down
- Framework: Express.js with TypeScript
- Database: PostgreSQL with Prisma ORM
- Authentication: JWT-based (access and refresh tokens)
- Messaging: Redis Pub/Sub for event handling
- Email: Nodemailer for sending notifications
- Scheduling: Node-cron for timed monitoring
-
Express Server (
server.ts)- Handles HTTP requests and API routes
- Manages CORS and cookie settings
- Entry point for the application
-
Authentication System (
userController.ts,auth.ts,jwt.ts)- User registration and login
- JWT token generation and validation
- Access and refresh token management
-
Monitor Management (
monitorController.ts)- Create, retrieve, and delete monitors
- Associate monitors with users
- Store monitor configurations
-
Watcher Service (
watcher.ts,pingService.ts)- Runs on a schedule using node-cron
- Checks which websites need to be pinged
- Performs HTTP requests to monitored URLs
- Records the results of each ping
- Detects and publishes status changes
-
Notification System
- Publisher (
publisher.ts)- Publishes status changes to Redis
- Subscriber (
subscriber.ts,emailService.ts)- Listens for status changes on Redis
- Sends email notifications when sites go down
- Publisher (
-
User Journey:
- User creates an account and logs in
- System issues access and refresh tokens
- User creates monitors for their websites
- User can view and manage their monitors
-
Monitoring Flow:
- Watcher service runs every second (configurable)
- For each monitor that needs checking:
- System pings the website
- Result is stored in the database
- If status changes (up→down or down→up), an event is published
-
Notification Flow:
- Subscriber listens for status change events
- When a site goes down, email service is triggered
- User receives notification at their configured email address
The system follows an event-driven architecture with loose coupling between components:
- API Layer: Handles user interactions and CRUD operations
- Worker Layer: Performs the monitoring tasks independently
- Event Layer: Connects components through Redis pub/sub
- Persistence Layer: Stores all data in PostgreSQL
This architecture allows different components to scale independently and ensures the monitoring and notification processes continue even if the API experiences issues.
You can quickly set up PostgreSQL and Redis using Docker:
docker run --name uptime-postgres -e POSTGRES_PASSWORD=yourpassword -e POSTGRES_USER=postgres -e POSTGRES_DB=uptime_monitor -p 5432:5432 -d postgres:latestdocker run --name uptime-redis -p 6379:6379 -d redis:latestThese commands will start PostgreSQL and Redis containers with the necessary configurations for the application.
- Clone the repository:
git clone uptime
cd uptime- Install dependencies:
npm install- Set up environment variables:
Create a .env file in the root directory with the following variables:
DATABASE_URL="postgresql://postgres:yourpassword@localhost:5432/uptime_monitor?schema=public"
PORT=3000
FRONTEND_URL="http://localhost:3001"
NAME="your-email@gmail.com" # For email notifications
PASS="your-email-password" # For email notifications
ACCESS_TOKEN_SECRET="your-secret-key-for-access-tokens"
REFRESH_TOKEN_SECRET="your-secret-key-for-refresh-tokens"
- Set up the database:
npx prisma migrate dev- Build the project:
npm run buildnpm run devnpm startnode dist/service/emailworker.jsnode dist/service/watcher.jsPOST /api/register- Register a new userPOST /api/user/login- Login and get access tokenPOST /api/refresh- Refresh access tokenPOST /api/logout- Logout and invalidate tokens
GET /api/users/:id- Get user details with monitors
POST /api/monitors- Create a new monitorGET /api/monitor- Get all monitors for the userGET /api/monitors/:id- Get a specific monitorDELETE /api/monitors/:id- Delete a monitor
- Passwords are hashed using bcrypt
- HTTP-only cookies for refresh tokens
- JWT-based authentication for API access
- CORS configuration for frontend access