aah Server Extension Point
aah server exposes the App and Request life cycle stages as server events. It is called as Server Extension Point. Function signature is the same as events (aah.EventCallbackFunc
).
aah server events are executed synchronously. Reference to Event Emitter/Publisher.
Table of Contents
Application Extension Points
Application extension points by default, a given function is executed as an added sequence unless priority
is specified.
Event: OnInit
Event OnInit
is published once the aah.AppConfig()
is loaded. At this stage, only aah.conf
config is initialized. App Variables, Routes, i18n, Security, View Engine, Logs and so on will be initialized after this event.
Supports Multiple: Yes
// As an anonymous func
func init() {
aah.OnInit(func(e *Event) {
// logic comes here
})
}
// Or define a func and supply it [recommended approach, name gets logged in log]
func loadUserConfig(e *aah.Event) {
// loading user config from /etc/myapp/myapp.conf
// merge it to aah.AppConfig().Merge(...)
}
func init() {
aah.OnInit(loadUserConfig)
// OR
aah.OnInit(loadUserConfig, 10) // with priority
}
Event: OnStart
Event OnStart
is published just before the start of aah Server
. The application is completely initialized at this stage. The server is yet to be started.
Supports Multiple: Yes
func connectDatabase(e *aah.Event) {
// logic comes here
}
func connectRedis(e *aah.Event) {
// logic comes here
}
func refreshCache(e *aah.Event) {
// logic comes here
}
func init() {
// By default, a given function is executed as an added sequence
// unless `priority` is specified.
aah.OnStart(connectDatabase)
aah.OnStart(refreshCache, 3) // with priority
aah.OnStart(connectRedis, 2) // with priority
}
Event: OnPreShutdown
Event OnPreShutdown
is published when application receives OS Signals SIGINT
or SIGTERM
and before the triggering graceful shutdown. After this event, aah triggers graceful shutdown with config value of server.timeout.grace_shutdown
.
Supports Multiple: Yes
func announceImGonnaShutdown() {
// announce it
}
func init() {
// By default, a given function is executed as an added sequence
// unless `priority` is specified.
aah.OnPreShutdown(announceImGonnaShutdown)
}
Event: OnPostShutdown
Event OnPostShutdown
is published just after the successful grace shutdown of aah server and then application does clean exit.
Supports Multiple: Yes
func disconnectDatabase(e *aah.Event) {
// logic comes here
}
func disconnectRedis(e *aah.Event) {
// logic comes here
}
func flushCache(e *aah.Event) {
// logic comes here
}
func init() {
// By default, a given function is executed as an added sequence
// unless `priority` is specified.
aah.OnPostShutdown(flushCache)
aah.OnPostShutdown(disconnectDatabase)
aah.OnPostShutdown(disconnectRedis)
}
HTTP Engine Extension Points
Event: OnRequest
Event OnRequest
is published on each incoming request to the aah server.
Note:
- At this point, the request
Route
is yet to be populated/evaluated. - The
aah.Context
object passed to the extension functions is decorated withctx.SetURL()
andctx.SetMethod()
methods. Calls to these methods will have an impact on how the request is routed.
func init() {
aah.AppHTTPEngine().OnRequest(func(e *aah.Event) {
ctx := e.Data.(*aah.Context)
// logic comes here
})
}
Event: OnPreAuth
Since v0.7 Event OnPreAuth
is published just before the Authentication and Authorization.
func init() {
aah.AppHTTPEngine().OnPreAuth(func(e *aah.Event) {
ctx := e.Data.(*aah.Context)
// logic comes here
})
}
Event: OnPostAuth
Since v0.7 Event OnPostAuth
is published once the Authentication and Authorization info gets populated into Subject.
func init() {
aah.AppHTTPEngine().OnPostAuth(func(e *aah.Event) {
ctx := e.Data.(*aah.Context)
// logic comes here
})
}
Event: OnPreReply
Event OnPreReply
is published just before writing a reply/response on the wire. At this point, the response writer is clean. i.e. Headers, Cookies, Redirects, Status Code and Response Body are not written.
Note: OnPreReply
will not get published when; Reply().Done()
was called
func init() {
aah.AppHTTPEngine().OnPreReply(func(e *aah.Event) {
ctx := e.Data.(*aah.Context)
// logic comes here
})
}
Event: OnHeaderReply
Since v0.11.0 Event OnHeaderReply
is published before writing HTTP header Status
. At this point, all the headers except the header Status
get written on the http.ResponseWriter
.
Note: OnHeaderReply
is not called when; Reply().Done()
and Reply().Redirect(...)
was called
func init() {
aah.AppHTTPEngine().OnHeaderReply(func(e *aah.Event) {
hdr := e.Data.(http.Header)
// Header instance is the direct reference to http.ResponseWritter
// Any changes reflect immediately :)
//
// logic comes here
})
}
Event: OnPostReply
Event OnPostReply
is published right after the response gets written on the wire. We can do nothing about the response, however the context has valuable information such as response bytes size, response status code, etc.
Note: OnPostReply
will not be published when; Reply().Done()
and Reply().Redirect(...)
was called
func init() {
aah.AppHTTPEngine().OnPostReply(func(e *aah.Event) {
ctx := e.Data.(*aah.Context)
// logic comes here
})
}