1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-25 07:34:10 +02:00

added native echo.HandlerFunc support and .staticDirectoryHandler bind

This commit is contained in:
Gani Georgiev
2023-07-24 21:11:55 +03:00
parent 99ea916c14
commit 8dfc90985b
7 changed files with 6938 additions and 9881 deletions

View File

@@ -144,31 +144,19 @@ func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
}
func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
loader.Set("routerAdd", func(method string, path string, handler string, middlewares ...goja.Value) {
loader.Set("routerAdd", func(method string, path string, handler goja.Value, middlewares ...goja.Value) {
wrappedMiddlewares, err := wrapMiddlewares(executors, middlewares...)
if err != nil {
panic("[routerAdd] failed to wrap middlewares: " + err.Error())
}
pr := goja.MustCompile("", "{("+handler+").apply(undefined, __args)}", true)
wrappedHandler, err := wrapHandler(executors, handler)
if err != nil {
panic("[routerAdd] failed to wrap handler: " + err.Error())
}
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.Add(strings.ToUpper(method), path, func(c echo.Context) error {
return executors.run(func(executor *goja.Runtime) error {
executor.Set("__args", []any{c})
res, err := executor.RunProgram(pr)
executor.Set("__args", goja.Undefined())
// check for returned error
if res != nil {
if v, ok := res.Export().(error); ok {
return v
}
}
return err
})
}, wrappedMiddlewares...)
e.Router.Add(strings.ToUpper(method), path, wrappedHandler, wrappedMiddlewares...)
return nil
})
@@ -199,6 +187,37 @@ func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
})
}
func wrapHandler(executors *vmsPool, handler goja.Value) (echo.HandlerFunc, error) {
switch h := handler.Export().(type) {
case echo.HandlerFunc:
// "native" handler - no need to wrap
return h, nil
case func(goja.FunctionCall) goja.Value, string:
pr := goja.MustCompile("", "{("+handler.String()+").apply(undefined, __args)}", true)
wrappedHandler := func(c echo.Context) error {
return executors.run(func(executor *goja.Runtime) error {
executor.Set("__args", []any{c})
res, err := executor.RunProgram(pr)
executor.Set("__args", goja.Undefined())
// check for returned error
if res != nil {
if v, ok := res.Export().(error); ok {
return v
}
}
return err
})
}
return wrappedHandler, nil
default:
return nil, errors.New("unsupported goja handler type")
}
}
func wrapMiddlewares(executors *vmsPool, rawMiddlewares ...goja.Value) ([]echo.MiddlewareFunc, error) {
wrappedMiddlewares := make([]echo.MiddlewareFunc, len(rawMiddlewares))
@@ -531,6 +550,10 @@ func apisBinds(vm *goja.Runtime) {
obj := vm.NewObject()
vm.Set("$apis", obj)
obj.Set("staticDirectoryHandler", func(dir string, indexFallback bool) echo.HandlerFunc {
return apis.StaticDirectoryHandler(os.DirFS(dir), indexFallback)
})
// middlewares
obj.Set("requireRecordAuth", apis.RequireRecordAuth)
obj.Set("requireAdminAuth", apis.RequireAdminAuth)