2023-05-06 14:57:34 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fthvgb1/wp-go/app/theme/wp"
|
2023-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
2023-05-06 14:57:34 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2024-04-17 17:39:36 +00:00
|
|
|
"net/http"
|
2023-05-06 14:57:34 +00:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
// Route
|
|
|
|
// Type value equal const or reg
|
2023-05-06 14:57:34 +00:00
|
|
|
type Route struct {
|
2023-05-07 14:56:23 +00:00
|
|
|
Path string
|
|
|
|
Scene string
|
|
|
|
Method []string
|
|
|
|
Type string
|
2023-05-06 14:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]()
|
2024-01-24 07:13:29 +00:00
|
|
|
reload.Append(func() {
|
2023-05-06 14:57:34 +00:00
|
|
|
r.Flush()
|
|
|
|
regRoutes.Flush()
|
2024-01-24 07:13:29 +00:00
|
|
|
}, "wp-routers")
|
2023-05-06 14:57:34 +00:00
|
|
|
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",
|
|
|
|
// }
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
|
|
|
|
var RegRouteFn = reload.BuildValFnWithAnyParams("regexRoute", RegRouteHook)
|
|
|
|
|
|
|
|
func RegRouteHook(_ ...any) 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 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[v.Path] = v
|
|
|
|
if v.Type != "reg" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v.Path != key {
|
|
|
|
vvv, err := regexp.Compile(v.Path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2023-05-06 14:57:34 +00:00
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
vv = vvv
|
2023-05-06 14:57:34 +00:00
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func ResolveRoute(h *wp.Handle) {
|
|
|
|
requestURI := h.C.Request.RequestURI
|
|
|
|
rs, rrs := RegRouteFn()()
|
2023-05-06 14:57:34 +00:00
|
|
|
v, ok := rs[requestURI]
|
|
|
|
if ok && slice.IsContained(v.Method, h.C.Request.Method) {
|
|
|
|
h.SetScene(v.Scene)
|
2023-05-07 14:56:23 +00:00
|
|
|
wp.Run(h, nil)
|
2024-01-18 15:29:50 +00:00
|
|
|
h.Abort()
|
2023-05-06 14:57:34 +00:00
|
|
|
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)
|
2023-05-07 14:56:23 +00:00
|
|
|
wp.Run(h, nil)
|
2024-01-18 15:29:50 +00:00
|
|
|
h.Abort()
|
2023-05-06 14:57:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 17:39:36 +00:00
|
|
|
h.C.Status(http.StatusNotFound)
|
|
|
|
h.Abort()
|
2023-05-06 14:57:34 +00:00
|
|
|
}
|