初始版本,工具基础类
This commit is contained in:
100
docs/README.md
Normal file
100
docs/README.md
Normal file
@@ -0,0 +1,100 @@
|
||||
# GoCommon 工具类库文档
|
||||
|
||||
## 目录
|
||||
|
||||
- [数据库迁移工具](./migration.md) - 数据库版本管理和迁移
|
||||
- [日期转换工具](./datetime.md) - 日期时间处理和时区转换
|
||||
- [HTTP Restful工具](./http.md) - HTTP请求响应处理和分页
|
||||
- [中间件工具](./middleware.md) - CORS和时区处理中间件
|
||||
- [配置工具](./config.md) - 外部配置文件加载和管理
|
||||
- [存储工具](./storage.md) - 文件上传和查看(OSS、MinIO)
|
||||
- [邮件工具](./email.md) - SMTP邮件发送
|
||||
- [短信工具](./sms.md) - 阿里云短信发送
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 安装
|
||||
|
||||
```bash
|
||||
go get github.com/go-common
|
||||
```
|
||||
|
||||
### 使用示例
|
||||
|
||||
#### 数据库迁移
|
||||
|
||||
```go
|
||||
import "github.com/go-common/migration"
|
||||
|
||||
migrator := migration.NewMigrator(db)
|
||||
migrator.AddMigration(migration.Migration{
|
||||
Version: "20240101000001",
|
||||
Description: "create_users_table",
|
||||
Up: func(db *gorm.DB) error {
|
||||
return db.Exec("CREATE TABLE users ...").Error
|
||||
},
|
||||
})
|
||||
migrator.Up()
|
||||
```
|
||||
|
||||
#### 日期转换
|
||||
|
||||
```go
|
||||
import "github.com/go-common/datetime"
|
||||
|
||||
datetime.SetDefaultTimeZone(datetime.AsiaShanghai)
|
||||
now := datetime.Now()
|
||||
str := datetime.FormatDateTime(now)
|
||||
```
|
||||
|
||||
#### HTTP响应
|
||||
|
||||
```go
|
||||
import "github.com/go-common/http"
|
||||
|
||||
http.Success(w, data)
|
||||
http.SuccessPage(w, list, total, page, pageSize)
|
||||
http.Error(w, 1001, "业务错误")
|
||||
```
|
||||
|
||||
#### 中间件
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/go-common/middleware"
|
||||
"github.com/go-common/http"
|
||||
)
|
||||
|
||||
// CORS + 时区中间件
|
||||
chain := middleware.NewChain(
|
||||
middleware.CORS(),
|
||||
middleware.Timezone,
|
||||
)
|
||||
handler := chain.ThenFunc(yourHandler)
|
||||
|
||||
// 在处理器中获取时区
|
||||
timezone := http.GetTimezone(r)
|
||||
```
|
||||
|
||||
#### 配置管理
|
||||
|
||||
```go
|
||||
import "github.com/go-common/config"
|
||||
|
||||
// 从文件加载配置
|
||||
cfg, err := config.LoadFromFile("./config.json")
|
||||
|
||||
// 获取各种配置
|
||||
dsn, _ := cfg.GetDatabaseDSN()
|
||||
redisAddr := cfg.GetRedisAddr()
|
||||
corsConfig := cfg.GetCORS()
|
||||
```
|
||||
|
||||
## 版本
|
||||
|
||||
v1.0.0
|
||||
|
||||
## 许可证
|
||||
|
||||
MIT
|
||||
|
||||
Reference in New Issue
Block a user