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"
|
2024-04-17 17:39:36 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/slice/mockmap"
|
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"
|
|
|
|
)
|
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
type GinHook func(*gin.Engine)
|
2023-05-06 14:57:34 +00:00
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
var hookers mockmap.Map[string, GinHook]
|
|
|
|
|
|
|
|
// SetGinAction 方便插件在init时使用
|
|
|
|
func SetGinAction(name string, hook GinHook, orders ...float64) {
|
|
|
|
hookers.Set(name, hook, orders...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HookGinAction(name string, fn func(item mockmap.Item[string, GinHook]) mockmap.Item[string, GinHook]) {
|
|
|
|
item := hookers.Get(name)
|
|
|
|
if item.Name == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t := fn(item)
|
|
|
|
SetGinAction(name, t.Value, t.Order)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelGinAction 方便插件在init时使用
|
|
|
|
func DelGinAction(name string) {
|
|
|
|
hookers.Del(name)
|
2023-05-06 14:57:34 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2024-04-17 17:39:36 +00:00
|
|
|
SetGinAction("initTrustIp", func(r *gin.Engine) {
|
|
|
|
if len(c.TrustIps) > 0 {
|
|
|
|
err := r.SetTrustedProxies(c.TrustIps)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-10-09 15:22:18 +00:00
|
|
|
}
|
2024-04-17 17:39:36 +00:00
|
|
|
}, 99.5)
|
2022-10-09 15:22:18 +00:00
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
SetGinAction("setTemplate", func(r *gin.Engine) {
|
|
|
|
r.HTMLRender = theme.BuildTemplate()
|
|
|
|
wpconfig.SetTemplateFs(theme.TemplateFs)
|
|
|
|
}, 90.5)
|
2023-05-06 14:57:34 +00:00
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
siteFlowLimitMiddleware, siteFlow := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
2024-01-24 07:13:29 +00:00
|
|
|
reload.Append(func() {
|
2024-04-17 17:39:36 +00:00
|
|
|
c = config.GetConfig()
|
2023-02-17 15:36:54 +00:00
|
|
|
siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
2024-01-24 07:13:29 +00:00
|
|
|
}, "site-flowLimit-config")
|
2024-04-17 17:39:36 +00:00
|
|
|
|
|
|
|
SetGinAction("setGlobalMiddleware", func(r *gin.Engine) {
|
|
|
|
r.Use(
|
|
|
|
gin.Logger(),
|
|
|
|
middleware.ValidateServerNames(),
|
|
|
|
middleware.RecoverAndSendMail(gin.DefaultErrorWriter),
|
|
|
|
siteFlowLimitMiddleware,
|
|
|
|
middleware.SetStaticFileCache,
|
|
|
|
)
|
|
|
|
}, 88.5)
|
|
|
|
|
|
|
|
SetGinAction("setGzip", func(r *gin.Engine) {
|
|
|
|
//gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用
|
|
|
|
if c.Gzip {
|
|
|
|
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{
|
|
|
|
"/wp-includes/", "/wp-content/",
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}, 87.6)
|
|
|
|
|
|
|
|
SetGinAction("setWpDir", func(r *gin.Engine) {
|
|
|
|
if c.WpDir == "" {
|
|
|
|
panic("wordpress path can't be empty")
|
|
|
|
}
|
|
|
|
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"))
|
|
|
|
}, 86.1)
|
|
|
|
|
|
|
|
SetGinAction("setSession", func(r *gin.Engine) {
|
|
|
|
store := cookie.NewStore([]byte("secret"))
|
|
|
|
r.Use(sessions.Sessions("go-wp", store))
|
|
|
|
}, 85.1)
|
|
|
|
|
|
|
|
SetGinAction("setRoute", func(r *gin.Engine) {
|
|
|
|
r.GET("/", actions.Feed, 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))
|
|
|
|
r.POST("/login", actions.Login)
|
|
|
|
r.GET("/p/:id", actions.ThemeHook(constraints.Detail))
|
|
|
|
r.GET("/p/:id/comment-page-:page", actions.ThemeHook(constraints.Detail))
|
|
|
|
r.GET("/p/:id/feed", actions.PostFeed)
|
|
|
|
r.GET("/feed", actions.SiteFeed)
|
|
|
|
r.GET("/comments/feed", actions.CommentsFeed)
|
|
|
|
commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime)
|
|
|
|
r.POST("/comment", commentMiddleWare, actions.PostComment)
|
|
|
|
|
|
|
|
r.NoRoute(actions.ThemeHook(constraints.NoRoute))
|
|
|
|
}, 84.6)
|
|
|
|
|
|
|
|
SetGinAction("setpprof", func(r *gin.Engine) {
|
|
|
|
if c.Pprof != "" {
|
|
|
|
pprof.Register(r, c.Pprof)
|
|
|
|
}
|
|
|
|
}, 80.8)
|
|
|
|
|
|
|
|
slice.SimpleSort(hookers, slice.DESC, func(t mockmap.Item[string, GinHook]) float64 {
|
|
|
|
return t.Order
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, fn := range hookers {
|
|
|
|
fn.Value(r)
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:54 +00:00
|
|
|
return r
|
2022-09-01 02:31:11 +00:00
|
|
|
}
|