2017-06-13 08:06:10 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
stdContext "context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-06-13 08:06:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/", func(ctx iris.Context) {
|
2017-07-10 17:32:42 +02:00
|
|
|
ctx.HTML("<h1>hi, I just exist in order to see if the server is closed</h1>")
|
2017-06-13 08:06:10 +02:00
|
|
|
})
|
|
|
|
|
2020-08-22 02:21:44 +02:00
|
|
|
idleConnsClosed := make(chan struct{})
|
2017-06-13 08:06:10 +02:00
|
|
|
go func() {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch,
|
|
|
|
// kill -SIGINT XXXX or Ctrl+c
|
|
|
|
os.Interrupt,
|
|
|
|
syscall.SIGINT, // register that too, it should be ok
|
|
|
|
// os.Kill is equivalent with the syscall.Kill
|
|
|
|
os.Kill,
|
|
|
|
syscall.SIGKILL, // register that too, it should be ok
|
|
|
|
// kill -SIGTERM XXXX
|
|
|
|
syscall.SIGTERM,
|
|
|
|
)
|
|
|
|
select {
|
|
|
|
case <-ch:
|
2017-07-10 17:32:42 +02:00
|
|
|
println("shutdown...")
|
2017-06-13 08:06:10 +02:00
|
|
|
|
2020-04-28 04:22:58 +02:00
|
|
|
timeout := 10 * time.Second
|
2017-06-13 08:06:10 +02:00
|
|
|
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
app.Shutdown(ctx)
|
2020-08-22 02:21:44 +02:00
|
|
|
close(idleConnsClosed)
|
2017-06-13 08:06:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// Start the server and disable the default interrupt handler in order to
|
|
|
|
// handle it clear and simple by our own, without any issues.
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080", iris.WithoutInterruptHandler)
|
2020-08-22 02:21:44 +02:00
|
|
|
<-idleConnsClosed
|
2017-06-13 08:06:10 +02:00
|
|
|
}
|