Skip to content

Commit

Permalink
Merge branch 'hotfix-0.2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Aug 25, 2014
2 parents bc719d4 + e6ac24d commit 38ba4bd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
example/example
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ Hello John

- Checks JSON Content-Type automatically:
```go
// Data is helper to construct JSON
type Data map[string]interface{}

func main() {
r := router.New()
r.GET("/api/v1/settings/database/:db", func(c *router.Control) {
data := map[string]map[string]string{
"Database settings": {
data := Data{
"Database settings": Data{
"database": c.Get(":db"),
"host": "localhost",
"port": "3306",
Expand Down Expand Up @@ -113,8 +116,8 @@ func main() {

// Do something

data := map[string]map[string]string{
"Database settings": {
data := Data{
"Database settings": Data{
"database": c.Get(":db"),
"host": "localhost",
"port": "3306",
Expand Down
7 changes: 5 additions & 2 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"github.com/takama/router"
)

// Data is helper to construct JSON
type Data map[string]interface{}

func main() {
r := router.New()
r.GET("/hello/:name", func(c *router.Control) {
Expand All @@ -15,8 +18,8 @@ func main() {

// Do something

data := map[string]map[string]string{
"Database settings": {
data := Data{
"Database settings": Data{
"database": c.Get(":db"),
"host": "localhost",
"port": "3306",
Expand Down
11 changes: 6 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

/*
Package router 0.2.3 provides fast HTTP request router.
Package router 0.2.4 provides fast HTTP request router.
The router matches incoming requests by the request method and the path.
If a handle is registered for this path and method, the router delegates the
Expand Down Expand Up @@ -85,12 +85,12 @@ type Router struct {
// NotFound is called when unknown HTTP method or a handler not found.
// If it is not set, http.NotFound is used.
// Please overwrite this if need your own NotFound handler.
NotFound http.HandlerFunc
NotFound Handle

// PanicHandler is called when panic happen.
// The handler prevents your server from crashing and should be used to return
// http status code http.StatusInternalServerError (500)
PanicHandler func(c *Control)
PanicHandler Handle

// Logger activates logging for each requests
Logger bool
Expand Down Expand Up @@ -174,8 +174,8 @@ func (r *Router) Listen(hostPort string) {
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer func() {
if recovery := recover(); recovery != nil {
c := &Control{Request: req, Writer: w}
if r.PanicHandler != nil {
c := &Control{Request: req, Writer: w}
r.PanicHandler(c)
} else {
log.Println("Recovered in handler:", req.Method, req.URL.Path)
Expand Down Expand Up @@ -204,7 +204,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {

if len(allowed) == 0 {
if r.NotFound != nil {
r.NotFound(w, req)
c := &Control{Request: req, Writer: w}
r.NotFound(c)
} else {
http.NotFound(w, req)
}
Expand Down

0 comments on commit 38ba4bd

Please sign in to comment.