Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active December 15, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyond-code-github/5310180 to your computer and use it in GitHub Desktop.
Save beyond-code-github/5310180 to your computer and use it in GitHub Desktop.
Http routes as state machine
The route state machine defined in pseudo code below is able to handle the following routes:
/Api/Products/
/Api/Products/1
/Api/Categories
/Api/Categories/1
/Api/Products/1/Categories
/Api/Products/1/Categories/2
Definition:
Api : StateMachineRoot
{
constraint = "Api"
allowedtransitions = { products, customers }
}
Products: RouteState
{
constraint = "Products"
target: { ProductsController }
allowedtransitions = { ProductId }
}
ProductId: SetIntParamRouteState
{
constraint = "[0-9]"
actions: { (int value) => vars.ProductId = value };
allowedtransitions: { Categories }
}
Categories: RouteState
{
constraint = "Categories"
target = { CategoriesController }
allowedtransitions = { CategoryId }
}
CategoryId: SetIntParamRouteState
{
constraint = "[0-9]"
actions: { (int value) => vars.CategoryId = value };
}
Algorithm:
Take first route segement -> ("Api")
Find matching StateMachineRoot -> (Api)
#Process Segment
Undertake any actions present -> N/A
Set any targets present -> N/A
Take next route segment -> ("Products")
Loop available transitions
If none match the route segment
If a target has been set, try to invoke method on it according to params and verb
If no target - return 404
If a match is found
Repeat #Process Segment with remaining route segments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment