Routing

In Macaron, a route is an HTTP method paired with a URL-matching pattern. Each route can take one or more handler methods:

m.Get("/", func() {
    // show something
})

m.Patch("/", func() {
    // update something
})

m.Post("/", func() {
    // create something
})

m.Put("/", func() {
    // replace something
})

m.Delete("/", func() {
    // destroy something
})

m.Options("/", func() {
    // http options
})

m.Any("/", func() {
    // do anything
})

m.Route("/", "GET,POST", func() {
    // combine something
})

m.Combo("/").
    Get(func() string { return "GET" }).
    Patch(func() string { return "PATCH" }).
    Post(func() string { return "POST" }).
    Put(func() string { return "PUT" }).
    Delete(func() string { return "DELETE" }).
    Options(func() string { return "OPTIONS" }).
    Head(func() string { return "HEAD" })

m.NotFound(func() {
    // Custom handle for 404
})

Notes:

  • Routes are matched in the order they are defined,

  • ...but, narrow range routes have higher priority than wider range routes(see below: Matching Priority)

  • The first route that matches the request is invoked.

In some cases, HEAD method is also used wherever GET method is registered. To reduce redundant code, there is a method called SetAutoHead can help you automatically register it:

If you want to use suburl without having a huge group indent, use m.SetURLPrefix(suburl).

Named Parameters

Route patterns may include named parameters, accessible via the method *Context.Params:

Placeholders

Use a specific name to represent a route part:

Of course, : seems noising sometimes, take it out when you feels like:

Globs

Routes can be matched with globs:

What happens when * is in the middle?

Regular Expressions

Regular expressions can be used as well:

  • Regular match:

  • Mixed match:

  • Optional match:

    • /user/?:id, matches both /user/ and /user/123.

  • Shortcuts:

    • /user/:id:int, :int is shortcut for ([0-9]+).

    • /user/:name:string, :string is shortcut for ([\w]+).

Matching Priority

Matching priority of different match patterns from higher to lower:

  • Static routes:

    • /

    • /home

  • Regular expression routes:

    • /(.+).html

    • /([0-9]+).css

  • Path-extension routesL

    • /*.*

  • Placeholder routes:

    • /:id

    • /:name

  • Glob routes:

    • /*

Other notes:

  • Matching priority of same pattern is first add first match.

  • More detailed pattern gets higher matching priority:

    • /*/*/events > /*

Building URLs

You can build URLs with named parameters, to do this, you should use *Route.Name method give route a name:

Then use *Router.URLFor to build URLs with route of given name:

Using it in Go templating engine

Using it in Pongo2 templating engine

Advanced Routing

Route handlers can be stacked on top of each other, which is useful for things like authentication and authorization:

Let's see an extreme example:

Guess what's output will be? Yes, There are 5 handlers before this. There are no hard limitation of how many handlers you can have for a route, but you may wonder how does Macaron know when to stop calling next handler?

To answer this question, please consider the following example:

In this case, the output will always be There are 4 handlers before this, and the last handler never gets chance to call. Why? Because we write response in 5th handler. Thus, once any handler writes anything to the response stream, Macaron will stop calling next handler.

Group Routing

Route groups can be added too using the macaron.Group method:

Just like you can pass middlewares to a handler you can pass middlewares to groups:

Still, no hard limitation of how many nested group routes and group level handlers(middlewares) you can have.

Last updated

Was this helpful?