wp-go/route/route.go

52 lines
1.4 KiB
Go
Raw Normal View History

2022-09-01 02:31:11 +00:00
package route
import (
"github.com/gin-gonic/gin"
2022-09-14 05:28:31 +00:00
"github/fthvgb1/wp-go/middleware"
2022-09-13 03:23:28 +00:00
"github/fthvgb1/wp-go/static"
2022-09-14 13:30:59 +00:00
"github/fthvgb1/wp-go/templates"
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
)
func SetupRouter() *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
r := gin.Default()
2022-09-14 05:28:31 +00:00
r.Use(middleware.SetStaticFileCache)
2022-09-15 17:24:56 +00:00
//gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用
/*r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{
2022-09-15 15:22:06 +00:00
"/wp-includes/", "/wp-content/",
2022-09-15 17:24:56 +00:00
})))*/
2022-09-14 05:28:31 +00:00
r.SetFuncMap(template.FuncMap{
"unescaped": func(s string) interface{} {
return template.HTML(s)
},
"dateCh": func(t time.Time) interface{} {
return t.Format("2006年01月02日")
},
})
2022-09-13 03:23:28 +00:00
f := static.Fs{FS: static.FsEx, Path: "wp-includes"}
r.StaticFS("/wp-includes", http.FS(f))
r.StaticFS("/wp-content", http.FS(static.Fs{
FS: static.FsEx,
Path: "wp-content",
}))
2022-09-14 13:30:59 +00:00
loadTemplates(r, "**/*")
2022-09-01 02:31:11 +00:00
r.GET("/", index)
2022-09-15 14:35:32 +00:00
r.GET("/page/:page", index)
2022-09-16 06:46:51 +00:00
r.GET("/p/category/:category", index)
2022-09-16 09:59:17 +00:00
r.GET("/p/tag/:tag", index)
2022-09-16 06:46:51 +00:00
r.GET("/p/date/:year/:month", index)
r.GET("/p/date/:year/:month/page/:page", index)
2022-09-01 02:31:11 +00:00
return r
}
2022-09-14 13:30:59 +00:00
func loadTemplates(engine *gin.Engine, pattern string) {
templ := template.New("").Funcs(engine.FuncMap).Delims("{{", "}}")
templ = template.Must(templ.ParseFS(templates.TemplateFs, pattern))
engine.SetHTMLTemplate(templ)
}