Add remaining (sans spawn, testbench)

This commit is contained in:
2024-05-05 00:02:38 +05:30
parent 750c23e26e
commit 2dbd7ac062
6 changed files with 94 additions and 2 deletions

30
httpctx/httpctx.go Normal file
View 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)
}