修改包依赖名称

This commit is contained in:
2025-11-30 13:15:13 +08:00
parent ea4e2e305d
commit 719238b1f0
33 changed files with 701 additions and 98 deletions

View File

@@ -16,7 +16,7 @@
### 安装
```bash
go get github.com/go-common
go get git.toowon.com/jimmy/go-commom
```
### 使用示例
@@ -24,7 +24,7 @@ go get github.com/go-common
#### 数据库迁移
```go
import "github.com/go-common/migration"
import "git.toowon.com/jimmy/go-commom/migration"
migrator := migration.NewMigrator(db)
migrator.AddMigration(migration.Migration{
@@ -40,7 +40,7 @@ migrator.Up()
#### 日期转换
```go
import "github.com/go-common/datetime"
import "git.toowon.com/jimmy/go-commom/datetime"
datetime.SetDefaultTimeZone(datetime.AsiaShanghai)
now := datetime.Now()
@@ -50,7 +50,7 @@ str := datetime.FormatDateTime(now)
#### HTTP响应
```go
import "github.com/go-common/http"
import "git.toowon.com/jimmy/go-commom/http"
http.Success(w, data)
http.SuccessPage(w, list, total, page, pageSize)
@@ -61,8 +61,8 @@ http.Error(w, 1001, "业务错误")
```go
import (
"github.com/go-common/middleware"
"github.com/go-common/http"
"git.toowon.com/jimmy/go-commom/middleware"
"git.toowon.com/jimmy/go-commom/http"
)
// CORS + 时区中间件
@@ -79,7 +79,7 @@ timezone := http.GetTimezone(r)
#### 配置管理
```go
import "github.com/go-common/config"
import "git.toowon.com/jimmy/go-commom/config"
// 从文件加载配置
cfg, err := config.LoadFromFile("./config.json")

View File

@@ -103,7 +103,7 @@
### 1. 加载配置文件
```go
import "github.com/go-common/config"
import "git.toowon.com/jimmy/go-commom/config"
// 从文件加载配置(支持绝对路径和相对路径)
config, err := config.LoadFromFile("/path/to/config.json")
@@ -167,7 +167,7 @@ addr := config.GetRedisAddr()
corsConfig := config.GetCORS()
// 使用CORS中间件
import "github.com/go-common/middleware"
import "git.toowon.com/jimmy/go-commom/middleware"
chain := middleware.NewChain(
middleware.CORS(corsConfig),
)
@@ -289,8 +289,8 @@ package main
import (
"log"
"github.com/go-common/config"
"github.com/go-common/middleware"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/middleware"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

View File

@@ -19,7 +19,7 @@
### 1. 设置默认时区
```go
import "github.com/go-common/datetime"
import "git.toowon.com/jimmy/go-commom/datetime"
// 设置默认时区为上海时区
err := datetime.SetDefaultTimeZone(datetime.AsiaShanghai)
@@ -401,7 +401,7 @@ import (
"log"
"time"
"github.com/go-common/datetime"
"git.toowon.com/jimmy/go-commom/datetime"
)
func main() {
@@ -428,7 +428,7 @@ import (
"fmt"
"log"
"github.com/go-common/datetime"
"git.toowon.com/jimmy/go-commom/datetime"
)
func main() {

View File

@@ -18,8 +18,8 @@
```go
import (
"github.com/go-common/config"
"github.com/go-common/email"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/email"
)
// 从配置加载
@@ -107,7 +107,7 @@ if err != nil {
### 5. 使用Message结构发送便捷方法
```go
import "github.com/go-common/email"
import "git.toowon.com/jimmy/go-commom/email"
msg := &email.Message{
To: []string{"to@example.com"},
@@ -295,8 +295,8 @@ package main
import (
"log"
"github.com/go-common/config"
"github.com/go-common/email"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/email"
)
func main() {

View File

@@ -49,7 +49,7 @@ HTTP Restful工具提供了标准化的HTTP请求和响应处理功能包含
```go
import (
"net/http"
"github.com/go-common/http"
"git.toowon.com/jimmy/go-commom/http"
)
// 简单成功响应data为nil
@@ -198,7 +198,7 @@ package main
import (
"net/http"
"github.com/go-common/http"
"git.toowon.com/jimmy/go-commom/http"
)
// 用户列表接口

View File

@@ -29,7 +29,7 @@ CORS中间件用于处理跨域资源共享支持
```go
import (
"net/http"
"github.com/go-common/middleware"
"git.toowon.com/jimmy/go-commom/middleware"
)
func main() {
@@ -50,7 +50,7 @@ func main() {
```go
import (
"net/http"
"github.com/go-common/middleware"
"git.toowon.com/jimmy/go-commom/middleware"
)
func main() {
@@ -131,9 +131,9 @@ corsHandler := middleware.CORS(corsConfig)(handler)
```go
import (
"net/http"
"github.com/go-common/middleware"
"github.com/go-common/http"
"github.com/go-common/datetime"
"git.toowon.com/jimmy/go-commom/middleware"
"git.toowon.com/jimmy/go-commom/http"
"git.toowon.com/jimmy/go-commom/datetime"
)
func handler(w http.ResponseWriter, r *http.Request) {
@@ -162,8 +162,8 @@ func main() {
```go
import (
"net/http"
"github.com/go-common/middleware"
"github.com/go-common/datetime"
"git.toowon.com/jimmy/go-commom/middleware"
"git.toowon.com/jimmy/go-commom/datetime"
)
func handler(w http.ResponseWriter, r *http.Request) {
@@ -183,8 +183,8 @@ func main() {
```go
import (
"net/http"
"github.com/go-common/http"
"github.com/go-common/datetime"
"git.toowon.com/jimmy/go-commom/http"
"git.toowon.com/jimmy/go-commom/datetime"
)
func GetUserList(w http.ResponseWriter, r *http.Request) {
@@ -240,7 +240,7 @@ X-Timezone: Asia/Shanghai
```go
import (
"net/http"
"github.com/go-common/middleware"
"git.toowon.com/jimmy/go-commom/middleware"
)
func handler(w http.ResponseWriter, r *http.Request) {
@@ -282,9 +282,9 @@ import (
"log"
"net/http"
"github.com/go-common/middleware"
"github.com/go-common/http"
"github.com/go-common/datetime"
"git.toowon.com/jimmy/go-commom/middleware"
"git.toowon.com/jimmy/go-commom/http"
"git.toowon.com/jimmy/go-commom/datetime"
)
func apiHandler(w http.ResponseWriter, r *http.Request) {
@@ -331,8 +331,8 @@ package main
import (
"net/http"
"github.com/go-common/middleware"
"github.com/go-common/http"
"git.toowon.com/jimmy/go-commom/middleware"
"git.toowon.com/jimmy/go-commom/http"
)
func main() {

View File

@@ -21,7 +21,7 @@
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"github.com/go-common/migration"
"git.toowon.com/jimmy/go-commom/migration"
)
// 初始化数据库连接
@@ -135,7 +135,46 @@ for _, s := range status {
}
```
### 6. 生成迁移版本号
### 6. 重置迁移
#### 方式一:仅清空迁移记录(不回滚数据库变更)
```go
// 直接调用(需要传入确认标志)
err := migrator.Reset(true)
if err != nil {
log.Fatal(err)
}
// 交互式确认(推荐,会提示警告信息)
err := migrator.ResetWithConfirm()
if err != nil {
log.Fatal(err)
}
```
#### 方式二:回滚所有迁移并清空记录
```go
// 直接调用(需要传入确认标志)
err := migrator.ResetAll(true)
if err != nil {
log.Fatal(err)
}
// 交互式确认(推荐,会提示警告信息)
err := migrator.ResetAllWithConfirm()
if err != nil {
log.Fatal(err)
}
```
**注意:**
- `Reset()` 仅清空迁移记录表,不会回滚已执行的数据库变更
- `ResetAll()` 会回滚所有已应用的迁移执行Down函数然后清空记录
- 交互式方法会显示详细的警告信息,需要输入确认文本才能执行
### 7. 生成迁移版本号
```go
// 生成基于时间戳的版本号
@@ -194,6 +233,44 @@ type Migration struct {
**返回:** 迁移状态列表和错误信息
#### Reset(confirm bool) error
重置所有迁移记录(仅清空记录表,不回滚数据库变更)。
**参数:**
- `confirm`: 确认标志必须为true才能执行
**返回:** 错误信息
**注意:** 此操作只清空迁移记录不会回滚已执行的迁移操作。如果需要回滚迁移请先使用Down方法逐个回滚。
#### ResetWithConfirm() error
交互式重置所有迁移记录(带确认提示)。
**返回:** 错误信息
**说明:** 会显示警告信息,需要输入"RESET"(全大写)才能执行。
#### ResetAll(confirm bool) error
重置所有迁移并回滚所有已应用的迁移。
**参数:**
- `confirm`: 确认标志必须为true才能执行
**返回:** 错误信息
**注意:** 此操作会回滚所有已应用的迁移执行Down函数然后清空迁移记录。操作不可逆请谨慎使用。
#### ResetAllWithConfirm() error
交互式重置所有迁移并回滚(带确认提示)。
**返回:** 错误信息
**说明:** 会显示警告信息,需要输入"RESET ALL"(全大写)才能执行。
### MigrationStatus 结构体
```go
@@ -230,11 +307,16 @@ type MigrationStatus struct {
## 注意事项
1. 迁移版本号必须唯一,建议使用时间戳格式
2. 迁移操作在事务中执行,失败会自动回滚
3. 迁移记录表会自动创建,无需手动创建
4. 如果迁移文件没有对应的down文件回滚操作会失败
5. 迁移按版本号升序执行,确保顺序正确
1. **迁移版本号**必须唯一,建议使用时间戳格式
2. **事务支持**迁移操作在事务中执行,失败会自动回滚
3. **自动创建表**迁移记录表会自动创建,无需手动创建
4. **回滚文件**如果迁移文件没有对应的down文件回滚操作会失败
5. **执行顺序**迁移按版本号升序执行,确保顺序正确
6. **重置操作**
- `Reset()` 只清空迁移记录,不会回滚数据库变更
- `ResetAll()` 会回滚所有迁移,操作不可逆
- 建议使用交互式方法(`ResetWithConfirm``ResetAllWithConfirm`)以确保安全
- 在生产环境使用重置功能前,请确保已备份数据库
## 示例

View File

@@ -19,8 +19,8 @@
```go
import (
"github.com/go-common/config"
"github.com/go-common/sms"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/sms"
)
// 从配置加载
@@ -102,7 +102,7 @@ if err != nil {
### 5. 使用SendRequest结构发送便捷方法
```go
import "github.com/go-common/sms"
import "git.toowon.com/jimmy/go-commom/sms"
req := &sms.SendRequest{
PhoneNumbers: []string{"13800138000", "13900139000"},
@@ -329,8 +329,8 @@ import (
"fmt"
"log"
"github.com/go-common/config"
"github.com/go-common/sms"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/sms"
)
func main() {

View File

@@ -21,8 +21,8 @@
```go
import (
"github.com/go-common/config"
"github.com/go-common/storage"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/storage"
)
// 加载配置
@@ -50,7 +50,7 @@ if err != nil {
import (
"context"
"os"
"github.com/go-common/storage"
"git.toowon.com/jimmy/go-commom/storage"
)
// 打开文件
@@ -81,7 +81,7 @@ fmt.Printf("File URL: %s\n", url)
```go
import (
"net/http"
"github.com/go-common/storage"
"git.toowon.com/jimmy/go-commom/storage"
)
// 创建上传处理器
@@ -125,7 +125,7 @@ curl -X POST http://localhost:8080/upload \
```go
import (
"net/http"
"github.com/go-common/storage"
"git.toowon.com/jimmy/go-commom/storage"
)
// 创建代理查看处理器
@@ -144,7 +144,7 @@ GET /file?key=images/test.jpg
### 5. 生成对象键
```go
import "github.com/go-common/storage"
import "git.toowon.com/jimmy/go-commom/storage"
// 生成简单对象键
objectKey := storage.GenerateObjectKey("images/", "test.jpg")
@@ -288,9 +288,9 @@ import (
"log"
"net/http"
"github.com/go-common/config"
"github.com/go-common/middleware"
"github.com/go-common/storage"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/middleware"
"git.toowon.com/jimmy/go-commom/storage"
)
func main() {
@@ -344,8 +344,8 @@ import (
"log"
"os"
"github.com/go-common/config"
"github.com/go-common/storage"
"git.toowon.com/jimmy/go-commom/config"
"git.toowon.com/jimmy/go-commom/storage"
)
func main() {