Skip to content

Commit 03e0408

Browse files
authored
Merge pull request #43 from tadvi/master
Remove panic when method not found in the tree
2 parents 5938731 + e87d364 commit 03e0408

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# gorouter [![GoDoc](https://godoc.org/github.com/xujiajun/gorouter?status.svg)](https://godoc.org/github.com/xujiajun/gorouter) <a href="https://travis-ci.org/xujiajun/gorouter"><img src="https://travis-ci.org/xujiajun/gorouter.svg?branch=master" alt="Build Status"></a> [![Go Report Card](https://goreportcard.com/badge/github.com/xujiajun/gorouter)](https://goreportcard.com/report/github.com/xujiajun/gorouter) [![Coverage Status](https://s3.amazonaws.com/assets.coveralls.io/badges/coveralls_100.svg)](https://coveralls.io/github/xujiajun/gorouter?branch=master) [![License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/xujiajun/gorouter/master/LICENSE) [![Release](https://img.shields.io/badge/release-v1.1.0-blue.svg?style=flat-square)](https://github.com/xujiajun/gorouter/releases/tag/v1.0.1) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go#routers)
1+
# gorouter [![GoDoc](https://godoc.org/github.com/xujiajun/gorouter?status.svg)](https://godoc.org/github.com/xujiajun/gorouter) <a href="https://travis-ci.org/xujiajun/gorouter"><img src="https://travis-ci.org/xujiajun/gorouter.svg?branch=master" alt="Build Status"></a> [![Go Report Card](https://goreportcard.com/badge/github.com/xujiajun/gorouter)](https://goreportcard.com/report/github.com/xujiajun/gorouter) [![Coverage Status](https://s3.amazonaws.com/assets.coveralls.io/badges/coveralls_100.svg)](https://coveralls.io/github/xujiajun/gorouter?branch=master) [![License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/xujiajun/gorouter/master/LICENSE) [![Release](https://img.shields.io/badge/release-v1.1.0-blue.svg?style=flat-square)](https://github.com/xujiajun/gorouter/releases/tag/v1.0.1) [![Awesome](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#routers)
22
`xujiajun/gorouter` is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework.
33

44
## Motivation
@@ -433,21 +433,21 @@ ok github.com/xujiajun/gorouter 15.918s
433433

434434
### Conclusions:
435435

436-
* Performance (xujiajun/gorouter,julienschmidt/httprouter and teambition/trie-mux is fast)
436+
* Performance (xujiajun/gorouter,julienschmidt/httprouter and teambition/trie-mux are fast)
437437

438-
* Memory Consumption (xujiajun/gorouter and julienschmidt/httprouter is fewer)
438+
* Memory Consumption (xujiajun/gorouter and julienschmidt/httprouter are fewer)
439439

440-
* Features (julienschmidt/httprouter not support regexp,but others support it)
440+
* Features (julienschmidt/httprouter not supports regexp,but others support it)
441441

442-
> if you want a high performance router which support regexp, maybe [xujiajun/gorouter](https://github.com/xujiajun/gorouter) is good choice.
442+
> if you want a high performance router which supports regexp, maybe [xujiajun/gorouter](https://github.com/xujiajun/gorouter) is good choice.
443443
444-
> if you want a high performance router which not support regexp, maybe [julienschmidt/httprouter](https://github.com/julienschmidt/httprouter) is good choice.
444+
> if you want a high performance router which not supports regexp, maybe [julienschmidt/httprouter](https://github.com/julienschmidt/httprouter) is good choice.
445445
446446
In the end, as julienschmidt said `performance can not be the (only) criterion for choosing a router. Play around a bit with some of the routers, and choose the one you like best.`
447447

448448
## Contributing
449449

450-
If you'd like to help out with the project. You can put up a Pull Request.
450+
If you'd like to help out with the project. You can put up a Pull Request. Thanks to all [contributors](https://github.com/xujiajun/gorouter/graphs/contributors).
451451

452452
## Author
453453

examples/graceful/main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"net/http"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
12+
"github.com/xujiajun/gorouter"
13+
)
14+
15+
func main() {
16+
logger := log.New(os.Stdout, "[gorouter] ", log.Ldate|log.Ltime)
17+
18+
mux := gorouter.New()
19+
mux.GET("/", func(w http.ResponseWriter, r *http.Request) {
20+
time.Sleep(2 * time.Second)
21+
w.Write([]byte("hello world"))
22+
logger.Println("Handle request success")
23+
})
24+
25+
srv := &http.Server{
26+
Addr: ":8181",
27+
Handler: mux,
28+
}
29+
30+
go func() {
31+
if err := srv.ListenAndServe(); err != nil {
32+
logger.Printf("listen: %s\n", err)
33+
}
34+
}()
35+
36+
graceful(srv, logger, 3*time.Second)
37+
}
38+
39+
// reference: https://gist.github.com/peterhellberg/38117e546c217960747aacf689af3dc2
40+
func graceful(hs *http.Server, logger *log.Logger, timeout time.Duration) {
41+
stop := make(chan os.Signal, 1)
42+
// Handle SIGINT and SIGTERM.
43+
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
44+
45+
<-stop
46+
47+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
48+
defer cancel()
49+
50+
logger.Printf("Shutdown with timeout: %s\n", timeout)
51+
// Stop the service gracefully.
52+
if err := hs.Shutdown(ctx); err != nil {
53+
logger.Printf("Error: %v\n", err)
54+
} else {
55+
logger.Println("Server stopped")
56+
}
57+
}

router.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
254254
}
255255

256256
if _, ok := r.trees[req.Method]; !ok {
257-
panic(fmt.Errorf("Error method or method is not registered "))
257+
r.HandleNotFound(w, req, r.middleware)
258+
return
258259
}
259260

260261
nodes := r.trees[req.Method].Find(requestUrl, false)

0 commit comments

Comments
 (0)