Add remaining (sans spawn, testbench)
This commit is contained in:
30
httpctx/httpctx.go
Normal file
30
httpctx/httpctx.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package httpctx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func helloHandler(w http.ResponseWriter, req *http.Request) {
|
||||
log.Println("hello handler started")
|
||||
ctx := req.Context()
|
||||
defer log.Println("hello handler finishing")
|
||||
|
||||
select {
|
||||
case <-time.After(10 * time.Second):
|
||||
fmt.Fprintln(w, "hello there!")
|
||||
case <-ctx.Done():
|
||||
err := ctx.Err()
|
||||
log.Println("hello error:", err.Error())
|
||||
internalError := http.StatusInternalServerError
|
||||
http.Error(w, err.Error(), internalError)
|
||||
}
|
||||
}
|
||||
|
||||
func HttpCtx() {
|
||||
log.Println("Server listening on :8002")
|
||||
http.HandleFunc("/hello", helloHandler)
|
||||
http.ListenAndServe(":8002", nil)
|
||||
}
|
||||
Reference in New Issue
Block a user