mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-02 23:40:24 +08:00
* fix: 保存 LastResult 时截断字符串过长部分,以避免数据库报错 * refactor: 新增 entity.DbTaskBase 和 persistence.dbTaskBase, 用于实现数据库备份和恢复任务处理相关部分 * fix: aeskey变更后,解密密码出现数组越界访问错误 * fix: 时间属性为零值时,保存到 mysql 数据库报错 * refactor db.infrastructure.service.scheduler * feat: 实现立即备份功能 * refactor db.infrastructure.service.db_instance * refactor: 从数据库中获取数据库备份目录、mysql文件路径等配置信息 * fix: 数据库备份和恢复问题 * fix: 修改 .gitignore 文件,忽略数据库备份目录和数据库程序目录
77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package starter
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"mayfly-go/initialize"
|
|
"mayfly-go/internal/db/application"
|
|
"mayfly-go/pkg/config"
|
|
"mayfly-go/pkg/logx"
|
|
"mayfly-go/pkg/req"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func runWebServer(ctx context.Context) {
|
|
// 设置gin日志输出器
|
|
logOut := logx.GetConfig().GetLogOut()
|
|
gin.DefaultErrorWriter = logOut
|
|
gin.DefaultWriter = logOut
|
|
|
|
// 权限处理器
|
|
req.UseBeforeHandlerInterceptor(req.PermissionHandler)
|
|
// 日志处理器
|
|
req.UseAfterHandlerInterceptor(req.LogHandler)
|
|
// 设置日志保存函数
|
|
req.SetSaveLogFunc(initialize.InitSaveLogFunc())
|
|
|
|
srv := http.Server{
|
|
Addr: config.Conf.Server.GetPort(),
|
|
// 注册路由
|
|
Handler: initialize.InitRouter(),
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
logx.Info("Shutdown HTTP Server ...")
|
|
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
err := srv.Shutdown(timeout)
|
|
if err != nil {
|
|
logx.Errorf("Failed to Shutdown HTTP Server: %v", err)
|
|
}
|
|
closeDbTasks()
|
|
// todo: close backupApp and restoreApp
|
|
}()
|
|
|
|
confSrv := config.Conf.Server
|
|
logx.Infof("Listening and serving HTTP on %s", srv.Addr+confSrv.ContextPath)
|
|
var err error
|
|
if confSrv.Tls != nil && confSrv.Tls.Enable {
|
|
err = srv.ListenAndServeTLS(confSrv.Tls.CertFile, confSrv.Tls.KeyFile)
|
|
} else {
|
|
err = srv.ListenAndServe()
|
|
}
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
logx.Info("HTTP Server Shutdown")
|
|
} else if err != nil {
|
|
logx.Errorf("Failed to Start HTTP Server: %v", err)
|
|
}
|
|
}
|
|
|
|
func closeDbTasks() {
|
|
restoreApp := application.GetDbRestoreApp()
|
|
if restoreApp != nil {
|
|
restoreApp.Close()
|
|
}
|
|
binlogApp := application.GetDbBinlogApp()
|
|
if binlogApp != nil {
|
|
binlogApp.Close()
|
|
}
|
|
backupApp := application.GetDbBackupApp()
|
|
if backupApp != nil {
|
|
backupApp.Close()
|
|
}
|
|
}
|