Files
gin-template/main.go
刘欢 930197e624 refactor(gorm): Rename database package to gorm package - [将database包重命名为gorm包]
Renamed database related code from 'database' package to 'gorm' package to better integrate with gorm ORM framework. Changes include updating import paths and structure names, such as changing from 'database.ToDBName' to 'gorm.ToDBName', and similar adjustments across multiple files. - [将数据库相关代码从'database'包重命名为'gorm'包,以更好地与gorm ORM框架整合。更改包括更新导入路径和结构名称,例如从'database.ToDBName'更改为'gorm.ToDBName',以及跨多个文件的类似调整。]
2024-09-03 23:18:06 +08:00

128 lines
2.9 KiB
Go

// Package gin-template
//
// @program: gin-template
// @author: [lliuhuan](https://github.com/lliuhuan)
// @create: 2024-07-02 18:07
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/LLiuHuan/gin-template/configs"
"github.com/LLiuHuan/gin-template/internal/router"
"github.com/LLiuHuan/gin-template/pkg/env"
"github.com/LLiuHuan/gin-template/pkg/kprocess"
"github.com/LLiuHuan/gin-template/pkg/logger"
"github.com/LLiuHuan/gin-template/pkg/timeutil"
"github.com/gin-gonic/gin"
)
// 初始化执行
func init() {
// 开发模式
gin.SetMode(gin.DebugMode)
// 生产模式
//gin.SetMode(gin.ReleaseMode)
}
// @title swagger 接口文档
// @version 2.0
// @description
// @contact.name
// @contact.url
// @contact.email
// @license.name MIT
// @license.url https://github.com/LLiuHuan/gin-template/blob/master/LICENSE
// @securityDefinitions.apikey LoginToken
// @in header
// @name token
func main() {
accessLogger, err := logger.NewJSONLogger(
logger.WithDisableConsole(),
logger.WithField("domain", fmt.Sprintf("%s[%s]", configs.ProjectName, env.Active().Value())),
logger.WithTimeLayout(timeutil.CSTLayout),
logger.WithFileRotationP(configs.ProjectLogFile))
if err != nil {
panic(err)
}
// 初始化 cron logger
cronLogger, err := logger.NewJSONLogger(
logger.WithDisableConsole(),
logger.WithField("domain", fmt.Sprintf("%s[%s]", configs.ProjectName, env.Active().Value())),
logger.WithTimeLayout(timeutil.CSTLayout),
logger.WithFileP(configs.ProjectCronLogFile),
)
if err != nil {
panic(err)
}
defer func() {
_ = accessLogger.Sync()
_ = cronLogger.Sync()
}()
// 初始化 HTTP 服务
s, err := router.NewHTTPServer(accessLogger, cronLogger)
if err != nil {
panic(err)
}
addr := fmt.Sprintf("%s:%d", configs.Get().Project.Domain, configs.Get().Project.Port)
kp := kprocess.NewKProcess(accessLogger, configs.Get().Project.PidFile)
ln, err := kp.Listen("tcp", addr)
if err != nil {
panic(err)
}
serve := http.Server{
Handler: s.Mux,
}
serverCloe := make(chan struct{})
go func() {
defer func() {
close(serverCloe)
}()
err = serve.Serve(ln)
if err != nil {
accessLogger.Info(fmt.Sprintf("App run Serve: %v\n", err))
fmt.Printf("App run Serve: %v\n", err)
}
}()
select {
case <-kp.Exit():
case <-serverCloe:
}
// Make sure to set a deadline on exiting the process
// after upg.Exit() is closed. No new upgrades can be
// performed if the parent doesn't exit.
time.AfterFunc(30*time.Second, func() {
accessLogger.Info("App server Shutdown timeout, force exit")
fmt.Println("App server Shutdown timeout, force exit")
os.Exit(1)
})
err = serve.Shutdown(context.Background())
if err != nil {
accessLogger.Info(fmt.Sprintf("App run Shutdown: %v\n", err))
fmt.Printf("App run Shutdown: %v\n", err)
}
accessLogger.Info("App server Shutdown ok")
fmt.Println("App server Shutdown ok")
router.ShutdownServer(accessLogger, s)
}