IT IS A DRAFT CONSIDERATION. May has ERRORS.
UPD: The main inside of this text is that Middleware mechanis in Golang is not write a chain of method that call each other. But! Return one method, that in it's core a wrapped "onion" of one closure calls anoter. If it's correct to say. We return a method, that calls a method, that calls a method, and all these methods are required from arguments of the call, and all are in Closure Scope.
If start to consider OS Server Process as an average UNIX OS Process (using very rough simplification): that Process has STDOUT to Write Respond, and STDIN to Read Request from a client. That's clear STDOUT to Write and STDIN to Read.
Next let's wrap these STDOUT and STDIN in Objects described by Interfaces. STDOUT would be http.Respnose to Write, and STDIN would be http.Requst to read from.
Eventually the handling of the request will be a single method ServerHTTP(w http.Responce, r * http.Request). It is a singe method with just two argument one for write and one for read.
Having one method we able to add wrapping methods f1(w,r){ f2(w,r){ f3(w,r){ ServeHTTP(w,r)}}}}.
Also we could put w and r in closures that not writing methods all the time in one place but share them among each other.
// w and r in the Closure.
f1 := func(handler1){ do1(); handler1();}
f2 := func(handler2){ do2(); handler2();}
f3 := func(){ do3(); ServeHTTP(w,r);}
f1(f2(f3()));
Something like hat in a draft with a simplification.
Something like hat in a draft with a simplification.
Yes, it Has Errors. Need to clarify the way a Handler Called. And the way it Return. But the Idea is Correct.
The main thing is: We just create a new Handler method by chain of calls of Middlware - that wrap, wrap, wrap, wrap initial ServeHTTP by Methods with Clouser. Middleware not a function to call, it is a funtion that crate function for call.
That's it. Thanks God, Jesus Christ! The main issue of understanding is closed.
Top comments (0)