85 lines
2.0 KiB
Go
Raw Permalink Normal View History

2025-01-14 13:09:25 +08:00
package ecpm_httpserver
import (
"context"
"errors"
2025-01-20 00:11:11 +08:00
"gitea.youtukeji.com.cn/xiabin/youtu_ecpm/api/gin/controller"
"gitea.youtukeji.com.cn/xiabin/youtu_ecpm/dao/query"
2025-01-14 13:09:25 +08:00
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
"time"
)
type HttpServer struct {
engine *gin.Engine
log *zap.Logger
}
func NewHttpServer(logger *zap.Logger, q *query.Query) *HttpServer {
r := gin.New()
r.Use(ginzap.Ginzap(logger, time.RFC3339, true), ginzap.RecoveryWithZap(logger, true))
//初始化路由
InitRouter(r, logger, q)
return &HttpServer{
engine: r,
}
}
// Run 启动http服务
func (s *HttpServer) Run() {
srv := &http.Server{
Addr: ":8080",
Handler: s.engine,
}
go func() {
// 服务连接
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
s.log.Sugar().Fatalf("listen: %s\n", err)
}
}()
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
s.log.Sugar().Info("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
s.log.Sugar().Fatal("Server Shutdown:", err)
}
s.log.Sugar().Info("Server exiting")
}
func InitRouter(r *gin.Engine, logger *zap.Logger, q *query.Query) {
2025-01-18 19:02:58 +08:00
{
douyinCtl := controller.NewDouyinOpenApiController(logger, q)
g := r.Group("/douyin")
g.GET("/get_ecpm", douyinCtl.GetEcpm)
g.GET("/code2userId", douyinCtl.Code2OpenId)
}
2025-01-18 17:56:36 +08:00
appUserCtl := controller.NewAppUserInfoController(logger, q)
2025-01-18 19:02:58 +08:00
r.POST("/app_user_info/set_app_account", appUserCtl.SetAppAccount)
2025-01-18 17:56:36 +08:00
{
rankingCtl := controller.NewRankingList(logger, q)
g := r.Group("/ranking")
g.GET("/list", rankingCtl.GetRankingList)
g.POST("/set_score", rankingCtl.SetScore)
}
{
wechatCtl := controller.NewWechatOpenApiController(logger, q)
g := r.Group("/wechat")
2025-01-18 19:02:58 +08:00
g.GET("/code2userId", wechatCtl.Code2OpenId)
2025-01-18 17:56:36 +08:00
}
2025-01-14 13:09:25 +08:00
}