GithubHelp home page GithubHelp logo

louisyoo / bone Goto Github PK

View Code? Open in Web Editor NEW

This project forked from go-zoo/bone

0.0 2.0 0.0 2.4 MB

Lightning Fast HTTP Multiplexer

Home Page: http://go-zoo.github.io/bone

License: MIT License

Go 98.55% CSS 0.45% HTML 1.00%

bone's Introduction

bone GoDoc Build Status Codeship Status for squiidz/bone

What is bone ?

Bone is a lightweight and lightning fast HTTP Multiplexer for Golang. It support :

  • URL Parameters
  • REGEX Parameters
  • Wildcard routes
  • Router Prefix
  • Sub Router, mux.SubRoute(), support most standard router (bone, gorilla/mux, httpRouter etc...)
  • Http method declaration
  • Support for http.Handler and http.HandlerFunc
  • Custom NotFound handler
  • Respect the Go standard http.Handler interface

alt tag

Speed

- BenchmarkBoneMux        10000000               118 ns/op
- BenchmarkZeusMux          100000               144 ns/op
- BenchmarkHttpRouterMux  10000000               134 ns/op
- BenchmarkNetHttpMux      3000000               580 ns/op
- BenchmarkGorillaMux       300000              3333 ns/op
- BenchmarkGorillaPatMux   1000000              1889 ns/op

These test are just for fun, all these router are great and really efficient. Bone do not pretend to be the fastest router for every job.

Example

package main

import(
  "net/http"

  "github.com/go-zoo/bone"
)

func main () {
  mux := bone.New()

  // mux.Get, Post, etc ... takes http.Handler
  mux.Get("/home/:id", http.HandlerFunc(HomeHandler))
  mux.Get("/profil/:id/:var", http.HandlerFunc(ProfilHandler))
  mux.Post("/data", http.HandlerFunc(DataHandler))

  // Support REGEX Route params
  mux.Get("/index/#id^[0-9]$", http.HandleFunc(IndexHandler))

  // Handle take http.Handler
  mux.Handle("/", http.HandlerFunc(RootHandler))

  // GetFunc, PostFunc etc ... takes http.HandlerFunc
  mux.GetFunc("/test", Handler)

  http.ListenAndServe(":8080", mux)
}

func Handler(rw http.ResponseWriter, req *http.Request) {
  // Get the value of the "id" parameters.
  val := bone.GetValue(req, "id")

  rw.Write([]byte(val))
}

Changelog

Update 25 September 2015

  • Add support for Sub router

Example :

func main() {
    mux := bone.New()
    sub := mux.NewRouter()

    sub.GetFunc("/test/example", func(rw http.ResponseWriter, req *http.Request) {
        rw.Write([]byte("From sub router !"))
    })

    mux.SubRoute("/api", sub)

    http.ListenAndServe(":8080", mux)
}

Update 26 April 2015

  • Add Support for REGEX parameters, using # instead of :.
  • Add Mux method mux.GetFunc(), mux.PostFunc(), etc ..., takes http.HandlerFunc instead of http.Handler.

Example :

func main() {
    mux.GetFunc("/route/#var^[a-z]$", handler)
}

func handler(rw http.ResponseWriter, req *http.Request) {
    bone.GetValue(req, "var")
}

Update 29 january 2015

  • Speed improvement for url Parameters, from ~ 1500 ns/op to ~ 1000 ns/op .

Update 25 december 2014

After trying to find a way of using the default url.Query() for route parameters, i decide to change the way bone is dealing with this. url.Query() is too slow for good router performance. So now to get the parameters value in your handler, you need to use bone.GetValue(req, key) instead of req.Url.Query().Get(key). This change give a big speed improvement for every kind of application using route parameters, like ~80x faster ... Really sorry for breaking things, but i think it's worth it.

TODO

  • DOC
  • More Testing
  • Debugging
  • Optimisation

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write Tests!
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

License

MIT

Links

Libs

  • Errors dump for Go : Trash
  • Middleware Chaining module : Claw

bone's People

Contributors

abest0 avatar bitdeli-chef avatar dushmis avatar rpmoore avatar sdkie avatar stuartnelson3 avatar vahe avatar

Watchers

 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.