Custom services
The Flame instance is building on top of the open in new window to provide service injections for your handlers. Both and flamego.Context
open in new window have embeded the inject.TypeMapper
that allow you to inject services at anywhere you want.
The Map
method is used to inject services (aka. map values to their own types), the injected service can be a concrete type () or an interface (io.Writer
open in new window):
The MapTo
method works similar to Map
but instead of mapping values to their own types, it allows you to map values to interfaces:
buf := &bytes.Buffer{}
f := flamego.New()
f.MapTo(buf, (*io.Writer)(nil))
You may also use the MapTo
method to transform interfaces to other interfaces:
WARNING
When you inject services to the Flame instance without attaching to any route, these injected services are considered as global services, which are available for all handlers of the Flame instance.
There are two ways you can inject a global service, directly call Map
or on the Flame instance, or through a using the Use
method:
db := database.New()
f := flamego.New()
// or
f := flamego.New()
f.Use(func(c flamego.Context) {
db := database.New()
c.Map(db)
})
When you inject services to a group of routes, these injected services are considered as group services, which are only available for all handlers within the group.
You can only inject a group service through a group middleware:
In the above example, the *database.User
is only available to the group of routes on line 3 to 7. Trying to use it outside the group will cause panic as illustrated on line 14.
You can only inject a route-level service through a :
f := flamego.New()
user := database.GetUser()
c.Map(user)
},
f.Get("/settings", func(user *database.User) {
...
}),
)
f.Get("/repo", func(user *database.User) {
// This handler will cause panic because *database.User is not available
})
In the above example, the *database.User
is only available to the route on line 7 to 9. Trying to use it in all other routes will cause panic as illustrated on line 11.
Overriding services
Injected services can be overridden when you’re not happy with the service functionality or behaviors provided by the other middleware.
Here is an example of overriding a global service at the route level:
When you run the above program and do curl http://localhost:2830/
, the following content are printed to your terminal: