shad-go/lectures/06-http/context/httpserver/httpserver.go
Fedor Korotkiy c6d2706ac9 Fix
2021-03-18 17:41:28 +03:00

36 lines
597 B
Go

package httpserver
import (
"context"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
type ReqTimeContextKey struct{}
func runServer() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
<-stop
cancel()
}()
srv := &http.Server{
Addr: ":8080",
Handler: handler{},
BaseContext: func(_ net.Listener) context.Context {
ctx = context.WithValue(ctx, ReqTimeContextKey{}, time.Now())
return ctx
},
}
return srv.ListenAndServe()
}