mirror of
https://github.com/kataras/iris.git
synced 2026-01-15 07:55:57 +00:00
Give an easier and more permant solution for https://github.com/kataras/iris/pull/689
Former-commit-id: 8dc16d15f7bd14cb98b91b16d7d6b1bd756132bd
This commit is contained in:
@@ -31,7 +31,8 @@ It doesn't always contain the "best ways" but it does cover each important featu
|
||||
- Graceful Shutdown
|
||||
* [using the `RegisterOnInterrupt`](http-listening/graceful-shutdown/default-notifier/main.go)
|
||||
* [using a custom notifier](http-listening/graceful-shutdown/custom-notifier/main.go)
|
||||
|
||||
* [notify on shutdown](http-listening/graceful-shutdown/notify-on-shutdown/main.go)
|
||||
|
||||
### Configuration
|
||||
|
||||
- [Functional](configuration/functional/main.go)
|
||||
|
||||
51
_examples/http-listening/notify-on-shutdown/main.go
Normal file
51
_examples/http-listening/notify-on-shutdown/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
stdContext "context"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/host"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.HTML("<h1>Hello, try to refresh the page after ~10 secs</h1>")
|
||||
})
|
||||
|
||||
app.Logger().Info("Wait 10 seconds and check your terminal again")
|
||||
// simulate a shutdown action here...
|
||||
go func() {
|
||||
<-time.After(10 * time.Second)
|
||||
timeout := 5 * time.Second
|
||||
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
|
||||
defer cancel()
|
||||
// close all hosts, this will notify the callback we had register
|
||||
// inside the `configureHost` func.
|
||||
app.Shutdown(ctx)
|
||||
}()
|
||||
|
||||
// start the server as usual, the only difference is that
|
||||
// we're adding a second (optional) function
|
||||
// to configure the just-created host supervisor.
|
||||
//
|
||||
// http://localhost:8080
|
||||
// wait 10 seconds and check your terminal.
|
||||
app.Run(iris.Addr(":8080", configureHost), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
|
||||
}
|
||||
|
||||
func configureHost(su *host.Supervisor) {
|
||||
// here we have full access to the host that will be created
|
||||
// inside the `Run` function.
|
||||
//
|
||||
// we register a shutdown "event" callback
|
||||
su.RegisterOnShutdown(func() {
|
||||
println("server is closed")
|
||||
})
|
||||
// su.RegisterOnError
|
||||
// su.RegisterOnServe
|
||||
}
|
||||
Reference in New Issue
Block a user