Skip to content

Commit

Permalink
Minor improvements to the muxchain api, allowing method mux to embed …
Browse files Browse the repository at this point in the history
…in muxchain
  • Loading branch information
stephens2424 committed Jun 3, 2014
1 parent 84ee1ad commit 18ff20b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
9 changes: 6 additions & 3 deletions muxchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func Chain(pattern string, handlers ...http.Handler) {
}

type MuxChain struct {
*http.ServeMux
Muxer
}

func (m *MuxChain) ServeHTTPChain(w http.ResponseWriter, req *http.Request, handlers ...http.Handler) {
Expand All @@ -23,8 +23,8 @@ func (m *MuxChain) ServeHTTPChain(w http.ResponseWriter, req *http.Request, hand
// of the chain is reached. If one of the handlers is a Muxer (e.g. http.ServeMux),
// the MuxChain will skip it if no pattern matches.
func (m *MuxChain) Chain(pattern string, handlers ...http.Handler) {
if m.ServeMux == nil {
m.ServeMux = http.NewServeMux()
if m.Muxer == nil {
m.Muxer = http.NewServeMux()
}
m.HandleFunc(pattern, func(w http.ResponseWriter, req *http.Request) {
if len(handlers) == 0 {
Expand Down Expand Up @@ -97,7 +97,10 @@ func handle(h http.Handler, remaining []http.Handler, w http.ResponseWriter, req

// Muxer identifies types that act as a ServeMux.
type Muxer interface {
http.Handler
Handle(pattern string, handler http.Handler)
Handler(r *http.Request) (h http.Handler, pattern string)
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}

type checked struct {
Expand Down
4 changes: 4 additions & 0 deletions muxchainutil/methodmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (m *MethodMux) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.ServeHTTP(w, req)
}

func (m *MethodMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
m.Handle(pattern, http.HandlerFunc(handler))
}

// HandleMethods registers a pattern to a handler for the given methods.
func (m *MethodMux) HandleMethods(pattern string, h http.Handler, methods ...string) {
for _, method := range methods {
Expand Down
3 changes: 3 additions & 0 deletions muxchainutil/panic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package muxchainutil

import (
"fmt"
"net/http"
"os"

"github.com/stephens2424/muxchain"
)
Expand All @@ -26,6 +28,7 @@ func (p PanicRecovery) ServeHTTPChain(w http.ResponseWriter, req *http.Request,
defer func() {
if r := recover(); r != nil {
recovery.ServeHTTP(w, req)
fmt.Fprintln(os.Stderr, r)
}
}()
muxchain.HandleChain(w, req, h...)
Expand Down
4 changes: 2 additions & 2 deletions sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"log"
"net/http"

"github.com/muxchain"
"github.com/muxchain/muxchainutil"
"github.com/stephens2424/muxchain"
"github.com/stephens2424/muxchain/muxchainutil"
)

func main() {
Expand Down

0 comments on commit 18ff20b

Please sign in to comment.