Core Concepts
Last updated
Was this helpful?
Last updated
Was this helpful?
To get up and running quickly, provides some reasonable defaults that work well for most of web applications:
Below is some of the functionality pulls in automatically:
Request/response logging -
Panic recovery -
Static file serving -
Any object with type can be seen as an instance of Macaron, you can have as many instances as you'd like in a single piece of code.
Handlers are the heart and soul of Macaron. A handler is basically any kind of callable function:
Non-anonymous function is also allowed for the purpose of using it in multiple routes:
Besides, one route can have as many as handlers you want to register with:
You can also optionally return a status code (only applys for string
and []byte
types):
If you add an argument to your handler, Macaron will search its list of services and attempt to resolve the dependency via type assertion:
Middleware Handlers sit between the incoming HTTP request and the router. In essence they are no different than any other Handler in Macaron. You can add a middleware handler to the stack like so:
You can have full control over the middleware stack with the Handlers
function. This will replace any handlers that have been previously set:
Middleware Handlers work really well for things like logging, authorization, authentication, sessions, gzipping, error pages and any other operations that must happen before or after an HTTP request:
Some Macaron handlers make use of the macaron.Env
global variable to provide special functionality for development environments vs production environments. It is recommended that the MACARON_ENV=production
environment variable to be set when deploying a Macaron server into a production environment.
If a handler returns something, Macaron will write the result to the current as a string:
Handlers are invoked via reflection. Macaron makes use of to resolve dependencies in a Handlers argument list. This makes Macaron completely compatible with golang's interface.
The most commonly used service in your code should be :
The following services are included with :
- HTTP request context
- Global logger for Macaron instances
- HTTP Response writer interface
- HTTP Request