logo
0
0
WeChat Login
worker-dev
# 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

Deployment

To deploy the worker to Cloudflare, follow these steps:

  1. Prepare the environment:
    Ensure the public directory exists (required for static assets).

    mkdir public
    
  2. Start local development (Optional):

    ./scripts/dev_start. sh
    
  3. Generate Types:

    bunx wrangler types
    
  4. 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.

Authentication

This project uses a middleware-based authentication system implemented with Hono framework.

Backend Authentication

  • Login Endpoint: POST /api/login

    • Request body: { "totp": "<totp_code>" }
    • Response: { "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:

    • Currently: Token passed in request body as token field (frontend)
    • Recommended: Use Authorization: Bearer <token> header (standard HTTP authentication)
    • Middleware location: src/middleware/auth.ts

Implementation Details

The authentication system consists of:

  • authMiddleware: For JSON response routes
  • authMiddlewareSSE: For Server-Sent Events (SSE) streaming routes
  • Both middlewares validate tokens using token_verify() from src/utils.ts

For more details, see:

Related Projects

About

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.

1.19 MiB
0 forks0 stars3 branches0 TagREADMEMIT license
Language
TypeScript95.3%
Shell4.7%