Created
December 20, 2017 22:17
-
-
Save codeblooded/1a8119f8ab2a9e83d8503abed56a61fd to your computer and use it in GitHub Desktop.
This file contains 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" | |
"time" | |
"net/http" | |
) | |
func chain(handleFuncs ...func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
for _, fn := range handleFuncs { | |
fn(w, r) | |
} | |
} | |
} | |
func log(w http.ResponseWriter, r *http.Request) { | |
t := time.Now() | |
fmt.Printf("%s - - [%s]", r.RemoteAddr, t) | |
} | |
func index(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "hello, world!") | |
} | |
func main() { | |
http.HandleFunc("/", chain(log, index)) | |
http.ListenAndServe(":4093", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment