docs: add graceful shtdown example

https://github.com/gin-gonic/autotls/issues/12
This commit is contained in:
Bo-Yi Wu
2022-08-30 16:26:03 +08:00
parent 9bf58ee3ca
commit bd8f53afb8
2 changed files with 67 additions and 0 deletions

View File

@@ -65,3 +65,39 @@ func main() {
log.Fatal(autotls.RunWithManager(r, &m))
}
```
example usage for graceful shutdown with custom context.
```go
package main
import (
"context"
"log"
"net/http"
"os/signal"
"syscall"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
)
func main() {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(
context.Background(),
syscall.SIGINT,
syscall.SIGTERM,
)
defer stop()
r := gin.Default()
// Ping handler
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
log.Fatal(autotls.RunWithContext(ctx, r, "example1.com", "example2.com"))
}
```

31
_example/example3/main.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"context"
"log"
"net/http"
"os/signal"
"syscall"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
)
func main() {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(
context.Background(),
syscall.SIGINT,
syscall.SIGTERM,
)
defer stop()
r := gin.Default()
// Ping handler
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
log.Fatal(autotls.RunWithContext(ctx, r, "example1.com", "example2.com"))
}