MCPcopy Create free account
hub / github.com/cloudwan/gohan / httpServer

Function httpServer

extension/gohanscript/lib/httpserver.go:64–179  ·  view source on GitHub ↗
(stmt *gohanscript.Stmt)

Source from the content-addressed store, hash-verified

62}
63
64func httpServer(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
65 return func(globalContext *gohanscript.Context) (interface{}, error) {
66 m := martini.Classic()
67 var mutex = &sync.Mutex{}
68 history := []interface{}{}
69 server := map[string]interface{}{
70 "history": history,
71 }
72 m.Handlers()
73 m.Use(middleware.Logging())
74 m.Use(martini.Recovery())
75 rawBody := util.MaybeMap(stmt.RawData["http_server"])
76 paths := util.MaybeMap(rawBody["paths"])
77 middlewareCode := util.MaybeString(rawBody["middleware"])
78 if middlewareCode != "" {
79 vm := gohanscript.NewVM()
80 err := vm.LoadString(stmt.File, middlewareCode)
81
82 if err != nil {
83 return nil, err
84 }
85 m.Use(func(w http.ResponseWriter, r *http.Request) {
86 context := globalContext.Extend(nil)
87 fillInContext(context.Data(), r, w, nil)
88
89 reqData, _ := ioutil.ReadAll(r.Body)
90 buff := ioutil.NopCloser(bytes.NewBuffer(reqData))
91 r.Body = buff
92 var data interface{}
93 if reqData != nil {
94 json.Unmarshal(reqData, &data)
95 }
96
97 context.Set("request", data)
98 vm.Run(context.Data())
99 })
100 }
101 m.Use(func(w http.ResponseWriter, r *http.Request) {
102 reqData, _ := ioutil.ReadAll(r.Body)
103 buff := ioutil.NopCloser(bytes.NewBuffer(reqData))
104 r.Body = buff
105 var data interface{}
106 if reqData != nil {
107 json.Unmarshal(reqData, &data)
108 }
109 mutex.Lock()
110 server["history"] = append(server["history"].([]interface{}),
111 map[string]interface{}{
112 "method": r.Method,
113 "path": r.URL.String(),
114 "data": data,
115 })
116 mutex.Unlock()
117 })
118 for path, body := range paths {
119 methods, ok := body.(map[string]interface{})
120 if !ok {
121 continue

Callers

nothing calls this directly

Calls 15

LoadStringMethod · 0.95
RunMethod · 0.95
serveResponseFunction · 0.85
HandlersMethod · 0.80
MaybeMapMethod · 0.80
MaybeStringMethod · 0.80
fillInContextFunction · 0.70
SetMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65
GetMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected