Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Dec 18, 2024
2 parents 9f62997 + 01e9af2 commit 875b713
Show file tree
Hide file tree
Showing 104 changed files with 2,463 additions and 1,819 deletions.
107 changes: 61 additions & 46 deletions .github/workflows/build-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
branches:
- main
- dev
- feature/**
paths:
- 'src/BE/**'
- 'src/FE/**'
Expand Down Expand Up @@ -51,6 +52,35 @@ jobs:
name: chats-fe
path: ./src/FE/out

upload-fe-to-minio:
strategy:
matrix:
include:
- asset: chats-fe
needs: build-fe
runs-on: ubuntu-latest
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: ${{ matrix.asset }}
path: ${{ matrix.asset }}

- name: Compress artifacts into ZIP
run: |
zip -r ${{ matrix.asset }}.zip ${{ matrix.asset }}
shell: bash

- name: Configure MINIO Credentials
run: |
echo "AWS_ACCESS_KEY_ID=${{ secrets.MINIO_KEY }}" >> $GITHUB_ENV
echo "AWS_SECRET_ACCESS_KEY=${{ secrets.MINIO_SECRET }}" >> $GITHUB_ENV
echo "AWS_DEFAULT_REGION=us-east-1" >> $GITHUB_ENV
- name: Upload to Minio
run: |
aws --endpoint-url ${{ vars.MINIO_URL }} s3 cp ${{ matrix.asset }}.zip s3://chats/r${{ github.run_number }}/${{ matrix.asset }}.zip
build-primary-container:
needs: build-fe
runs-on: ubuntu-latest
Expand Down Expand Up @@ -201,13 +231,27 @@ jobs:
- name: build binary
run: |
dotnet publish ./src/BE/Chats.BE.csproj -c Release -o ./Publish ${{ matrix.args }} /p:DeleteExistingFiles=True
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.id }}
path: ./Publish

- name: Compress artifacts into ZIP
run: |
zip -r ${{ matrix.id }}.zip ./Publish
- name: Configure MINIO Credentials
run: |
echo "AWS_ACCESS_KEY_ID=${{ secrets.MINIO_KEY }}" >> $GITHUB_ENV
echo "AWS_SECRET_ACCESS_KEY=${{ secrets.MINIO_SECRET }}" >> $GITHUB_ENV
echo "AWS_DEFAULT_REGION=us-east-1" >> $GITHUB_ENV
- name: Upload to Minio
run: |
aws --endpoint-url ${{ vars.MINIO_URL }} s3 cp ${{ matrix.id }}.zip s3://chats/r${{ github.run_number }}/${{ matrix.id }}.zip
create-release:
if: github.ref == 'refs/heads/main'
needs: [docker-manifest, build-binaries, upload-minio-latest]
Expand Down Expand Up @@ -333,47 +377,9 @@ jobs:
with:
name: ${{ matrix.asset }}
upload-minio:
if: github.ref == 'refs/heads/main'
strategy:
matrix:
include:
- asset: chats
- asset: chats-fe
- asset: chats-win-x64
- asset: chats-linux-x64
- asset: chats-linux-arm64
- asset: chats-linux-musl-x64
- asset: chats-linux-musl-arm64
- asset: chats-osx-arm64
- asset: chats-osx-x64
needs: build-binaries
runs-on: ubuntu-latest
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: ${{ matrix.asset }}
path: ${{ matrix.asset }}
- name: Compress artifacts into ZIP
run: |
zip -r ${{ matrix.asset }}.zip ${{ matrix.asset }}
shell: bash
- name: Configure MINIO Credentials
run: |
echo "AWS_ACCESS_KEY_ID=${{ secrets.MINIO_KEY }}" >> $GITHUB_ENV
echo "AWS_SECRET_ACCESS_KEY=${{ secrets.MINIO_SECRET }}" >> $GITHUB_ENV
echo "AWS_DEFAULT_REGION=us-east-1" >> $GITHUB_ENV
- name: Upload to Minio
run: |
aws --endpoint-url ${{ vars.MINIO_URL }} s3 cp ${{ matrix.asset }}.zip s3://chats/r${{ github.run_number }}/${{ matrix.asset }}.zip
upload-minio-latest:
if: github.ref == 'refs/heads/main'
needs: upload-minio
needs: [build-binaries, upload-fe-to-minio]
runs-on: ubuntu-latest
steps:
- name: Configure MINIO Credentials
Expand All @@ -387,20 +393,29 @@ jobs:
aws --endpoint-url ${{ vars.MINIO_URL }} s3 rm s3://chats/latest --recursive
aws --endpoint-url ${{ vars.MINIO_URL }} s3 cp s3://chats/r${{ github.run_number }} s3://chats/latest --recursive
deploy-dev-stg:
deploy:
runs-on: ubuntu-latest
needs: build-primary-container
steps:
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: deploy dev/stg
- name: deploy dev/stg or feature
run: |
ssh -o StrictHostKeyChecking=no -p 22 ${{ secrets.SSH_TARGET }} << 'EOF'
docker pull ${{ vars.DOCKER_REGISTRY }}/${{ vars.DOCKER_NAMESPACE }}/chats:r${{ github.run_number }}-linux-x64
cd chats
sed -i "s/^TAG=.*/TAG=r${{ github.run_number }}-linux-x64/" ~/chats/dev.env
sed -i "s/^TAG=.*/TAG=r${{ github.run_number }}-linux-x64/" ~/chats/stg.env
./dev.sh && ./stg.sh
EOF
if [[ "${{ github.ref_name }}" == feature/* ]]; then
echo "Deploying feature branch: ${{ github.ref_name }}"
sed -i "s/^TAG=.*/TAG=r${{ github.run_number }}-linux-x64/" ~/chats/feature.env
docker compose --env-file=./feature.env --project-name chats-feature up -d --remove-orphans
else
echo "Deploying non-feature branch: ${{ github.ref_name }}"
sed -i "s/^TAG=.*/TAG=r${{ github.run_number }}-linux-x64/" ~/chats/dev.env
sed -i "s/^TAG=.*/TAG=r${{ github.run_number }}-linux-x64/" ~/chats/stg.env
docker compose --env-file=./dev.env --project-name chats-dev up -d --remove-orphans
docker compose --env-file=./stg.env --project-name chats-stg up -d --remove-orphans
fi
EOF
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Sdcb Chats 是一个强大且灵活的大语言模型前端,支持多种功能

## 快速开始

### 开发文档

Chats使用`C#`/`TypeScript`开发,有关如何编译Chats,请查看 [开发文档链接](./doc/zh-CN/build.md)

### Docker 部署

对于大多数用户而言,Docker 提供了最简单快速的部署方式。以下是一步到位的部署命令:
Expand Down
4 changes: 4 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Sdcb Chats is a powerful and flexible frontend for large language models that su

## Quick Start

### Development Documentation

Chats is developed using `C#`/`TypeScript`. For information on how to compile Chats, please refer to the [development documentation link](./doc/en-US/build.md).

### Docker Deployment

For most users, Docker provides the simplest and fastest way to deploy. Here is an all-in-one deployment command:
Expand Down
209 changes: 209 additions & 0 deletions doc/en-US/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Chats Development Guide

Welcome to Chats! This guide will help you quickly get started with development and understand how to use and configure the Chats project during the development phase. In the development phase, Chats adopts a front-end and back-end separation model, but in production, they will be combined into a single deployment package.

## Technology Stack

- **Backend:** Developed using C#/ASP.NET Core.
- **Frontend:** Developed using Next.js/React/TypeScript.
- **CSS:** Utilizes Tailwind CSS.

## Environment Requirements

- Git
- .NET SDK 8.0
- Node.js >= 20
- Visual Studio Code
- Visual Studio 2022 (optional but recommended)

## Obtaining the Code

First, clone the Chats code repository:

```bash
git clone https://github.com/sdcb/chats.git
```

## Joint Frontend and Backend Development

### Backend Development Guide

1. Use Visual Studio to open the solution:

Locate the `chats/Chats.sln` solution file in the root directory and open it. In Visual Studio, you'll see a website project named `Chats.BE`.

2. Run the project:

- Press F5 to run the project. The default configuration will check if the SQLite database file `chats.db` exists, and if not, it will automatically create it in the `./AppData` directory and initialize the database.
- The service will run on `http://localhost:5146`, providing API services. If running in development mode (`ASPNETCORE_ENVIRONMENT=Development`), Swagger UI will be available at `http://localhost:5146/swagger`.

3. Configuration file explanation:

The default configuration is located in `appsettings.json`, but it is strongly recommended to manage sensitive information using `userSecrets.json`. This can prevent accidental exposure of sensitive development configurations in the code base.

**Default configuration structure:**

```json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"FE_URL": "http://localhost:3001",
"ENCRYPTION_PASSWORD": "this is used for encrypt auto increment int id, please set as a random string.",
"DBType": "sqlite",
"ConnectionStrings": {
"ChatsDB": "Data Source=./AppData/chats.db"
}
}
```

**Configuration options explanation:**

- `Logging`: Manages log level; defaults to recording information level logs.
- `AllowedHosts`: Configures allowed host names; `*` accepts all hosts.
- `FE_URL`: Frontend URL, defaults to `http://localhost:3001`. The frontend can access the backend via CORS, with no extra configuration required for port 3000.
- `DBType`: Database type, supporting `sqlite` (default), `mssql`, and `postgresql`.
- `ConnectionStrings:ChatsDB`: Database `ADO.NET` connection string, varying based on `DBType`.
- `ENCRYPTION_PASSWORD`: Used for encrypting auto-increment integer IDs. In a production environment, it should be set to a random string to avoid direct exposure of IDs.

**Why use integer + encryption instead of GUID?**

Initially, the Chats project used GUIDs, but due to the following two reasons and careful consideration, it was switched to auto-increment integer IDs:
- GUID fields are larger, taking up more space;
- GUIDs as clustered indexes can lead to index fragmentation, affecting performance;


**Managing sensitive configurations:**

It's not recommended to directly modify configuration items in `appsettings.json`. Instead, use `userSecrets.json` via Visual Studio:

- Visual Studio: Right-click the `Chats.BE` project -> `Manage User Secrets`.
- CLI: Manage user secrets with the following commands.

```bash
dotnet user-secrets init
dotnet user-secrets set "key" "value"
dotnet user-secrets list
```

This helps avoid accidentally uploading sensitive information when committing code.

4. Running without Visual Studio:

Navigate to the backend directory:

```bash
cd ./chats/src/BE
dotnet run
```

### Frontend Development Guide

1. Navigate to the frontend directory:

```bash
cd ./chats/src/FE
```

2. Create a `.env.local` file and specify the backend URL:

```bash
echo "API_URL=http://localhost:5146" > .env.local
```

3. Install dependencies and run the development server:

```bash
npm i
npm run dev
```

After running, the frontend service will listen on `http://localhost:3000`. The backend already supports CORS configuration with no extra setup needed.

## Frontend Only Development

For frontend-focused development scenarios, we provide a pre-deployed backend development environment:

1. Clone the repository:

```bash
git clone https://github.com/sdcb/chats.git
```

2. Enter the frontend directory and specify the remote backend:

```bash
cd ./chats/src/FE
echo "API_URL=https://chats-dev.starworks.cc:88" > .env.local
```

This environment already allows cross-origin access behavior from http://localhost:3000.

3. Install dependencies and run:

```bash
npm i
npm run dev
```

### Notes

To simulate a production build process, execute:

```bash
npm run build
```

This command will generate an `./out` folder in the current directory containing all necessary static files.

## Backend Only Development

For backend-focused development scenarios, you can use packaged frontend files:

1. Clone the repository and navigate to the backend directory:

```bash
git clone https://github.com/sdcb/chats.git
cd ./chats/src/BE
```

2. Download and extract frontend static files into `wwwroot`:

**On Linux:**

```bash
curl -O https://github.com/sdcb/chats/releases/latest/download/chats-fe.zip
unzip chats-fe.zip
cp -r chats-fe/* wwwroot/
```

**On Windows:**

```powershell
Invoke-WebRequest -Uri "https://github.com/sdcb/chats/releases/latest/download/chats-fe.zip" -OutFile "chats-fe.zip"
Expand-Archive -Path "chats-fe.zip" -DestinationPath "."
Copy-Item -Path ".\chats-fe\*" -Destination ".\wwwroot" -Recurse -Force
```

### Note
1. I have also uploaded the above https://github.com/sdcb/chats/releases/latest/download/chats-fe.zip to my personal Minio file server at: http://io.starworks.cc:88/chats/latest/chats-fe.zip

If downloading directly from GitHub is too slow, you can use this address instead.

2. The attached `chats-fe.zip` is automatically generated by GitHub Actions when code is merged into the `main` branch, not triggered for `dev` branch merges.

3. Run the backend:

```bash
dotnet run
```

Alternatively, open `Chats.sln` in Visual Studio and run the `Chats.BE` project.

Once running, visiting `http://localhost:5146/login` will directly take you to the Chats login page, realizing a deployment mode where front-end and back-end are not separated.

I hope this guide will assist you in successfully developing the Chats project. If you have any questions, please refer to the documentation in the source code or create an issue at https://github.com/sdcb/chats to receive support.
Loading

0 comments on commit 875b713

Please sign in to comment.