80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package command
|
||
|
||
import (
|
||
"context"
|
||
"github.com/Superdanda/hade/framework/cobra"
|
||
"github.com/Superdanda/hade/framework/contract"
|
||
"log"
|
||
"net/http"
|
||
"os"
|
||
"os/signal"
|
||
"syscall"
|
||
"time"
|
||
)
|
||
|
||
var appAddress = ""
|
||
|
||
func initAppCommand() *cobra.Command {
|
||
appStartCommand.Flags().StringVar(&appAddress, "address", "", "set app address")
|
||
|
||
appCommand.AddCommand(appStartCommand)
|
||
return appCommand
|
||
}
|
||
|
||
var appCommand = &cobra.Command{
|
||
Use: "app",
|
||
Short: "业务应用控制命令",
|
||
Long: "业务应用控制命令,其包含业务启动,关闭,重启,查询等功能",
|
||
RunE: func(c *cobra.Command, args []string) error {
|
||
c.Help()
|
||
return nil
|
||
},
|
||
}
|
||
|
||
var appStartCommand = &cobra.Command{
|
||
Use: "start",
|
||
Short: "启动一个Web服务",
|
||
RunE: func(c *cobra.Command, args []string) error {
|
||
// 从Command中获取服务容器
|
||
container := c.GetContainer()
|
||
// 从服务容器中获取kernel的服务实例
|
||
kernelService := container.MustMake(contract.KernelKey).(contract.Kernel)
|
||
// 从kernel服务实例中获取引擎
|
||
core := kernelService.HttpEngine()
|
||
|
||
var addr string
|
||
if appAddress == "" {
|
||
addr = ":8070"
|
||
} else {
|
||
addr = appAddress
|
||
}
|
||
// 创建一个Server服务
|
||
server := &http.Server{
|
||
Handler: core,
|
||
Addr: addr,
|
||
}
|
||
|
||
// 这个goroutine是启动服务的goroutine
|
||
go func() {
|
||
server.ListenAndServe()
|
||
}()
|
||
|
||
// 当前的goroutine等待信号量
|
||
quit := make(chan os.Signal)
|
||
// 监控信号:SIGINT, SIGTERM, SIGQUIT
|
||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||
// 这里会阻塞当前goroutine等待信号
|
||
<-quit
|
||
|
||
// 调用Server.Shutdown graceful结束
|
||
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
defer cancel()
|
||
|
||
if err := server.Shutdown(timeoutCtx); err != nil {
|
||
log.Fatal("Server Shutdown:", err)
|
||
}
|
||
|
||
return nil
|
||
},
|
||
}
|