127 lines
2.6 KiB
Go
127 lines
2.6 KiB
Go
package router
|
|
|
|
import (
|
|
"Edu/app/api"
|
|
"Edu/config"
|
|
"Edu/router/rate"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Router struct {
|
|
*Option
|
|
handler *gin.Engine
|
|
}
|
|
|
|
type (
|
|
Option struct {
|
|
Mode string
|
|
IsCors bool
|
|
*RateLimitConfig
|
|
}
|
|
// RateLimitConfig 限流配置
|
|
RateLimitConfig struct {
|
|
IsRate bool `json:"is_rate"`
|
|
Limit int `json:"limit"`
|
|
Capacity int `json:"capacity"`
|
|
}
|
|
)
|
|
|
|
func (this *Router) registerAPI() {
|
|
apiPrefix := "/api"
|
|
g := this.handler.Group(apiPrefix)
|
|
g.Use()
|
|
// 登录验证
|
|
g.Use(NeedLogin(AddSkipperURL([]string{
|
|
apiPrefix + "/v1/account/authorize",
|
|
apiPrefix + "/v1/account/tourist",
|
|
apiPrefix + "/v1/index",
|
|
apiPrefix + "/v1/grade",
|
|
apiPrefix + "/v1/book/list",
|
|
}...)))
|
|
g.Use(NeedGradeParam(AddSkipperURL([]string{
|
|
apiPrefix + "/v1/account/authorize",
|
|
apiPrefix + "/v1/account/tourist",
|
|
apiPrefix + "/v1/grade",
|
|
}...)))
|
|
v1 := g.Group("/v1")
|
|
// Account 接口管理
|
|
account := v1.Group("/account")
|
|
{
|
|
_api := new(api.Account)
|
|
account.POST("/authorize", _api.Authorize)
|
|
|
|
if config.IsDebug() {
|
|
account.GET("/tourist", _api.Tourist)
|
|
}
|
|
}
|
|
// Banner 接口管理
|
|
v1.GET("/index", new(api.Index).Index)
|
|
v1.GET("/grade", new(api.Index).Grade)
|
|
v1.GET("/banner", new(api.Banner).List)
|
|
v1.GET("/book/list", new(api.Book).List)
|
|
// User 接口管理
|
|
user := v1.Group("/user")
|
|
{
|
|
_api := new(api.User)
|
|
user.GET("/info", _api.Info)
|
|
}
|
|
// Book 接口管理
|
|
book := v1.Group("/book")
|
|
{
|
|
_api := new(api.Book)
|
|
book.GET("/home", _api.Home)
|
|
//book.GET("/list", _api.List)
|
|
book.GET("/detail", _api.Detail)
|
|
book.POST("/play", _api.Play)
|
|
}
|
|
// Pay 支付管理
|
|
pay := v1.Group("/pay")
|
|
{
|
|
_api := new(api.Pay)
|
|
pay.POST("/launch", _api.Launch)
|
|
pay.POST("/notice", _api.Notify)
|
|
}
|
|
}
|
|
|
|
func (this *Router) Init() *gin.Engine {
|
|
gin.SetMode(this.Mode)
|
|
|
|
app := gin.New()
|
|
|
|
if this.IsCors {
|
|
app.Use(Cors())
|
|
}
|
|
app.NoRoute(NoRouteHandler())
|
|
app.NoMethod(NoMethodHandler())
|
|
app.Use(LoggerHandle("log/gin.log", 3))
|
|
app.Use(TimeoutHandle(time.Second * 30))
|
|
app.Use(RecoveryHandler())
|
|
|
|
if this.RateLimitConfig != nil {
|
|
if this.RateLimitConfig.IsRate {
|
|
rate.NewIPRateLimiter()(this.RateLimitConfig.Limit, this.RateLimitConfig.Capacity)
|
|
app.Use(rate.RequestIPRateLimiter()(this.RateLimitConfig.Limit, this.RateLimitConfig.Capacity))
|
|
}
|
|
}
|
|
if config.IsDebug() {
|
|
gin.DefaultWriter = io.MultiWriter(os.Stdout)
|
|
app.StaticFS("/api-docs", http.Dir("./doc"))
|
|
}
|
|
app.StaticFS("/upload", http.Dir("./upload"))
|
|
app.MaxMultipartMemory = 4 << 20
|
|
this.handler = app
|
|
// 注册路由
|
|
this.registerAPI()
|
|
|
|
return app
|
|
}
|
|
|
|
func NewRouter(option *Option) *Router {
|
|
return &Router{Option: option}
|
|
}
|