Skip to content
Matthew Dawson edited this page Apr 8, 2013 · 2 revisions

The backend system for dealing with HTTP is meant to be simple and handle a few issues in a simple manner. It provides a couple of features:

  1. A regex based router for URLs. It matches URLs to a regex string, which is generated from a special passed in string.
  2. A simple restful API generator. Using the above router, this allows for quick and easy generation of restful API with similar features. Note it only calls functions which then must be created manually.

Regex Router

The regex router is a simple re-usable system component. It is a regular handler that can be plugged into any other handler. It processes incoming URLs, and calls registered handlers when it finds a match to handler regex. It has two functions:

Handle

Handle accepts a path to match against. Anytime it finds part of the path enclosed in {}, it will match that portion of the url and send it to the handler function in an appropriate map, using the contents of the {} as the key. Currently no escaping system is used to allow for {} to appear in URLs. Also, no escaping of dangerous regex characters is preformed. Sending in characters like *()[] will cause issues if not properly escaped. This can also be used to make more complicated URLs if wanted.

ServeHTTP

This provides the standard http handler function. It uses the provided url to match against regexes made in Handle, and calls the appropriate function. Note that it calls a slightly modified Handler function, to allow for the data extracted from the URL to be passed in.

Restful API generator

Currently not finished, this API generator simple registers a pre-defined set of urls. These urls, if called, trigger a function that then attempts to call an appropriate function on a registered object. It uses type assertions to find out if the object implements the method, otherwise it just throws a regular 404 error.

Clone this wiki locally