2022-09-01 02:31:11 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
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-01-12 10:17:34 +00:00
|
|
|
actions2 "github/fthvgb1/wp-go/internal/actions"
|
|
|
|
middleware2 "github/fthvgb1/wp-go/internal/middleware"
|
2023-01-13 06:04:32 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/pkg/config"
|
2023-01-12 10:17:34 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/static"
|
|
|
|
"github/fthvgb1/wp-go/internal/templates"
|
|
|
|
"github/fthvgb1/wp-go/internal/wpconfig"
|
2022-09-13 03:23:28 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2022-09-14 05:28:31 +00:00
|
|
|
"time"
|
2022-09-01 02:31:11 +00:00
|
|
|
)
|
|
|
|
|
2022-11-16 02:17:29 +00:00
|
|
|
func SetupRouter() (*gin.Engine, func()) {
|
2022-09-01 02:31:11 +00:00
|
|
|
// Disable Console Color
|
|
|
|
// gin.DisableConsoleColor()
|
2022-09-29 09:23:02 +00:00
|
|
|
r := gin.New()
|
2022-11-15 08:36:21 +00:00
|
|
|
c := config.Conf.Load()
|
|
|
|
if len(c.TrustIps) > 0 {
|
|
|
|
err := r.SetTrustedProxies(c.TrustIps)
|
2022-10-09 15:22:18 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:44:54 +00:00
|
|
|
r.HTMLRender = templates.NewFsTemplate(template.FuncMap{
|
2022-09-20 14:37:31 +00:00
|
|
|
"unescaped": func(s string) any {
|
2022-09-14 05:28:31 +00:00
|
|
|
return template.HTML(s)
|
|
|
|
},
|
2022-09-20 14:37:31 +00:00
|
|
|
"dateCh": func(t time.Time) any {
|
2022-09-18 14:06:27 +00:00
|
|
|
return t.Format("2006年 01月 02日")
|
2022-09-14 05:28:31 +00:00
|
|
|
},
|
2022-11-15 03:11:08 +00:00
|
|
|
"getOption": func(k string) string {
|
2022-11-17 08:29:39 +00:00
|
|
|
return wpconfig.Options.Value(k)
|
2022-11-15 03:11:08 +00:00
|
|
|
},
|
2022-09-19 04:45:41 +00:00
|
|
|
}).SetTemplate()
|
2023-01-12 10:17:34 +00:00
|
|
|
validServerName, reloadValidServerNameFn := middleware2.ValidateServerNames()
|
|
|
|
fl, flReload := middleware2.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime)
|
2022-10-10 13:07:39 +00:00
|
|
|
r.Use(
|
|
|
|
gin.Logger(),
|
2022-11-16 02:17:29 +00:00
|
|
|
validServerName,
|
2023-01-12 10:17:34 +00:00
|
|
|
middleware2.RecoverAndSendMail(gin.DefaultErrorWriter),
|
2022-11-16 02:51:39 +00:00
|
|
|
fl,
|
2023-01-12 10:17:34 +00:00
|
|
|
middleware2.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
|
|
|
|
2022-09-13 03:23:28 +00:00
|
|
|
f := static.Fs{FS: static.FsEx, Path: "wp-includes"}
|
2022-09-22 13:59:33 +00:00
|
|
|
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.FsEx))
|
2022-09-13 03:23:28 +00:00
|
|
|
r.StaticFS("/wp-includes", http.FS(f))
|
|
|
|
r.StaticFS("/wp-content", http.FS(static.Fs{
|
|
|
|
FS: static.FsEx,
|
|
|
|
Path: "wp-content",
|
|
|
|
}))
|
2022-09-18 03:57:43 +00:00
|
|
|
store := cookie.NewStore([]byte("secret"))
|
|
|
|
r.Use(sessions.Sessions("go-wp", store))
|
2023-01-12 10:17:34 +00:00
|
|
|
sl, slRload := middleware2.SearchLimit(c.SingleIpSearchNum)
|
|
|
|
r.GET("/", sl, actions2.Index)
|
|
|
|
r.GET("/page/:page", actions2.Index)
|
|
|
|
r.GET("/p/category/:category", actions2.Index)
|
|
|
|
r.GET("/p/category/:category/page/:page", actions2.Index)
|
|
|
|
r.GET("/p/tag/:tag", actions2.Index)
|
|
|
|
r.GET("/p/tag/:tag/page/:page", actions2.Index)
|
|
|
|
r.GET("/p/date/:year/:month", actions2.Index)
|
|
|
|
r.GET("/p/date/:year/:month/page/:page", actions2.Index)
|
|
|
|
r.GET("/p/author/:author", actions2.Index)
|
|
|
|
r.GET("/p/author/:author/page/:page", actions2.Index)
|
|
|
|
r.POST("/login", actions2.Login)
|
|
|
|
r.GET("/p/:id", actions2.Detail)
|
|
|
|
r.GET("/p/:id/feed", actions2.PostFeed)
|
|
|
|
r.GET("/feed", actions2.Feed)
|
|
|
|
r.GET("/comments/feed", actions2.CommentsFeed)
|
|
|
|
cfl, _ := middleware2.FlowLimit(c.MaxRequestSleepNum, 5, c.SleepTime)
|
|
|
|
r.POST("/comment", cfl, actions2.PostComment)
|
2022-11-05 13:10:20 +00:00
|
|
|
if gin.Mode() != gin.ReleaseMode {
|
2022-09-29 09:23:02 +00:00
|
|
|
pprof.Register(r, "dev/pprof")
|
|
|
|
}
|
2022-11-16 02:51:39 +00:00
|
|
|
fn := func() {
|
|
|
|
reloadValidServerNameFn()
|
|
|
|
c := config.Conf.Load()
|
|
|
|
flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime)
|
2022-11-16 03:09:25 +00:00
|
|
|
slRload(c.SingleIpSearchNum)
|
2022-11-16 02:51:39 +00:00
|
|
|
}
|
2022-11-16 02:17:29 +00:00
|
|
|
return r, fn
|
2022-09-01 02:31:11 +00:00
|
|
|
}
|