package main
import (
"context"
"net/http"
"os"
"os/signal"
"github.com/fxrlv/detach"
)
func main() {
main, cancel := signal.NotifyContext(
context.Background(), os.Interrupt,
)
defer cancel()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// For incoming server requests, the context is canceled when the
// client's connection closes, the request is canceled (with HTTP/2),
// or when the ServeHTTP method returns.
ctx := r.Context()
ctx = detach.WithCancel(ctx, main)
go func() {
// Will be cancelled by signal.
<-ctx.Done()
// Values are associated with http.Request context.
server := ctx.Value(http.ServerContextKey).(*http.Server)
server.Close()
}()
})
http.ListenAndServe(":8080", nil)
}
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
License
fxrlv/detach
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
License
Stars
Watchers
Forks
Packages 0
No packages published