wp-go/internal/cmd/route/route.go

91 lines
3.2 KiB
Go
Raw Normal View History

2022-09-01 02:31:11 +00:00
package route
import (
"github.com/fthvgb1/wp-go/internal/actions"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/middleware"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/static"
"github.com/fthvgb1/wp-go/internal/theme"
2023-02-15 16:32:02 +00:00
"github.com/fthvgb1/wp-go/internal/wpconfig"
2022-10-07 15:48:42 +00:00
"github.com/gin-contrib/gzip"
2022-09-24 09:52:06 +00:00
"github.com/gin-contrib/pprof"
2022-09-18 03:57:43 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
2022-09-01 02:31:11 +00:00
"github.com/gin-gonic/gin"
2022-09-13 03:23:28 +00:00
"net/http"
2022-09-01 02:31:11 +00:00
)
func SetupRouter() *gin.Engine {
2022-09-01 02:31:11 +00:00
// Disable Console Color
// gin.DisableConsoleColor()
2022-09-29 09:23:02 +00:00
r := gin.New()
2023-02-05 13:06:04 +00:00
c := config.GetConfig()
2022-11-15 08:36:21 +00:00
if len(c.TrustIps) > 0 {
err := r.SetTrustedProxies(c.TrustIps)
2022-10-09 15:22:18 +00:00
if err != nil {
panic(err)
}
}
2023-03-09 14:36:41 +00:00
r.HTMLRender = theme.Template()
2023-02-15 16:32:02 +00:00
wpconfig.SetTemplateFs(theme.TemplateFs)
siteFlowLimitMiddleware, siteFlow := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
2022-10-10 13:07:39 +00:00
r.Use(
gin.Logger(),
middleware.ValidateServerNames(),
2023-01-14 04:21:31 +00:00
middleware.RecoverAndSendMail(gin.DefaultErrorWriter),
siteFlowLimitMiddleware,
2023-01-14 04:21:31 +00:00
middleware.SetStaticFileCache,
2022-10-10 13:07:39 +00:00
)
2022-09-17 13:57:45 +00:00
//gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用
2022-11-15 08:36:21 +00:00
if c.Gzip {
2022-10-07 15:48:42 +00:00
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{
"/wp-includes/", "/wp-content/",
})))
}
2022-11-16 02:51:39 +00:00
2023-01-30 12:16:03 +00:00
f := static.Fs{FS: static.FsDir, Path: "wp-includes"}
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.FsDir))
2022-09-13 03:23:28 +00:00
r.StaticFS("/wp-includes", http.FS(f))
2023-01-30 12:16:03 +00:00
r.StaticFS("/wp-content/plugins", http.FS(static.Fs{
FS: static.FsDir,
Path: "wp-content/plugins",
2022-09-13 03:23:28 +00:00
}))
2023-01-30 12:16:03 +00:00
r.StaticFS("/wp-content/themes", http.FS(static.Fs{
FS: static.FsDir,
Path: "wp-content/themes",
}))
if c.UploadDir != "" {
r.Static("/wp-content/uploads", c.UploadDir)
}
2022-09-18 03:57:43 +00:00
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("go-wp", store))
r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home))
r.GET("/page/:page", actions.ThemeHook(constraints.Home))
r.GET("/p/category/:category", actions.ThemeHook(constraints.Category))
r.GET("/p/category/:category/page/:page", actions.ThemeHook(constraints.Category))
r.GET("/p/tag/:tag", actions.ThemeHook(constraints.Tag))
r.GET("/p/tag/:tag/page/:page", actions.ThemeHook(constraints.Tag))
r.GET("/p/date/:year/:month", actions.ThemeHook(constraints.Archive))
r.GET("/p/date/:year/:month/page/:page", actions.ThemeHook(constraints.Archive))
r.GET("/p/author/:author", actions.ThemeHook(constraints.Author))
r.GET("/p/author/:author/page/:page", actions.ThemeHook(constraints.Author))
2023-01-14 04:21:31 +00:00
r.POST("/login", actions.Login)
r.GET("/p/:id", actions.ThemeHook(constraints.Detail))
2023-01-14 04:21:31 +00:00
r.GET("/p/:id/feed", actions.PostFeed)
r.GET("/feed", actions.Feed)
r.GET("/comments/feed", actions.CommentsFeed)
commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime)
r.POST("/comment", commentMiddleWare, actions.PostComment)
2023-01-31 16:58:42 +00:00
if c.Pprof != "" {
pprof.Register(r, c.Pprof)
2022-09-29 09:23:02 +00:00
}
reload.Push(func() {
2023-02-05 13:06:04 +00:00
c := config.GetConfig()
siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
})
return r
2022-09-01 02:31:11 +00:00
}