// Serve executes the provided Handler on the currently active CGI // request, if any. If there's no current CGI environment // an error is returned. The provided handler may be nil to use // http.DefaultServeMux. func Serve(handler http.Handler) os.Error { req, err := Request() if err != nil { return err } if handler == nil { handler = http.DefaultServeMux } rw := &response{ req: req, header: make(http.Header), bufw: bufio.NewWriter(os.Stdout), } handler.ServeHTTP(rw, req) if err = rw.bufw.Flush(); err != nil { return err } return nil }
// ServeHTTP dispatches the handler registered in the matched route. // // When there is a match, the route variables can be retrieved calling // mux.Vars(request). func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) { // Clean path to canonical form and redirect. // (this comes from the http package) if p := cleanPath(request.URL.Path); p != request.URL.Path { writer.Header().Set("Location", p) writer.WriteHeader(http.StatusMovedPermanently) return } var handler http.Handler if match, ok := r.Match(request); ok { handler = match.Handler } if handler == nil { if r.NotFoundHandler == nil { r.NotFoundHandler = http.NotFoundHandler() } handler = r.NotFoundHandler } defer context.DefaultContext.Clear(request) handler.ServeHTTP(writer, request) }
func loggingHandler(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { log.Printf("%s\t%s", req.RemoteAddr, req.URL) h.ServeHTTP(w, req) }) }
func loggingHandler(h http.Handler) http.Handler { return http.HandlerFunc(func(c *http.Conn, req *http.Request) { log.Stderrf("%s\t%s", c.RemoteAddr, req.Url); h.ServeHTTP(c, req); }); }