13 Star 147 Fork 95

Pear Admin/Pear Admin Golang

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
cilidm 提交于 2021-12-02 18:17 . 去掉toolbox依赖库
package main
import (
"context"
"embed"
"fmt"
"net/http"
"os"
"os/signal"
"pear-admin-go/app/core/config"
"pear-admin-go/app/core/db"
"pear-admin-go/app/core/log"
"pear-admin-go/app/core/redis"
"pear-admin-go/app/router"
"pear-admin-go/app/util/gconv"
"pear-admin-go/app/util/validate"
"syscall"
"time"
)
//go:embed template
var templateFs embed.FS
//go:embed static
var staticFs embed.FS
func main() {
if err := validate.InitTrans("zh"); err != nil {
fmt.Println("init trans failed, err:", err)
}
config.InitConfig("./config.toml")
log.InitLog()
db.InitConn()
redis.InitRedis()
r := router.InitRouter(staticFs, templateFs)
s := &http.Server{
Addr: fmt.Sprintf(":%d", config.Instance().App.HttpPort),
Handler: r,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
}
fmt.Printf(` 欢迎使用 Pear Admin Go
程序运行地址:http://127.0.0.1:%s
`, gconv.String(config.Instance().App.HttpPort))
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Instance().Error(err.Error())
os.Exit(0)
}
}()
shutDown(s)
}
func shutDown(s *http.Server) {
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("Shutdown Server ...")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := db.Instance().Close()
if err != nil {
log.Instance().Fatal("Close DB error:" + err.Error())
}
if err := s.Shutdown(ctx); err != nil {
log.Instance().Fatal("Server Shutdown:" + err.Error())
}
fmt.Println("Server exiting")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/pear-admin/pear-admin-golang.git
git@gitee.com:pear-admin/pear-admin-golang.git
pear-admin
pear-admin-golang
Pear Admin Golang
master

搜索帮助