A Cloudflare Workers implementation of the electerm sync server using D1 as the database backend. This provides a lightweight, scalable, and serverless alternative to the Node.js implementation.
- Serverless Architecture: Runs on Cloudflare's global edge network
- D1 Database: SQLite-compatible database that's distributed across Cloudflare's network
- JWT Authentication: Secure authentication using JSON Web Tokens
- Simple API: Compatible with electerm's sync protocol
- Node.js (v20 or later)
- A Cloudflare account with Workers and D1 enabled
git clone https://github.com/your-username/electerm-sync-cloudflare.git
cd electerm-sync-cloudflare
npm install
npm i -g wranglerwrangler d1 create electerm_sync_dbTake note of the database ID that appears in the output. You'll need to update this in your wrangler.toml file.
wrangler d1 execute electerm_sync_db --file=./bin/schema.sql
wrangler d1 execute electerm_sync_db --file=./bin/schema.sql --remoteEdit the wrangler.toml file and update the following:
YOUR_D1_DATABASE_ID: Replace with the database ID you received when creating the D1 database
npm run deployThen need in cloudflare worker setting to set JWT_SECRET and JWT_USERS (user1,user2...)
The server provides the following API endpoints:
GET /test: Basic health check endpointGET /api/sync: Retrieve user data (requires JWT authentication)PUT /api/sync: Update user data (requires JWT authentication)POST /api/sync: Test authentication (requires JWT authentication)
This service uses JWT (JSON Web Tokens) for authentication. All /api/sync endpoints require a valid JWT token in the Authorization header:
Authorization: Bearer YOUR_JWT_TOKEN
The JWT token should contain an id claim that matches one of the allowed user IDs specified in the JWT_USERS environment variable.
To run the server locally for development:
npm run devnpm run testIn Electerm, configure the sync feature to use your deployed Cloudflare worker URL:
- Go to Settings > Sync -> Custom
- Enter the following settings:
- Sync server: Your Cloudflare worker URL (e.g.,
https://your-subdomain.workers.dev/api/async) - JWT secret: the JWT_SECRET secret for authentication which configured in cloudflare worker setting
- User ID: one of the JWT_USERS secret which configured in cloudflare worker setting
- Sync server: Your Cloudflare worker URL (e.g.,
https://github.com/electerm/electerm/wiki/Custom-sync-server
MIT