Giter Site home page Giter Site logo

vermouth's Introduction

Vermouth

Vermouth is context friendly web framework. This has dead simple and flexible interface. This project is heavily inspired by negroni and kami.

Features

  • flexible middleware stack like negroni

  • include httprouter based router

  • context-based graceful shutdown support

Examples

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

Middleware stack

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

Graceful shutdown support

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

Contribution

  1. Fork (https://github.com/bluele/vermouth/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

Author

Jun Kimura

vermouth's People

Contributors

bluele avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.