web.go 928 Bytes
Newer Older
彭芳 committed
1 2 3 4 5 6 7 8
package router

import (
	"net/http"

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"

彭芳 committed
9
	"demo/internal/router/mid"
彭芳 committed
10 11 12 13
	"demo/internal/service"
)

type Web struct {
彭芳 committed
14 15
	Common  *mid.Common
	Jwt     *mid.Jwt
彭芳 committed
16
	Greeter *service.GreeterService
彭芳 committed
17
	Report  *service.ReportService
彭芳 committed
18 19 20
}

func (p *Web) RouterInit(engine *gin.Engine) {
彭芳 committed
21
	engine.Use(p.Common.SetRequestId, p.Common.PassMethods)
彭芳 committed
22 23 24 25 26 27 28
	engine.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{"code": "404", "msg": "PageNo not found"})
	})
	// add pprof to look at the heap,a 30-second CPU and so on profile
	// eg: go tool pprof http://localhost:8000/api/pprof/profile
	pprof.Register(engine, "/api/pprof")
	// add api
彭芳 committed
29 30 31
	engine.GET("/hello/:name", p.Greeter.SayHello)
	api := engine.Group("/api", p.Jwt.CheckToken)
	{
彭芳 committed
32 33 34 35 36
		report := api.Group("/report")
		{
			report.POST("/campaign", p.Report.ListCampaignData)
			report.POST("/ad", p.Report.ListAdData)
		}
彭芳 committed
37
	}
彭芳 committed
38
}