gin-blog/internal/routess/router.go

64 lines
1.7 KiB
Go
Raw Normal View History

2021-05-28 10:40:39 +00:00
package routess
import (
_ "blog/docs"
2021-05-31 03:17:33 +00:00
"blog/global"
2021-05-28 10:40:39 +00:00
"blog/internal/middleware"
"blog/internal/routess/api"
v1 "blog/internal/routess/api/v1"
"blog/pkg/limiter"
2021-05-28 10:40:39 +00:00
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
2021-05-31 03:17:33 +00:00
"net/http"
"time"
2021-05-28 10:40:39 +00:00
)
var methodLimiters = limiter.NewMethodLimiter().AddBuckets(limiter.LimiterBucketRule{
Key: "/auth",
FillInterval: time.Second,
Capacity: 10,
Quantum: 10,
})
2021-05-28 10:40:39 +00:00
func NewRouter() *gin.Engine {
r := gin.New()
if global.ServerSetting.RunMode == "debug" {
r.Use(gin.Logger())
r.Use(gin.Recovery())
} else {
r.Use(middleware.AccessLog())
r.Use(middleware.Recovery())
}
2021-05-28 10:40:39 +00:00
r.Use(middleware.Translations())
r.Use(middleware.RateLimiter(methodLimiters))
r.Use(middleware.ContextTimeout(60 * time.Second))
2021-05-28 10:40:39 +00:00
article := v1.NewArticle()
tag := v1.NewTag()
upload := api.NewUpload()
r.POST("/upload/file", middleware.JWT(), upload.UploadFile)
2021-05-31 03:17:33 +00:00
r.StaticFS("/static", http.Dir(global.AppSetting.UploadSavePath))
//r.GET("/static/*any",api.ReadFile)
2021-05-28 10:40:39 +00:00
r.GET("/doc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
2021-05-31 09:40:47 +00:00
r.POST("/auth", api.GetAuth)
2021-05-28 10:40:39 +00:00
apiv1 := r.Group("/api/v1")
2021-05-31 09:40:47 +00:00
apiv1.Use(middleware.JWT())
2021-05-28 10:40:39 +00:00
{
apiv1.POST("/tags", tag.Create)
apiv1.DELETE("/tags/:id", tag.Delete)
apiv1.PUT("/tags/:id", tag.Update)
apiv1.PATCH("/tags/:id/state", tag.Update)
apiv1.GET("/tags", tag.List)
apiv1.POST("/articles", article.Create)
apiv1.DELETE("/articles/:id", article.Delete)
apiv1.PUT("/articles/:id", article.Update)
apiv1.PATCH("/articles/:id/state", article.Update)
apiv1.GET("/articles/:id", article.Get)
apiv1.GET("/articles", article.List)
}
return r
}