Vermouth is context friendly web framework. This has dead simple and flexible interface. This project is heavily inspired by negroni and kami.
-
flexible middleware stack like negroni
-
include httprouter based router
-
context-based graceful shutdown support
package main
import (
"github.com/bluele/vermouth"
"context"
"fmt"
"net/http"
)
func main() {
vm := vermouth.New()
vm.Use("/", func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
fmt.Fprint(w, "start:")
defer fmt.Fprint(w, ":end")
// call next middleware
next(w, vermouth.WithValue(r, "key", "value"))
})
vm.Get("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, r.Context().Value("key").(string))
})
vm.Serve(":3000")
}
// $ curl 127.0.0.1:3000/
// start:value:end
vermouth implements hierarchical structures like negroni. Here is example:
vm.Use("/", Middleware1)
vm.Use("/", Middleware2)
vm.Get("/", HTTPHandler)
In above case, normally call stack is:
Request ->
Middleware1 -> Middleware2 ->
HTTPHandler ->
Middleware2 -> Middleware1 ->
Response
Vermouth includes context-based graceful shutdown support.
Vermouth server observe the status of context object. If context.Done()
returns a value, vermouth will be shutdown gracefully.
See following example:
func main() {
// This server will be shutdown after 10 sec.
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
vm := vermouth.New().WithContext(ctx)
vm.Get("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello")
})
vm.Serve(":3000")
log.Println("shutdown...")
}
See detail configurations: godoc, tylerb/graceful
- Fork (https://github.com/bluele/vermouth/fork)
- Create a feature branch
- Commit your changes
- Rebase your local changes against the master branch
- Run test suite with the
go test ./...
command and confirm that it passes - Run
gofmt -s
- Create new Pull Request
Jun Kimura