Giter VIP home page Giter VIP logo

router's Introduction

Router

A fast standalone router for Go.

Using https://github.com/cypriss/golang-mux-benchmark, Router was at least 2.5x faster (and up to 382x faster).

Usage

Create, instance, configure your routes, hook into Go's HTTP server:

import (
  "github.com/karlseguin/router"
  "net/http"
  "log"
)

func main() {
  router := router.New(router.Configure())
  router.Get("/users", userList)
  router.Get("/users/:id", userShow)

  s := &http.Server{
    Handler:        router,
  }
  log.Fatal(s.ListenAndServe())
}

func userList(out http.ResponseWriter, req *router.Request) {
  ...
}

func userShow(out http.ResponseWriter, req *router.Request) {
  id := req.Param("id")
  ...
}

Notice that userList and userShow take a *router.Request and not a *http.Request. This is to expose the Param method.

Methods

All methods used to setup a route expect two parameters:

  • path string
  • handler func(res http.ResponseWriter, req *router.Request)

The methods are:

  • Get
  • Post
  • Put
  • Delete
  • Patch
  • Purge
  • Head
  • Options

The convenience method All sets up a route for all methods. This can be overwritten by setting a specific handler before calling All:

router.Get("/power", testHandler("get-9000"))
router.All("/power", testHandler("9000"))

Prefix matches

A route that ends with a '*' will do a prefix match on the incoming URL:

router.Delete("/users/*", users)

Parameter matches are evaluated before prefix matches are considered. Given:

router.Put("/users/:id", userShow)
router.Put("/users/ad*", userDebug)

and a request to /users/ad123, userShow will be executed. However, a request to /users/ad/333 will execute userDebug.

Prefix matching is case insensitive.

Constraints

Constraints can be placed on parameters:

route.Delete("/users/:id(^\\d+$)", userDelete)

You'll very likely always want to bind to the start and end (^ and $), but this isn't automated in order to give you the flexibility of doing a partial match.

The code will panic on an invalid regular expression.

Postfixes

A parameter can be followed by a postfix value, such as an extension:

route.Get("/users/:id:.json", showUser)

404

Specify a handler for not found requests:

router.NotFound(notFound)


func notFound(out http.ResponseWriter, req *router.Request) {
  ...
}

A default not found handler is used if none is provided

router's People

Contributors

karlseguin avatar

Watchers

James Cloos avatar Tim Lieberman 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.