-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17b219f
commit 322f2f4
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
*.coverprofile |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: go | ||
go: | ||
- 1.7 | ||
before_install: | ||
- go get -t -v ./... | ||
- go get github.com/modocache/gover | ||
- go get github.com/mattn/goveralls | ||
script: | ||
- go test -v -coverprofile=mux.coverprofile | ||
- gover | ||
- goveralls -coverprofile=mux.coverprofile -service=travis-ci |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test: | ||
go test -v | ||
|
||
cover: | ||
rm -rf *.coverprofile | ||
go test -coverprofile=mux.coverprofile | ||
gover | ||
go tool cover -html=mux.coverprofile |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# mux | ||
# mux | ||
[![Build Status](https://travis-ci.org/go-http-utils/mux.svg?branch=master)](https://travis-ci.org/go-http-utils/mux) | ||
[![Coverage Status](https://coveralls.io/repos/github/go-http-utils/mux/badge.svg?branch=master)](https://coveralls.io/github/go-http-utils/mux?branch=master) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package mux | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/DavidCai1993/routing" | ||
"github.com/go-http-utils/headers" | ||
) | ||
|
||
// Version is this package's version number. | ||
const Version = "0.0.1" | ||
|
||
// Handler responds to an HTTP request. | ||
type Handler interface { | ||
ServeHTTP(http.ResponseWriter, *http.Request, map[string]string) | ||
} | ||
|
||
// Mux is the HTTP request multiplexer. | ||
type Mux struct { | ||
root map[string]*routing.Node | ||
} | ||
|
||
// New returns a new mux. | ||
func New() *Mux { | ||
return &Mux{} | ||
} | ||
|
||
// Handle registers the handler for the given method and url. | ||
func (m *Mux) Handle(method string, url string, handler Handler) *Mux { | ||
if _, ok := m.root[method]; !ok { | ||
m.root[method] = routing.New() | ||
} | ||
|
||
m.root[method].Define(url, handler) | ||
|
||
return m | ||
} | ||
|
||
func (m Mux) ServeHTTP(res http.ResponseWriter, req *http.Request) { | ||
uri := req.RequestURI | ||
method := req.Method | ||
|
||
if method == http.MethodOptions { | ||
allows := []string{} | ||
res.WriteHeader(http.StatusNoContent) | ||
|
||
for m, n := range m.root { | ||
if _, _, ok := n.Match(uri); ok { | ||
allows = append(allows, m) | ||
} | ||
} | ||
|
||
res.Header().Add(headers.Allow, strings.Join(allows, ", ")) | ||
|
||
return | ||
} | ||
|
||
if method == http.MethodHead { | ||
method = http.MethodGet | ||
} | ||
|
||
node, ok := m.root[method] | ||
|
||
if !ok { | ||
res.WriteHeader(http.StatusMethodNotAllowed) | ||
res.Write([]byte(http.StatusText(http.StatusMethodNotAllowed))) | ||
|
||
return | ||
} | ||
|
||
handler, params, ok := node.Match(uri) | ||
|
||
if !ok { | ||
res.WriteHeader(http.StatusNotFound) | ||
res.Write([]byte(http.StatusText(http.StatusNotFound))) | ||
|
||
return | ||
} | ||
|
||
handler.(Handler).ServeHTTP(res, req, params) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package mux |