Refactor SimpleServer#mount's handler creation.
@@ -94,8 +94,17 @@ func builtinSimpleServerInstanceMethods() []*BuiltInMethodObject {
Fn: func(receiver Object) builtinMethodBody {
return func(t *thread, args []Object, blockFrame *callFrame) Object {
path := args[0].(*StringObject).Value
+ router.HandleFunc(path, newHandler(t, blockFrame))
- router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+ return receiver
+ }
+ },
+ },
+ }
+}
+
+func newHandler(t *thread, blockFrame *callFrame) func(http.ResponseWriter, *http.Request) {
+ return func(w http.ResponseWriter, r *http.Request) {
// Go creates one goroutine per request, so we also need to create a new Goby thread for every request.
thread := t.vm.newThread()
res := httpResponseClass.initializeInstance()
@@ -103,12 +112,6 @@ func builtinSimpleServerInstanceMethods() []*BuiltInMethodObject {
thread.builtInMethodYield(blockFrame, req, res)
thread = nil
setupResponse(w, r, res)
- })
-
- return receiver
- }
- },
- },
}
}
