Core Services
Last updated
Was this helpful?
Last updated
Was this helpful?
By default, Macaron injects some services to power your application, those services are known as core services, which means you can directly use them as handler arguments without any additional work.
This service is represented by type . This is the very core service for everything you do upon Macaron. It contains all the information you need for request, response, templating, data store, and inject or retrieve other services.
To use it:
Method is an optional feature that Middleware Handlers can call to yield the until after the other Handlers have been executed. This works really well for any operations that must happen after an HTTP request:
The very basic usage of cookie is just:
To use them:
Use following arguments order to set more properties: SetCookie(<name>, <value>, <max age>, <path>, <domain>, <secure>, <http only>,<expires>)
.
For example, the most advanced usage would be: SetCookie("user", "unknwon", 999, "/", "localhost", true, true, time.Now())
.
Note that order is fixed.
These two methods uses default secret string you set globally to encode and decode values.
To use them:
For people who wants even more secure cookies that change secret string every time, just use:
To use them:
To use it:
To use it:
To use it:
By default, when you try to request a directory, this service will not list directory files. Instead, it tries to find the index.html
file.
Suppose you have following directory structure:
Results:
Request URL
Match File
/html/main.html
None
/html/
index.html
/css/main.css
main.css
To use it:
This will register both public
and views
as static directories.
To use it:
To use it:
Besides, this service provides three methods to help you easily retrieve request body:
To use them:
Notice that request body can only be read once.
, , ,
There are also more secure cookie support. First, you need to call , then use it by calling:
To set/get URL parameters: / , , , ,
To get query parameters: , , , , , ,
To serve content or file: , ,
To get remote IP address:
This service can be injected by function . It is responsible for your application routing log.
This service is injected automatically when you use .
Sample output take from :
This service can be injected by function . It is responsible for recovering your application when panic happens.
This service is injected automatically when you use .
This service can be injected by function . It is responsible for serving static resources of your application, it can be injected as many times as you want if you have multiple static directories.
This service is injected automatically with directory public
when you use .
Sample output take from :
This service also accepts second argument for custom options():
In case you have multiple static directories, there is one helper function to make your life easier.
This service is represented by type . It is optional to use, but for convenience when you do not have a global level logger.
This service is injected automatically for every Macaron .
This service is represented by type . It is optional to use, normally, you should use *macaron.Context.Resp
.
This service is injected automatically for every Macaron .
This service is represented by type . It is optional to use, normally, you should use *macaron.Context.Req
.
: get request body in string
type
: get request body in []byte
type
: get request body in io.ReadCloser
type
Sometimes you need to pass type as an argument, you should use *macaron.Context.Req.Request
.
This service is injected automatically for every Macaron .