100-go-mistakes/08-concurrency-foundations/60-contexts/main.go
2021-12-27 15:57:20 +01:00

33 lines
586 B
Go

package main
import (
"context"
"net/http"
)
type key string
const isValidHostKey key = "isValidHost"
func checkValid(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
validHost := r.Host == "acme"
ctx := context.WithValue(r.Context(), isValidHostKey, validHost)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func handler(ctx context.Context, ch chan Message) error {
for {
select {
case msg := <-ch:
// Do something with msg
_ = msg
case <-ctx.Done():
return ctx.Err()
}
}
}
type Message struct{}