Merge pull request #276 from highkay/main

chore(docker): replace uvicorn with granian
This commit is contained in:
Chenyme authored and GitHub committed 2026-03-05 15:57:49 +08:00
commit fc2bf157a4
7 files changed
+838 -815

No files matched your search

+1 -1
View File
@@ -70,4 +70,4 @@ EXPOSE 8000
ENTRYPOINT ["/app/scripts/entrypoint.sh"]
CMD ["sh", "-c", "uvicorn main:app --host ${SERVER_HOST:-0.0.0.0} --port ${SERVER_PORT:-8000} --workers ${SERVER_WORKERS:-1}"]
CMD ["sh", "-c", "granian --interface asgi --host ${SERVER_HOST:-0.0.0.0} --port ${SERVER_PORT:-8000} --workers ${SERVER_WORKERS:-1} main:app"]
+2 -2
View File
@@ -25,8 +25,8 @@ services:
- sh
- -c
- >
uvicorn main:app --host "$${SERVER_HOST:-0.0.0.0}"
--port "$${SERVER_PORT:-8000}" --workers "$${SERVER_WORKERS:-1}"
granian --interface asgi --host "$${SERVER_HOST:-0.0.0.0}"
--port "$${SERVER_PORT:-8000}" --workers "$${SERVER_WORKERS:-1}" main:app
volumes:
- ./data:/app/data
- ./logs:/app/logs
+4 -2
View File
@@ -21,13 +21,15 @@ Grok2API rebuilt with **FastAPI**, fully aligned with the latest web call format
```bash
uv sync
uv run main.py
uv run granian --interface asgi --host 0.0.0.0 --port 8000 --workers 1 main:app
```
### Docker Compose
```bash
git clone https://github.com/chenyme/grok2api
cd grok2api
docker compose up -d
@@ -88,7 +90,7 @@ docker compose up -d
| `SERVER_HOST` | Bind address | `0.0.0.0` | `0.0.0.0` |
| `SERVER_PORT` | Server port | `8000` | `8000` |
| `HOST_PORT` | Host published port for Docker Compose | `8000` | `9000` |
| `SERVER_WORKERS` | Uvicorn worker count | `1` | `2` |
| `SERVER_WORKERS` | Server worker count | `1` | `2` |
| `SERVER_STORAGE_TYPE` | Storage type (`local`/`redis`/`mysql`/`pgsql`) | `local` | `pgsql` |
| `SERVER_STORAGE_URL` | Storage DSN (optional for local) | `""` | `postgresql+asyncpg://user:password@host:5432/db` |
+10 -20
View File
@@ -172,27 +172,17 @@ app = create_app()
if __name__ == "__main__":
import uvicorn
host = os.getenv("SERVER_HOST", "0.0.0.0")
port = int(os.getenv("SERVER_PORT", "8000"))
workers = int(os.getenv("SERVER_WORKERS", "1"))
# 平台检查
is_windows = platform.system() == "Windows"
# 自动降级
if is_windows and workers > 1:
logger.warning(
f"Windows platform detected. Multiple workers ({workers}) is not supported. "
"Using single worker instead."
)
workers = 1
uvicorn.run(
"main:app",
host=host,
port=port,
workers=workers,
log_level=os.getenv("LOG_LEVEL", "INFO").lower(),
log_level = os.getenv("LOG_LEVEL", "INFO").lower()
logger.error(
"Direct startup via `python main.py` is disabled. "
"Please run with Granian CLI to avoid Python wrapper issues."
)
logger.error(
"Use: uv run granian --interface asgi "
f"--host {host} --port {port} --workers {workers} "
f"--log-level {log_level} main:app"
)
raise SystemExit(1)
+1 -1
View File
@@ -13,6 +13,7 @@ dependencies = [
"cryptography>=46.0.5",
"curl-cffi>=0.13.0",
"fastapi>=0.119.0",
"granian>=2.7.2",
"greenlet>=3.3.1",
"livekit>=1.0.25",
"loguru>=0.7.3",
@@ -21,7 +22,6 @@ dependencies = [
"python-multipart>=0.0.21",
"redis>=6.4.0",
"sqlalchemy>=2.0.46",
"uvicorn>=0.37.0",
"websockets>=16.0",
]
+4 -2
View File
@@ -21,13 +21,15 @@
```bash
uv sync
uv run main.py
uv run granian --interface asgi --host 0.0.0.0 --port 8000 --workers 1 main:app
```
### Docker Compose
```bash
git clone https://github.com/chenyme/grok2api
cd grok2api
docker compose up -d
@@ -88,7 +90,7 @@ docker compose up -d
| `SERVER_HOST` | 服务监听地址 | `0.0.0.0` | `0.0.0.0` |
| `SERVER_PORT` | 服务端口 | `8000` | `8000` |
| `HOST_PORT` | Docker Compose 宿主机映射端口 | `8000` | `9000` |
| `SERVER_WORKERS` | Uvicorn worker 数量 | `1` | `2` |
| `SERVER_WORKERS` | 服务进程数量 | `1` | `2` |
| `SERVER_STORAGE_TYPE` | 存储类型(`local`/`redis`/`mysql`/`pgsql`) | `local` | `pgsql` |
| `SERVER_STORAGE_URL` | 存储连接串(local 时可为空) | `""` | `postgresql+asyncpg://user:password@host:5432/db` |
Generated
+816 -787
View File
File diff suppressed because it is too large. Load diff