Skip to main content

API Server

The API Server is the main entry point for all client interactions with Peekaping. It provides a RESTful API, WebSocket connections for real-time updates, and serves as the authentication gateway.

Role & Responsibilities

The API Server handles:

  • HTTP API: RESTful endpoints for all CRUD operations
  • Authentication: User login, registration, JWT token management, and 2FA
  • Authorization: Role-based access control and API key validation
  • WebSocket Server: Real-time bidirectional communication with web clients
  • API Documentation: Auto-generated Swagger/OpenAPI documentation
  • Security: Brute force protection, rate limiting, CORS handling
  • Push Endpoint: Receives heartbeats from push-based monitors
  • Cleanup Tasks: Scheduled cleanup of old data

Architecture

Dependency Injection

The API server uses Uber's Dig for dependency injection, registering all modules and their dependencies at startup.

Event-Driven Communication

The API server subscribes to Redis pub/sub events and broadcasts them to connected WebSocket clients for real-time updates.

Environment Variables

Server Configuration

VariableTypeRequiredDefaultDescription
SERVER_PORTstringYes8034Port the API server listens on
CLIENT_URLstringYeshttp://localhost:3000Frontend URL for CORS configuration
MODEstringYesdevRuntime mode: dev, prod, or test
LOG_LEVELstringNoinfoLogging level: debug, info, warn, error
TZstringYesUTCTimezone for the server
SERVICE_NAMEstringYespeekaping:apiService identifier for logging and monitoring

Database Configuration

VariableTypeRequiredDefaultDescription
DB_TYPEstringYes-Database type: postgres, mysql, sqlite, mongo, mongodb
DB_HOSTstringConditional-Database host (not required for SQLite)
DB_PORTstringConditional-Database port (not required for SQLite)
DB_NAMEstringYes-Database name or SQLite file path
DB_USERstringConditional-Database username (not required for SQLite)
DB_PASSstringConditional-Database password (not required for SQLite)

Redis Configuration

VariableTypeRequiredDefaultDescription
REDIS_HOSTstringYesredisRedis server hostname
REDIS_PORTstringYes6379Redis server port
REDIS_PASSWORDstringNo""Redis password (if authentication enabled)
REDIS_DBintNo0Redis database number (0-15)

Queue Configuration

VariableTypeRequiredDefaultDescription
QUEUE_CONCURRENCYintNo128Maximum concurrent queue operations
PRODUCER_CONCURRENCYintNo10Concurrent producers for push endpoint (1-128)

Security Configuration

VariableTypeRequiredDefaultDescription
BRUTEFORCE_MAX_ATTEMPTSintNo20Maximum login attempts before lockout
BRUTEFORCE_WINDOWdurationNo1mTime window for counting failed attempts
BRUTEFORCE_LOCKOUTdurationNo1mLockout duration after max attempts

API Endpoints

Core Resources

The API server exposes the following endpoint groups:

  • /api/v1/auth - Authentication (login, register, logout, 2FA)
  • /api/v1/monitors - Monitor management
  • /api/v1/heartbeats - Heartbeat data retrieval
  • /api/v1/notification-channels - Notification channel configuration
  • /api/v1/status-pages - Status page management
  • /api/v1/proxies - Proxy configuration
  • /api/v1/settings - System settings
  • /api/v1/stats - Statistics and analytics
  • /api/v1/api-keys - API key management
  • /api/v1/tags - Monitor tagging
  • /api/v1/maintenances - Maintenance window management
  • /api/v1/health - Health check endpoint
  • /api/v1/push/:id - Push monitor heartbeat receiver

Swagger Documentation

API documentation is automatically generated and available at:

http://localhost:8034/swagger/index.html

WebSocket Connection

The API server provides WebSocket endpoints for real-time updates:

ws://localhost:8034/api/v1/ws

Authentication Methods

JWT Authentication

Header: Authorization: Bearer <token>

Used for web application authentication.

API Key Authentication

Header: X-API-Key: pk_<key>

Used for programmatic API access.

Health Check

The API server exposes a health check endpoint:

GET /api/v1/health

Response:

{
"status": "ok",
"timestamp": "2024-01-01T00:00:00Z"
}
  • Producer - Schedules monitor health checks
  • Worker - Executes health checks
  • Ingester - Processes health check results