Giter VIP home page Giter VIP logo

http_router's Introduction

HTTP Router

What is it?

This is an HTTP router for use in either a web framework, or on it's own using Rack. It takes a set of routes and attempts to find the best match for it. Take a look at the examples directory for how you'd use it in the Rack context.

Features

  • Ordered route resolution.
  • Supports variables, and globbing, both named and unnamed.
  • Regex support for variables.
  • Request condition support.
  • Partial matches.
  • Supports interstitial variables (e.g. /my-:variable-brings.all.the.boys/yard) and unnamed variable /one/:/two
  • Very fast and small code base (~1,000 loc).
  • Sinatra via https://github.com/joshbuddy/http_router_sinatra

Usage

Please see the examples directory for a bunch of awesome rackup file examples, with tonnes of commentary. As well, the rdocs should provide a lot of useful specifics and exact usage.

HttpRouter.new

Takes the following options:

  • :default_app - The default #call made on non-matches. Defaults to a 404 generator.
  • :ignore_trailing_slash - Ignores the trailing slash when matching. Defaults to true.
  • :middleware - Perform matching without deferring to matched route. Defaults to false.

#add(name, options)

Maps a route. The format for variables in paths is: :variable *glob

Everything else is treated literally. Optional parts are surrounded by brackets. Partially matching paths have a trailing *. Optional trailing slash matching is done with /?.

As well, you can escape the following characters with a backslash: ( ) : *

Once you have a route object, use HttpRouter::Route#to to add a destination and HttpRouter::Route#name to name it.

e.g.

  r = HttpRouter.new
  r.add('/test/:variable(.:format)').name(:my_test_path).to {|env| [200, {}, "Hey dude #{env['router.params'][:variable]}"]}
  r.add('/test').redirect("http://www.google.com/")
  r.add('/static').static('/my_file_system')

As well, you can support regex matching and request conditions. To add a regex match, use matching(:id => /\d+/). To match on a request condition you can use condition(:request_method => %w(POST HEAD)) or more succinctly request_method('POST', 'HEAD').

There are convenience methods HttpRouter#get, HttpRouter#post, etc for each request method.

Routes will not be recognized unless #to has been called on it.

#url(name or route, *args)

Generates a route. The args can either be a hash, a list, or a mix of both.

#call(env)

Recognizes and dispatches the request. env should be a Hash representing the rack environment.

#recognize(env)

Only performs recognition. env should be a Hash representing the rack environment.

http_router's People

Contributors

dstrelau avatar funny-falcon avatar jamesdabbs avatar jboursiquot avatar jimmycuadra avatar joshbuddy avatar justahero avatar lasso avatar mattetti avatar nesquena avatar reyesyang avatar san983 avatar tlewin avatar tyler 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.