-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
main.go
59 lines (48 loc) · 1.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"time"
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<h1>Hello, try to refresh the page after ~5 secs</h1>")
})
app.Logger().Info("Wait 5 seconds and check your terminal again")
// simulate a shutdown action here...
go func() {
<-time.After(5 * time.Second)
timeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// close all hosts, this will notify the callback we had register
// inside the `configureHost` func.
app.Shutdown(ctx)
}()
// app.ConfigureHost(configureHost) -> or pass "configureHost" as `app.Addr` argument, same result.
// 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))
time.Sleep(500 * time.Millisecond) // give time to the separate go routine(`onServerShutdown`) to finish.
/* See
iris.RegisterOnInterrupt(callback) for global catch of the CTRL/CMD+C and OS events.
Look at the "graceful-shutdown" example for more.
*/
}
func onServerShutdown() {
println("server is closed")
}
func configureHost(su *iris.Supervisor) {
// here we have full access to the host that will be created
// inside the `app.Run` function or `NewHost`.
//
// we're registering a shutdown "event" callback here:
su.RegisterOnShutdown(onServerShutdown)
// su.RegisterOnError
// su.RegisterOnServe
}