前端添加中英文切换
This commit is contained in:
@@ -150,3 +150,66 @@ This application is optimized for deployment on [Vercel](https://vercel.com/).
|
||||
- **GitHub**: [@AbhinayManikanti](https://github.com/abhinay-07)
|
||||
- **LinkedIn**: [Abhinay Manikanti](https://www.linkedin.com/in/abhinay-manikanti)
|
||||
- **Project Link**: [https://github.com/AbhinayManikanti/goal-planner](https://github.com/AbhinayManikanti/goal-planner)
|
||||
|
||||
|
||||
|
||||
这是前后端分开跑的项目:后端是 FastAPI(端口 8000),前端是 Vite(端口 3000)。项目根目录已经有 `.venv`,`backend\.env` 也已经写好了。
|
||||
|
||||
## 1. 准备 MySQL
|
||||
|
||||
用 phpStudy 把 MySQL 启动起来,然后建库(表会在后端第一次启动时自动创建):
|
||||
|
||||
```sql
|
||||
CREATE DATABASE goal_planner CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
```
|
||||
|
||||
确认 `backend\.env` 里的 `MYSQL_HOST`、`MYSQL_PORT`、`MYSQL_USER`、`MYSQL_PASSWORD` 和 phpStudy 里的 MySQL 一致。当前文件里数据库名是 `goal_planner`,用户是 `root`。
|
||||
|
||||
## 2. 启动后端
|
||||
|
||||
在 PowerShell 里执行。必须在 `backend` 目录启动,否则读不到 `.env`。
|
||||
|
||||
```powershell
|
||||
cd d:\phpstudy_pro\WWW\python-projects\goal-planner
|
||||
.\.venv\Scripts\python.exe -m pip install -r backend\requirements.txt
|
||||
cd backend
|
||||
..\.venv\Scripts\python.exe -m uvicorn main:app --host 127.0.0.1 --port 8000 --reload
|
||||
```
|
||||
|
||||
看到 `Uvicorn running on http://127.0.0.1:8000` 后,浏览器打开 http://127.0.0.1:8000/api/health ,应返回 `{"status":"ok"}`。
|
||||
|
||||
如果提示无法运行脚本,不要用 `Activate.ps1`,直接用上面的 `python.exe` 完整路径即可。
|
||||
|
||||
## 3. 启动前端
|
||||
|
||||
另开一个 PowerShell:
|
||||
|
||||
```powershell
|
||||
cd d:\phpstudy_pro\WWW\python-projects\goal-planner\frontend
|
||||
npm install
|
||||
npm run dev
|
||||
```
|
||||
|
||||
浏览器打开 http://localhost:3000 。需要 Node.js 18 或更高版本。
|
||||
|
||||
## 4. 让前端能打到后端
|
||||
|
||||
前端请求走的是 `/api-proxy`(给 Nginx 反代用的)。Vite 目前没有这条代理,只开两个服务的话,页面能打开,但登录和生成计划会 404。
|
||||
|
||||
在 `frontend\vite.config.ts` 的 `server` 里加上:
|
||||
|
||||
```ts
|
||||
server: {
|
||||
port: 3000,
|
||||
host: '0.0.0.0',
|
||||
proxy: {
|
||||
'/api-proxy': {
|
||||
target: 'http://127.0.0.1:8000',
|
||||
changeOrigin: true,
|
||||
rewrite: (path) => path.replace(/^\/api-proxy/, ''),
|
||||
},
|
||||
},
|
||||
},
|
||||
```
|
||||
|
||||
改完后重启 `npm run dev`。请求 `/api-proxy/api/...` 会被转到 `http://127.0.0.1:8000/api/...`。
|
||||
|
||||
Reference in New Issue
Block a user