Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Dec 3, 2016
1 parent 17b219f commit 322f2f4
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ _testmain.go
*.exe
*.test
*.prof
*.coverprofile
11 changes: 11 additions & 0 deletions .travis.yml
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
8 changes: 8 additions & 0 deletions Makefile
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
4 changes: 3 additions & 1 deletion README.md
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)
82 changes: 82 additions & 0 deletions mux.go
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)
}
1 change: 1 addition & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package mux

0 comments on commit 322f2f4

Please sign in to comment.