A powerful Bun app for the Discord API (and later Microsoft Outlook) which securely manages private keys in both Github and the .env.local file.
Also see Frontend
- Core Components:
server.js: Main Bun server with request handling and session managementindex.js: Application entry point, initializes and starts the serverservices/discordFactory.js: Factory for creating Discord service instancesservices/discord.service.js: Core Discord API integration service.env[Optional]: Environment configuration (Discord tokens, server settings)
The backend uses Bun for high-performance JavaScript runtime execution. Key features include:
-
Authentication & Session Management
- Secure session handling with nanoid-generated unique IDs
- Configurable session timeout and cleanup
- Rate limiting per client IP
- Maximum concurrent users limit
-
Discord Integration
- Factory pattern for Discord service instances
- Comprehensive guild management capabilities
- Efficient message and channel handling
-
API Endpoints
-
Authentication
POST /api/auth/login: Create new Discord bot instance and session{ "token": "your-discord-token" }POST /api/auth/logout: Destroy bot instance and clear session
-
Discord Data
GET /api/members: Fetch all server membersGET /api/channels: Get all text channelsGET /api/messages?channelId={id}&limit={count}: Retrieve channel messages- Optional
limitparameter (default: 100)
- Optional
-
System
GET /_ah/health: Health check endpoint
-
-
Error Handling
- Comprehensive error management with appropriate status codes
- Request tracking with unique request IDs
- Detailed error logging for debugging
-
Security Features
- CORS configuration with allowlist support
- Rate limiting with configurable windows
- Request tracking and logging
- Session timeout management
MemberSense
│
├── backend
│ ├── node_modules
│ ├── src
│ │ ├── services
│ │ │ ├── discord.service.js
│ │ │ ├── discordFactory.js
│ │ │ └── index.js
│ │ └── server.js
│ ├── tests
│ │ ├── mocks
│ │ └── server.test.js
│ ├── .env
│ ├── package.json
│ └── bun.lockb
│
└── docs
├── DESIGNDOC.md
├── GUIDE.md
└── README.mdThe project includes a comprehensive test suite using Bun's built-in testing framework. Key test categories include:
-
Server Configuration Tests
- Environment variable handling
- Custom configuration override
- CORS setup validation
-
Authentication Tests
- Login flow validation
- Session management
- Concurrent user limits
- Token validation
-
Session Management Tests
- Session timeout handling
- Activity tracking
- Cleanup processes
- Bot instance lifecycle
-
Rate Limiting Tests
- Request tracking per IP
- Window-based limiting
- Retry-after header handling
-
API Error Handling Tests
- Invalid JSON handling
- Missing parameter validation
- Discord API error handling
- Authentication failures
To run the tests:
cd backend
bun testFor watch mode during development:
bun test --watch-
Clone and install
git clone https://github.com/yourusername/membersense.git cd membersense/backend bun install -
Configure Environment
Thebackend/.envfile is optional as all settings have defaults. You can override any of these values based on your needs:# Server Configuration PORT=8080 # Server port number ALLOWED_ORIGINS=* # CORS allowed origins, use comma for multiple # Discord Configuration DISCORD_BOT_TOKEN=your_token_here # Discord bot token # Security Settings MAX_CONCURRENT_USERS=100 # Maximum concurrent bot instances RATE_LIMIT_WINDOW=60000 # Rate limit window (60 seconds) RATE_LIMIT_MAX_REQUESTS=100 # Maximum requests per window # Session Management SESSION_TIMEOUT=1800000 # Session timeout (30 minutes) CLEANUP_INTERVAL=300000 # Cleanup interval (5 minutes)
-
Start Development Server
bun run src/server.js
-
Build the Image
# Build the image docker build -t your-image-name . # Run the container locally docker run -p 8080:8080 your-image-name # Container Management docker ps # List running containers docker stop your-container-name # Stop the container docker start your-container-name # Restart the container
-
Docker Compose (Development)
# docker-compose.yml version: '3.8' services: backend: build: . ports: - "8080:8080" env_file: .env volumes: - ./src:/app/src
docker-compose up --build
-
Setup Google Cloud
# Install Google Cloud CLI brew install google-cloud-sdk # macOS # Initialize and set project gcloud init gcloud config set project YOUR_PROJECT_ID
-
Configure Deployment
# Build and push to Google Container Registry gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/membersense-backend # Deploy to Cloud Run gcloud run deploy membersense-backend \ --image gcr.io/YOUR_PROJECT_ID/membersense-backend \ --platform managed \ --region us-central1 \ --allow-unauthenticated \ --port 8080
-
Environment Variables [Optional]
- Go to Google Cloud Console → Cloud Run
- Select your service
- Edit & Deploy New Revision
- Add environment variables from your
.envfile
-
Continuous Deployment [TBD]
-
Containerization
- Multi-stage Docker builds for optimization
- Separate production and development configurations
-
Cloud Deployment
- Cloud platform deployment support (AWS ECS, Google Cloud Run)
- CDN integration for static assets
- Environment-based configuration
-
Local Development
- Docker Compose for development environment
- Hot-reloading support
- Test suite integration
- WebSocket integration for real-time Discord updates
- Enhanced member role management
- Google Sheets API integration
- Redis caching layer implementation
- Multi-server support per user
- Expanded test coverage
- Performance monitoring and analytics