Last active
December 15, 2015 19:19
-
-
Save beyond-code-github/5310180 to your computer and use it in GitHub Desktop.
Http routes as state machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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