Golang HTTP handlers in Middleware
IT IS A DRAFT CONSIDERATION. May has ERRORS. 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 ST...

Source: DEV Community
IT IS A DRAFT CONSIDERATION. May has ERRORS. 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(