Built-in Handlers

Go-Mojito ships with a few built-in handlers meant to cover common use-cases.

Assets Handler

The assets handler provides a turnkey solution to serve your projects assets. For that to work, it expects the assets to be stored inside your Resources directory. You can configure the assets path via handler.AssetsPrefix, which will be appended to the Resources directory path. The handler ensures that every served file's path must be within that path to protect against traversal attacks.

How to use the Assets Handler

There are 3 main ways how you can use the AssetsHandler.

package main

// imports

func main() {
  // Will register the assets handler on /assets
  handler.HandleAssets()

  // Will register the assets handler on /static
  handler.HandleAssetsOn("/static")

  // Register it yourself, helpful in custom implementations
  mojito.GET("/assets/*path", mojito.Assets)
}

View Handler

The view handler is meant to provide a quick way to register a template on a route that does not need any rendering context. It's great if you only have static content while still having the ability to split up your website into layout and sub-pages and potentially components. It will attempt to render the given template or will write the error into the response if an error occurs.

How to use the View Handler
package main

// imports

func main() {
  mojito.GET("/home", handler.View("Pages/Home"))
}