-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp.go
More file actions
50 lines (43 loc) · 1.02 KB
/
Copy pathhttp.go
File metadata and controls
50 lines (43 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package expr
import (
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
"github.com/mrhaoxx/OpenNG/http"
"github.com/mrhaoxx/OpenNG/ui"
"github.com/mrhaoxx/OpenNG/utils"
"github.com/rs/zerolog/log"
)
type httpexprbased struct {
*vm.Program
}
func (e *httpexprbased) HandleHTTP(ctx *http.HttpCtx) http.Ret {
output, err := expr.Run(e.Program, ctx)
if err != nil {
panic(err)
}
ret, _ := output.(bool)
return http.Ret(ret)
}
func (e *httpexprbased) Hosts() utils.GroupRegexp {
return nil
}
func init() {
ui.Register("expr::http", func(spec *ui.ArgNode) (any, error) {
expression := spec.ToString()
log.Debug().
Str("expression", expression).
Msg("new http expr backend")
program, err := expr.Compile(expression, expr.Env(&http.HttpCtx{}), expr.AsBool(), expr.Patch(MethodAsFuncPatcher{}),
caller)
if err != nil {
return nil, err
}
return &httpexprbased{
Program: program,
}, nil
}, ui.Assert{
Type: "string",
Required: true,
Desc: "expression-based HTTP backend",
})
}