MCPcopy Create free account
hub / github.com/crossoverJie/gscript / httpHandle

Function httpHandle

internal_http.go:23–78  ·  view source on GitHub ↗
(v *Visitor, ctx *parser.FunctionCallContext)

Source from the content-addressed store, hash-verified

21var path2HttpTool map[string]*httpTool
22
23func httpHandle(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
24 paramValues := v.buildParamValues(ctx)
25 p := paramValues[0]
26 p0 := paramValues[1]
27 p1 := paramValues[2]
28 var (
29 method, path string
30 )
31 switch p.(type) {
32 case string:
33 method = fmt.Sprintf("%s", p)
34 }
35
36 switch p0.(type) {
37 case string:
38 path = fmt.Sprintf("%s", p0)
39 }
40 switch p1.(type) {
41 case *stack.FuncObject:
42 funcObject := p1.(*stack.FuncObject)
43 function := funcObject.GetFunction()
44 var h = func(w http.ResponseWriter, r *http.Request) {
45 defer func() {
46 if r := recover(); r != nil {
47 switch x := r.(type) {
48 case *log.Log:
49 http.Error(w, x.String(), http.StatusInternalServerError)
50 default:
51 panic(x)
52 }
53 }
54 }()
55 if r.Method != strings.ToUpper(method) {
56 // todo crossoverJie http panic 单独 recovery
57 log.RuntimePanic(ctx, fmt.Sprintf("http method %s not correct", method))
58 }
59 // 保存 path 与 HTTPTool 映射关系
60 if path2HttpTool == nil {
61 path2HttpTool = make(map[string]*httpTool)
62 }
63 path2HttpTool[path] = &httpTool{w: w, r: r}
64
65 // 注入 classObject 对象:HttpContext
66 // todo crossoverJie 基于该能力可以实现反射
67 class := symbol.NewClass(ctx, "HttpContext")
68 classObject := stack.NewClassObject(class)
69 pathVar := v.at.GetHttpPathVariable()
70 classObject.SetValue(pathVar, path)
71 v.executeFunctionCall(stack.NewFuncObject(function), []interface{}{classObject})
72 }
73 http.HandleFunc(path, h)
74 }
75
76 return nil
77
78}
79
80func httpRun(v *Visitor, ctx *parser.FunctionCallContext) interface{} {

Callers

nothing calls this directly

Calls 10

SetValueMethod · 0.95
RuntimePanicFunction · 0.92
NewClassFunction · 0.92
NewClassObjectFunction · 0.92
NewFuncObjectFunction · 0.92
buildParamValuesMethod · 0.80
GetHttpPathVariableMethod · 0.80
executeFunctionCallMethod · 0.80
GetFunctionMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected