-
Notifications
You must be signed in to change notification settings - Fork 8
Routing
Once you start the app with app.Start() then you can start routing requests like:
function init(){
app.Start()
app.Get ( "/index" , index )
app.Get ( "/products" , products )
app.Get ( "/product/new" , newProduct )
app.Post( "/product/new" , saveProduct )
app.Get ( "/product/(.*)" , viewProduct )
app.Get ( "/category/(.*)" , category )
}
You can use any regular expression you like, being the most used "entity/(.*)" to match an id for a particular data entity.
The values extracted from the patterns are stored in an array named Values in the app context, from where you can access them in numeric order of appearance in the pattern like:
pattern: /product/(.*)
request: /product/1234
results:
id := ctx.Values[0] // 1234
pattern: /product/(.*)/(.*)
request: /product/computers/1234567890
results:
category := ctx.Values[0] // computers
productid := ctx.Values[1] // 1234567890
Note: in a future version we will implement named patterns like /product/{category}/{id}
Important note: since the pattern "/" can cause unexpected behavior (our bug, not theirs), we recommend using the "/index" pattern to route any request for the index page. We internally map "/" requests to the "/index" handler so rest assured it will be properly routed.
All your views should be implemented with this signature:
function index(ctx app.Context){ /* do stuff with ctx */ }
function Blog(ctx app.Context){ /* do stuff with ctx */ }
function Hello(ctx app.Context){ /* do stuff with ctx */ }
where "ctx" can be named whatever you want, but must be of app.Context type.
To understand the power of app.Context read the next section.