Compare commits
2 Commits
b69b01f27b
...
87f639da58
Author | SHA1 | Date | |
---|---|---|---|
87f639da58 | |||
ec211e8970 |
|
@ -17,6 +17,13 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
var hooker []func(r *gin.Engine)
|
||||
|
||||
// Hook 方便插件在init时使用
|
||||
func Hook(fn ...func(r *gin.Engine)) {
|
||||
hooker = append(hooker, fn...)
|
||||
}
|
||||
|
||||
func SetupRouter() *gin.Engine {
|
||||
// Disable Console Color
|
||||
// gin.DisableConsoleColor()
|
||||
|
@ -83,6 +90,10 @@ func SetupRouter() *gin.Engine {
|
|||
if c.Pprof != "" {
|
||||
pprof.Register(r, c.Pprof)
|
||||
}
|
||||
for _, fn := range hooker {
|
||||
fn(r)
|
||||
}
|
||||
|
||||
reload.Push(func() {
|
||||
c := config.GetConfig()
|
||||
siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
||||
|
|
9
app/plugins/devexample/plugintt/a.gohtml
Normal file
9
app/plugins/devexample/plugintt/a.gohtml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<div>
|
||||
{{.aa}}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
{{ range $k,$v := .posts}}
|
||||
<h2>{{$v.PostTitle}} </h2>
|
||||
{{end}}
|
||||
</div>
|
|
@ -1,24 +1,82 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"github.com/fthvgb1/wp-go/app/cmd/route"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/app/plugins/wphandle"
|
||||
"github.com/fthvgb1/wp-go/app/theme"
|
||||
"github.com/fthvgb1/wp-go/app/theme/wp"
|
||||
"github.com/fthvgb1/wp-go/app/theme/wp/components"
|
||||
"github.com/fthvgb1/wp-go/app/theme/wp/components/widget"
|
||||
route2 "github.com/fthvgb1/wp-go/app/theme/wp/route"
|
||||
"github.com/gin-gonic/gin"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"plugintt/xx"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// here can register theme
|
||||
theme.AddThemeHookFunc("xxx", Xo)
|
||||
}
|
||||
//go:embed a.gohtml
|
||||
var em embed.FS
|
||||
var tt *template.Template
|
||||
|
||||
func Xo(h *wp.Handle) {
|
||||
// action or hook or config theme
|
||||
h.PushHandler(constraints.PipeRender, constraints.Home, wp.HandleCall{
|
||||
Fn: func(handle *wp.Handle) {
|
||||
xx.Xo()
|
||||
},
|
||||
Order: 100,
|
||||
Name: "xxxx",
|
||||
func init() {
|
||||
// register as theme
|
||||
theme.AddThemeHookFunc("themename", hook)
|
||||
|
||||
//use the local template
|
||||
//note: must use embed.FS
|
||||
t, err := template.ParseFS(em, "a.gohtml")
|
||||
if err != nil {
|
||||
logs.Error(err, "")
|
||||
}
|
||||
tt = t
|
||||
|
||||
// register gin route. it will be effecting when server restart.
|
||||
route.Hook(func(r *gin.Engine) {
|
||||
r.GET("xx", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "xxoo")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func hook(h *wp.Handle) {
|
||||
wp.Run(h, config)
|
||||
}
|
||||
|
||||
func config(h *wp.Handle) {
|
||||
// same theme config
|
||||
wphandle.UsePlugins(h)
|
||||
wp.InitPipe(h)
|
||||
h.PushHandler(constraints.PipeMiddleware, constraints.Home,
|
||||
wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory"))
|
||||
components.WidgetArea(h)
|
||||
h.PushHandler(constraints.PipeRender, constraints.Home, wp.NewHandleFn(func(h *wp.Handle) {
|
||||
h.SetData("aa", "xyxxxx")
|
||||
h.RenderHtml(tt, http.StatusOK, "a.gohtml")
|
||||
h.Abort()
|
||||
h.StopPipe()
|
||||
}, 10, "renderHome"))
|
||||
|
||||
// use simple reg route
|
||||
route2.PushRoute(`(?P<control>\w+)/(?P<method>\w+)`, route2.Route{
|
||||
Path: `(?P<control>\w+)/(?P<method>\w+)`,
|
||||
Scene: constraints.Home,
|
||||
Method: []string{"GET"},
|
||||
Type: "reg",
|
||||
ConfigHandle: wp.Index,
|
||||
})
|
||||
//...
|
||||
}
|
||||
|
||||
// Xo to be a func when theme init
|
||||
func Xo(h *wp.Handle) {
|
||||
xx.Xo()
|
||||
route2.Delete(`(?P<control>\w+)/(?P<method>\w+)`)
|
||||
h.ReplaceHandle(constraints.PipeRender, "wp.RenderTemplate", func(h *wp.Handle) {
|
||||
h.SetData("aa", "xyxxxx")
|
||||
h.RenderHtml(tt, http.StatusOK, "a.gohtml")
|
||||
h.StopPipe()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -2,5 +2,6 @@
|
|||
|
||||
# copy plugintt to other dir and remove .dev suffix
|
||||
# note the go version and build tool flag must same to server build
|
||||
# eg: -gcflags all="-N -l" may used in ide debug
|
||||
# eg: -gcflags all="-N -l" --race may used in ide debug
|
||||
go mod tidy
|
||||
go build -buildmode=plugin -o xx.so main.go
|
131
app/theme/wp/route/route.go
Normal file
131
app/theme/wp/route/route.go
Normal file
|
@ -0,0 +1,131 @@
|
|||
package route
|
||||
|
||||
import (
|
||||
"github.com/fthvgb1/wp-go/app/cmd/reload"
|
||||
"github.com/fthvgb1/wp-go/app/theme/wp"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
Path string
|
||||
Scene string
|
||||
Method []string
|
||||
Type string //const|reg
|
||||
ConfigHandle func(*wp.Handle)
|
||||
}
|
||||
|
||||
var routeHook []func(Route) (Route, bool)
|
||||
|
||||
var regRoutes *safety.Map[string, *regexp.Regexp]
|
||||
var routes = func() *safety.Map[string, Route] {
|
||||
r := safety.NewMap[string, Route]()
|
||||
reload.Push(func() {
|
||||
r.Flush()
|
||||
regRoutes.Flush()
|
||||
})
|
||||
regRoutes = safety.NewMap[string, *regexp.Regexp]()
|
||||
return r
|
||||
}()
|
||||
|
||||
// PushRoute path can be const or regex string
|
||||
//
|
||||
// eg: `(?P<control>\w+)/(?P<method>\w+)`, route.Route{
|
||||
// Path: `(?P<control>\w+)/(?P<method>\w+)`,
|
||||
// Scene: constraints.Home,
|
||||
// Method: []string{"GET"},
|
||||
// Type: "reg",
|
||||
// ConfigHandle: $theme.Hook or nil,
|
||||
// }
|
||||
func PushRoute(path string, route Route) error {
|
||||
if route.Type == "const" {
|
||||
routes.Store(path, route)
|
||||
return nil
|
||||
}
|
||||
re, err := regexp.Compile(route.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
regRoutes.Store(path, re)
|
||||
routes.Store(path, route)
|
||||
return err
|
||||
}
|
||||
|
||||
func Delete(path string) {
|
||||
routeHook = append(routeHook, func(route Route) (Route, bool) {
|
||||
return route, route.Path != path
|
||||
})
|
||||
}
|
||||
|
||||
func Replace(path string, route Route) {
|
||||
routeHook = append(routeHook, func(r Route) (Route, bool) {
|
||||
return route, path == route.Path
|
||||
})
|
||||
}
|
||||
|
||||
func Hook(path string, fn func(Route) Route) {
|
||||
routeHook = append(routeHook, func(r Route) (Route, bool) {
|
||||
if path == r.Path {
|
||||
r = fn(r)
|
||||
}
|
||||
return r, path == r.Path
|
||||
})
|
||||
}
|
||||
func ResolveRoute(h *wp.Handle) {
|
||||
requestURI := h.C.Request.RequestURI
|
||||
rs, rrs := reload.GetAnyValBys("route",
|
||||
struct{}{},
|
||||
func(_ struct{}) func() (map[string]Route, map[string]*regexp.Regexp) {
|
||||
m := map[string]Route{}
|
||||
rrs := map[string]*regexp.Regexp{}
|
||||
routes.Range(func(key string, value Route) bool {
|
||||
vv, _ := regRoutes.Load(key)
|
||||
if len(routeHook) > 0 {
|
||||
for _, fn := range routeHook {
|
||||
v, ok := fn(value)
|
||||
if ok {
|
||||
m[v.Path] = v
|
||||
if v.Type == "reg" {
|
||||
if v.Path != key {
|
||||
vvv, err := regexp.Compile(v.Path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
vv = vvv
|
||||
}
|
||||
rrs[v.Path] = vv
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m[key] = value
|
||||
rrs[key] = vv
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
return func() (map[string]Route, map[string]*regexp.Regexp) {
|
||||
return m, rrs
|
||||
}
|
||||
})()
|
||||
v, ok := rs[requestURI]
|
||||
if ok && slice.IsContained(v.Method, h.C.Request.Method) {
|
||||
h.SetScene(v.Scene)
|
||||
wp.Run(h, v.ConfigHandle)
|
||||
return
|
||||
}
|
||||
for path, reg := range rrs {
|
||||
r := reg.FindAllStringSubmatch(requestURI, -1)
|
||||
if len(r) < 1 {
|
||||
return
|
||||
}
|
||||
rr := rs[path]
|
||||
if slice.IsContained(rr.Method, h.C.Request.Method) {
|
||||
h.SetScene(rr.Scene)
|
||||
h.C.Set("route", r)
|
||||
wp.Run(h, rr.ConfigHandle)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
|
@ -234,6 +234,7 @@ func (h *Handle) CommonComponents() {
|
|||
|
||||
func PreRenderTemplate(h *Handle) {
|
||||
h.C.HTML(h.Code, h.templ, h.ginH)
|
||||
h.Abort()
|
||||
h.StopPipe()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user