gohttp/main.go

56 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"os"
"os/signal"
"syscall"
"git.pyer.club/kingecg/godaemon"
"git.pyer.club/kingecg/gologger"
)
// main 函数是程序的入口点
func main() {
// 加载配置文件
LoadConfig()
// 获取日志记录器
l := gologger.GetLogger("main")
// 使用 defer 和 recover 捕获 panic确保程序不会因未处理的异常而崩溃
defer func() {
if err := recover(); err != nil {
l.Error("panic", err)
}
}()
// 创建守护进程实例
daemon := godaemon.NewGoDaemon(start, stop)
// 启动守护进程
daemon.Start()
}
// waiter 用于接收系统信号
var waiter chan os.Signal
// start 函数是守护进程启动时执行的函数
func start(g *godaemon.GoDaemon) {
// 创建缓冲通道用于接收系统信号
waiter = make(chan os.Signal, 1)
// 监听 SIGTERM 和 SIGINT 信号
signal.Notify(waiter, syscall.SIGTERM, syscall.SIGINT)
// 创建 HTTP 服务器实例
httpd := &GoHttp{}
// 启动 HTTP 服务器
httpd.Start()
// 阻塞等待信号
<-waiter
// 记录退出日志
httpd.logger.Info("Exit")
}
// stop 函数是守护进程停止时执行的函数
func stop(g *godaemon.GoDaemon) {
// 发送 SIGTERM 信号给当前进程
g.Running.Process.Signal(syscall.SIGTERM)
}