# Chathub Server (Worker Version)
The Chathub Server is the backend service for the [Chathub](https://cnb.cool/tryandrun/web_dev/chat_hub/chathub) project. It provides an API for managing chat sessions and messages and is designed to be deployed on **Cloudflare Workers**, enabling a serverless, edge-based architecture.
## Prerequisites
- [Bun](https://bun.sh/) installed
- [Wrangler](https://developers.cloudflare.com/workers/wrangler/install-AND-update/) CLI installed
- A Cloudflare account
## Installation
Install the project dependencies using Bun:
```sh
bun install
To deploy the worker to Cloudflare, follow these steps:
Prepare the environment:
Ensure the public directory exists (required for static assets).
mkdir public
Start local development (Optional):
./scripts/dev_start. sh
Generate Types:
bunx wrangler types
Deploy to Cloudflare:
bunx wrangler deploy
Important Note: This worker depends on external services. You must deploy chathub, cf-worker-token-server, and cf-worker-totp concurrently to ensure the token and TOTP servers are accessible for the Chathub server to function correctly.
This project uses a middleware-based authentication system implemented with Hono framework.
Login Endpoint: POST /api/login
{ "totp": "<totp_code>" }{ "isValid": true, "token": "<session_token>" }Protected Routes:
POST /api/clear/:model - Clear chat history (uses authMiddleware)POST /api/stream/:model - Stream chat responses (uses authMiddlewareSSE)Authentication Method:
token field (frontend)Authorization: Bearer <token> header (standard HTTP authentication)src/middleware/auth.tsThe authentication system consists of:
authMiddleware: For JSON response routesauthMiddlewareSSE: For Server-Sent Events (SSE) streaming routestoken_verify() from src/utils.tsFor more details, see: