Created
August 22, 2015 13:20
-
-
Save mmirolim/13a91d386af45878f615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "time" | |
| ) | |
| // handlerFuncs sayHello for http://localhost:4000/hello url | |
| func sayHello(w http.ResponseWriter, r *http.Request) { | |
| reply := "Hello, nice day isn't it :)" | |
| // print to console | |
| log.Println("reply =>", reply) | |
| // write to our response (data received by Browser) | |
| fmt.Fprint(w, reply) | |
| } | |
| // handleFuncs homePage func executed when url will be http://localhost:4000 | |
| func homePage(w http.ResponseWriter, r *http.Request) { | |
| reply := "This is Awesome Home Page" | |
| // print to console | |
| log.Println("reply =>", reply) | |
| // write to our response (data received by Browser) | |
| fmt.Fprint(w, reply) | |
| } | |
| // this will add loggin before and after some h which is handlerFunc executed | |
| func logReq(h http.HandlerFunc) http.HandlerFunc { | |
| // wrap h argument which is HandlerFunc and add functionality to it | |
| // we should return http.HandlerFunc so the signature | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| // this is logger decorator | |
| log.Println("log => something before request") | |
| // call and execute func passed as argument | |
| h(w, r) | |
| // do something after execution of h func | |
| log.Println("log => after request") | |
| } | |
| } | |
| // this will add measuring execution time of handlerFunc passed as h argument | |
| func measureExecTime(h http.HandlerFunc) http.HandlerFunc { | |
| // wrap handlerFunc to add some functionality | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| // for measuring time we need start time and stop time | |
| // to get duration of action | |
| start := time.Now() | |
| // call h which is HandlerFunc | |
| h(w, r) | |
| // print to console execution duration with time.Since(Start)) | |
| log.Println("handler execution time =>", time.Since(start)) | |
| } | |
| } | |
| func main() { | |
| // register url and handlerFunc called for "/" url | |
| http.HandleFunc("/", homePage) | |
| // register url and handlerFunc for "/hello" url | |
| // with measuring execution time and loggin features | |
| http.HandleFunc("/hello", measureExecTime(logReq(sayHello))) | |
| // write that server starting in console | |
| fmt.Println("Starting Server ...") | |
| // start server and check for errors, maybe port is busy and it could | |
| // not start | |
| err := http.ListenAndServe("localhost:4000", nil) | |
| // fatal (stop program) on errors | |
| if err != nil { | |
| // fatal and log error | |
| log.Fatal("Server could not started", err) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "time" | |
| ) | |
| // handlerFuncs sayHello for http://localhost:4000/hello url | |
| func sayHello(w http.ResponseWriter, r *http.Request) { | |
| reply := "Hello, nice day isn't it :)" | |
| // print to console | |
| log.Println("reply =>", reply) | |
| // write to our response (data received by Browser) | |
| fmt.Fprint(w, reply) | |
| } | |
| // handleFuncs homePage func executed when url will be http://localhost:4000 | |
| func homePage(w http.ResponseWriter, r *http.Request) { | |
| reply := "This is Awesome Home Page" | |
| // print to console | |
| log.Println("reply =>", reply) | |
| // write to our response (data received by Browser) | |
| fmt.Fprint(w, reply) | |
| } | |
| // this will add loggin before and after some h which is handlerFunc executed | |
| func logReq(h http.HandlerFunc) http.HandlerFunc { | |
| // wrap h argument which is HandlerFunc and add functionality to it | |
| // we should return http.HandlerFunc so the signature | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| // this is logger decorator | |
| log.Println("log => something before request") | |
| // call and execute func passed as argument | |
| h(w, r) | |
| // do something after execution of h func | |
| log.Println("log => after request") | |
| } | |
| } | |
| // this will add measuring execution time of handlerFunc passed as h argument | |
| func measureExecTime(h http.HandlerFunc) http.HandlerFunc { | |
| // wrap handlerFunc to add some functionality | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| // for measuring time we need start time and stop time | |
| // to get duration of action | |
| start := time.Now() | |
| // call h which is HandlerFunc | |
| h(w, r) | |
| // print to console execution duration with time.Since(Start)) | |
| log.Println("handler execution time =>", time.Since(start)) | |
| } | |
| } | |
| // common middleware for request, you can stack them | |
| func commonAction(h http.HandlerFunc) http.HandlerFunc { | |
| return measureExecTime(logReq(h)) | |
| } | |
| func main() { | |
| // register url and handlerFunc called for "/" url | |
| http.HandleFunc("/", commonAction(homePage)) | |
| // register url and handlerFunc for "/hello" url | |
| // with measuring execution time and loggin features | |
| http.HandleFunc("/hello", commonAction(sayHello)) | |
| // write that server starting in console | |
| fmt.Println("Starting Server ...") | |
| // start server and check for errors, maybe port is busy and it could | |
| // not start | |
| err := http.ListenAndServe("localhost:4000", nil) | |
| // fatal (stop program) on errors | |
| if err != nil { | |
| // fatal and log error | |
| log.Fatal("Server could not started", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment