Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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)
# 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)
`xujiajun/gorouter` is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework.

## Motivation
Expand Down Expand Up @@ -433,21 +433,21 @@ ok github.com/xujiajun/gorouter 15.918s

### Conclusions:

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

* Memory Consumption (xujiajun/gorouter and julienschmidt/httprouter is fewer)
* Memory Consumption (xujiajun/gorouter and julienschmidt/httprouter are fewer)

* Features (julienschmidt/httprouter not support regexp,but others support it)
* Features (julienschmidt/httprouter not supports regexp,but others support it)

> if you want a high performance router which support regexp, maybe [xujiajun/gorouter](https://github.com/xujiajun/gorouter) is good choice.
> if you want a high performance router which supports regexp, maybe [xujiajun/gorouter](https://github.com/xujiajun/gorouter) is good choice.

> if you want a high performance router which not support regexp, maybe [julienschmidt/httprouter](https://github.com/julienschmidt/httprouter) is good choice.
> if you want a high performance router which not supports regexp, maybe [julienschmidt/httprouter](https://github.com/julienschmidt/httprouter) is good choice.

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.`

## Contributing

If you'd like to help out with the project. You can put up a Pull Request.
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).

## Author

Expand Down
57 changes: 57 additions & 0 deletions examples/graceful/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/xujiajun/gorouter"
)

func main() {
logger := log.New(os.Stdout, "[gorouter] ", log.Ldate|log.Ltime)

mux := gorouter.New()
mux.GET("/", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
w.Write([]byte("hello world"))
logger.Println("Handle request success")
})

srv := &http.Server{
Addr: ":8181",
Handler: mux,
}

go func() {
if err := srv.ListenAndServe(); err != nil {
logger.Printf("listen: %s\n", err)
}
}()

graceful(srv, logger, 3*time.Second)
}

// reference: https://gist.github.com/peterhellberg/38117e546c217960747aacf689af3dc2
func graceful(hs *http.Server, logger *log.Logger, timeout time.Duration) {
stop := make(chan os.Signal, 1)
// Handle SIGINT and SIGTERM.
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

<-stop

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

logger.Printf("Shutdown with timeout: %s\n", timeout)
// Stop the service gracefully.
if err := hs.Shutdown(ctx); err != nil {
logger.Printf("Error: %v\n", err)
} else {
logger.Println("Server stopped")
}
}
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}

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

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