75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
/**
|
|
* @Author: david.yan(david.yan@qq.com)
|
|
* @Date: 2021-11-26 15:25:03
|
|
*/
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
"git.apinb.com/bsm-sdk/core/middleware"
|
|
"git.apinb.com/bsm-sdk/core/printer"
|
|
"git.apinb.com/ops/sample/internal/config"
|
|
"git.apinb.com/ops/sample/internal/impl"
|
|
"git.apinb.com/ops/sample/internal/routers"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
var (
|
|
ServiceKey = "Sample"
|
|
)
|
|
|
|
func main() {
|
|
// 配置初始化
|
|
config.New(ServiceKey)
|
|
|
|
// 创建实现层
|
|
impl.NewImpl()
|
|
|
|
// 初始化Gin引擎
|
|
app := gin.Default()
|
|
|
|
// 使用中间件
|
|
middleware.Mode(app)
|
|
app.Use(middleware.Cors())
|
|
app.Use(gin.Recovery())
|
|
|
|
// 注册健康检查路由
|
|
app.HEAD("/", infra.Health)
|
|
|
|
// 注册应用路由
|
|
routers.Register(ServiceKey, app)
|
|
|
|
addr := fmt.Sprintf(":%s", config.Spec.Port)
|
|
srv := &http.Server{Addr: addr, Handler: app}
|
|
|
|
go func() {
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
panic(err)
|
|
}
|
|
}()
|
|
|
|
printer.Success("[BSM - %s] Service Started At: %s", ServiceKey, addr)
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
printer.Info("[BSM - %s] Shutting down...", ServiceKey)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
printer.Success("[BSM - %s] Service stopped", ServiceKey)
|
|
}
|