/
main.go
48 lines (40 loc) · 1.77 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
package main
import (
stdContext "context"
"time"
"github.com/go-siris/siris"
"github.com/go-siris/siris/context"
"github.com/go-siris/siris/core/host"
)
// Before continue, please read the below notes:
//
// Current version of Siris is auto-graceful on control+C/command+C
// or kill command sent or whenever app.Shutdown called.
//
// In order to add a custom interrupt handler(ctrl+c/cmd+c) or
// shutdown manually you have to "schedule a host supervisor's task" or
// use the core/host package manually or use a pure http.Server as we already seen at "custom-server" example.
//
// At this example, we will disable the interrupt handler and set our own interrupt handler.
func main() {
app := siris.New()
// output startup banner and error logs on os.Stdout
app.Get("/", func(ctx context.Context) {
ctx.HTML(" <h1>hi, I just exist in order to see if the server is closed</h1>")
})
// tasks are always running in their go-routine by-default.
//
// register custom interrupt handler, fires when ctrl+C/cmd+C pressed or kill command sent.
app.Scheduler.Schedule(host.OnInterrupt(func(proc host.TaskProcess) {
println("Shutdown the server gracefully...")
timeout := 5 * time.Second // give the server 5 seconds to wait for idle connections.
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
proc.Host().Shutdown(ctx) // Shutdown the underline supervisor's server
cancel()
}))
// Start the server and disable the default interrupt handler in order to use our scheduled interrupt task.
app.Run(siris.Addr(":8080"), siris.WithoutInterruptHandler)
}
// Note:
// You can just use an http.Handler with your own signal notify channel and do that as you did with the net/http
// package. I will not show this way, but you can find many examples on the internet.