2022-09-01 02:31:11 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2023-05-04 12:36:17 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/actions"
|
|
|
|
"github.com/fthvgb1/wp-go/app/middleware"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/config"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/constraints"
|
|
|
|
"github.com/fthvgb1/wp-go/app/theme"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
2023-05-31 13:54:23 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
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"
|
|
|
|
)
|
|
|
|
|
2023-05-06 14:57:34 +00:00
|
|
|
var hooker []func(r *gin.Engine)
|
|
|
|
|
|
|
|
// Hook 方便插件在init时使用
|
|
|
|
func Hook(fn ...func(r *gin.Engine)) {
|
|
|
|
hooker = append(hooker, fn...)
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:54 +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)
|
2023-02-17 15:36:54 +00:00
|
|
|
siteFlowLimitMiddleware, siteFlow := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
2022-10-10 13:07:39 +00:00
|
|
|
r.Use(
|
|
|
|
gin.Logger(),
|
2023-02-17 15:36:54 +00:00
|
|
|
middleware.ValidateServerNames(),
|
2023-01-14 04:21:31 +00:00
|
|
|
middleware.RecoverAndSendMail(gin.DefaultErrorWriter),
|
2023-02-17 15:36:54 +00:00
|
|
|
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-06-16 11:26:56 +00:00
|
|
|
if c.WpDir == "" {
|
|
|
|
panic("wordpress path can't be empty")
|
2023-01-30 12:16:03 +00:00
|
|
|
}
|
2023-06-16 11:26:56 +00:00
|
|
|
r.Static("/wp-content/uploads", str.Join(c.WpDir, "/wp-content/uploads"))
|
|
|
|
r.Static("/wp-content/themes", str.Join(c.WpDir, "/wp-content/themes"))
|
|
|
|
r.Static("/wp-content/plugins", str.Join(c.WpDir, "/wp-content/plugins"))
|
|
|
|
r.Static("/wp-includes/css", str.Join(c.WpDir, "/wp-includes/css"))
|
|
|
|
r.Static("/wp-includes/fonts", str.Join(c.WpDir, "/wp-includes/fonts"))
|
|
|
|
r.Static("/wp-includes/js", str.Join(c.WpDir, "/wp-includes/js"))
|
2023-05-31 13:54:23 +00:00
|
|
|
|
2022-09-18 03:57:43 +00:00
|
|
|
store := cookie.NewStore([]byte("secret"))
|
|
|
|
r.Use(sessions.Sessions("go-wp", store))
|
2023-02-17 15:36:54 +00:00
|
|
|
r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home))
|
2023-02-11 15:51:07 +00:00
|
|
|
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)
|
2023-02-11 15:51:07 +00:00
|
|
|
r.GET("/p/:id", actions.ThemeHook(constraints.Detail))
|
2023-12-08 13:33:09 +00:00
|
|
|
r.GET("/p/:id/comment-page-:page", 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)
|
2023-06-18 11:47:47 +00:00
|
|
|
//r.NoRoute(actions.ThemeHook(constraints.NoRoute))
|
2023-02-17 15:36:54 +00:00
|
|
|
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
|
|
|
}
|
2023-05-06 14:57:34 +00:00
|
|
|
for _, fn := range hooker {
|
|
|
|
fn(r)
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:54 +00:00
|
|
|
reload.Push(func() {
|
2023-02-05 13:06:04 +00:00
|
|
|
c := config.GetConfig()
|
2023-02-17 15:36:54 +00:00
|
|
|
siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
|
|
|
})
|
|
|
|
return r
|
2022-09-01 02:31:11 +00:00
|
|
|
}
|