Skip to content

Instantly share code, notes, and snippets.

@codeblooded
Created December 20, 2017 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeblooded/1a8119f8ab2a9e83d8503abed56a61fd to your computer and use it in GitHub Desktop.
Save codeblooded/1a8119f8ab2a9e83d8503abed56a61fd to your computer and use it in GitHub Desktop.
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