fix(docs): fix config toml

This commit is contained in:
2026-02-13 16:06:56 +08:00
parent 7ef61145f6
commit fb30d5da1a
4 changed files with 57 additions and 43 deletions

View File

@@ -37,13 +37,21 @@ cargo run
**使用 MySQL/PostgreSQL** **使用 MySQL/PostgreSQL**
复制对应的配置文件并修改 使用环境变量配置或创建本地配置文件:
**方式一:使用环境变量(推荐)**
```bash ```bash
# 使用 MySQL # 使用 MySQL
cp config/development.mysql.toml config/local.toml DATABASE_TYPE=mysql DATABASE_HOST=localhost DATABASE_PORT=3306 DATABASE_USER=root DATABASE_PASSWORD=your-password DATABASE_DATABASE=web_template_dev cargo run
# 或使用 PostgreSQL # 或使用 PostgreSQL
cp config/development.postgresql.toml config/local.toml DATABASE_TYPE=postgresql DATABASE_HOST=localhost DATABASE_PORT=5432 DATABASE_USER=postgres DATABASE_PASSWORD=your-password DATABASE_DATABASE=web_template_dev cargo run
```
**方式二:创建本地配置文件**
```bash
# 复制开发环境配置
cp config/development.toml config/local.toml
# 编辑 config/local.toml修改数据库连接信息 # 编辑 config/local.toml修改数据库连接信息
# 然后运行 # 然后运行
@@ -98,18 +106,14 @@ curl -X POST http://localhost:3000/auth/register \
使用 `config/` 目录下的配置文件: 使用 `config/` 目录下的配置文件:
```bash ```bash
# SQLite默认 # SQLite默认,修改 config/development.toml 中的 database.type 为 "sqlite"
cargo run cargo run
# MySQL # MySQL(修改 config/development.toml 中的 database.type 为 "mysql"
cp config/development.mysql.toml config/local.toml cargo run
# 编辑 config/local.toml
cargo run -- -c config/local.toml
# PostgreSQL # PostgreSQL(修改 config/development.toml 中的 database.type 为 "postgresql"
cp config/development.postgresql.toml config/local.toml cargo run
# 编辑 config/local.toml
cargo run -- -c config/local.toml
``` ```
**生产环境** **生产环境**

View File

@@ -400,17 +400,16 @@ cargo run -- -c config/local.toml
**重要**:本项目不支持 .env 文件。开发环境请使用 `config/` 目录下的 toml 配置文件。 **重要**:本项目不支持 .env 文件。开发环境请使用 `config/` 目录下的 toml 配置文件。
复制并修改 MySQL 配置文件: 编辑 MySQL 配置文件:
```bash ```bash
# 复制 MySQL 配置模板 # 编辑开发环境配置文件
cp config/development.mysql.toml config/local.toml nano config/development.toml
# 编辑 config/local.toml修改数据库连接信息 # 设置 database.type = "mysql" 并修改连接信息
nano config/local.toml
# 运行 # 运行
cargo run -- -c config/local.toml cargo run
``` ```
或使用环境变量(适用于 Docker/Kubernetes 或使用环境变量(适用于 Docker/Kubernetes

View File

@@ -92,22 +92,15 @@ cargo run
#### 方式二:使用 MySQL/PostgreSQL #### 方式二:使用 MySQL/PostgreSQL
**步骤 1**复制对应的配置文件 **步骤 1**修改配置文件
```bash 编辑 `config/development.toml`,修改数据库类型和连接信息:
# 使用 MySQL
cp config/development.mysql.toml config/local.toml
# 或使用 PostgreSQL
cp config/development.postgresql.toml config/local.toml
```
**步骤 2**:修改配置文件
编辑 `config/local.toml`,修改数据库连接信息:
```toml ```toml
[database] [database]
# 修改数据库类型mysql, postgresql 或 sqlite
type = "mysql" # 或 "postgresql"
# MySQL 配置 # MySQL 配置
host = "localhost" host = "localhost"
port = 3306 port = 3306
@@ -116,6 +109,7 @@ password = "your-password"
database = "web_template_dev" database = "web_template_dev"
# 或 PostgreSQL 配置 # 或 PostgreSQL 配置
# type = "postgresql"
# host = "localhost" # host = "localhost"
# port = 5432 # port = 5432
# user = "postgres" # user = "postgres"
@@ -123,11 +117,10 @@ database = "web_template_dev"
# database = "web_template_dev" # database = "web_template_dev"
``` ```
**步骤 3**:运行项目 **步骤 2**:运行项目
```bash ```bash
# 使用指定配置文件运行 cargo run
cargo run -- -c config/local.toml
``` ```
#### 方式三:通过环境变量覆盖(适用于 Docker/Kubernetes #### 方式三:通过环境变量覆盖(适用于 Docker/Kubernetes
@@ -171,11 +164,16 @@ cargo run
**选项 1**:修改配置文件 **选项 1**:修改配置文件
```bash 编辑 `config/development.toml`,设置数据库类型为 `mysql` 并修改连接信息:
# 复制 MySQL 配置模板
cp config/development.mysql.toml config/local.toml
# 编辑 config/local.toml修改数据库连接信息 ```toml
[database]
type = "mysql"
host = "localhost"
port = 3306
user = "root"
password = "your-password"
database = "web_template_dev"
``` ```
**选项 2**:使用环境变量 **选项 2**:使用环境变量
@@ -198,7 +196,21 @@ cargo run
**适用场景**:需要高级数据库功能的应用 **适用场景**:需要高级数据库功能的应用
**配置方法**与 MySQL 类似,使用 `config/development.postgresql.toml` 或环境变量 **配置方法**
编辑 `config/development.toml`,设置数据库类型为 `postgresql` 并修改连接信息:
```toml
[database]
type = "postgresql"
host = "localhost"
port = 5432
user = "postgres"
password = "your-password"
database = "web_template_dev"
```
或使用环境变量(格式与 MySQL 相同)。
### 认证配置 ### 认证配置
@@ -243,7 +255,8 @@ cargo run
**使用指定配置文件** **使用指定配置文件**
```bash ```bash
cargo run -- -c config/development.mysql.toml # 修改 config/development.toml 后运行
cargo run
``` ```
**使用环境变量** **使用环境变量**

View File

@@ -99,9 +99,7 @@ web-rust-template/
├── config/ # 配置文件目录 ├── config/ # 配置文件目录
│ ├── default.toml # 默认配置 │ ├── default.toml # 默认配置
│ ├── development.sqlite.toml # SQLite 开发环境配置 │ ├── development.toml # 开发环境配置(支持 MySQL/PostgreSQL/SQLite
│ ├── development.mysql.toml # MySQL 开发环境配置
│ ├── development.postgresql.toml # PostgreSQL 开发环境配置
│ └── production.toml # 生产环境配置 │ └── production.toml # 生产环境配置
├── sql/ # SQL 脚本 ├── sql/ # SQL 脚本