Исходники и презентации

This commit is contained in:
2025-05-23 07:26:39 +03:00
parent aa948179d5
commit 02d8430a3a
514 changed files with 13773 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package main
import (
"context"
"fmt"
"net/http"
)
func main() {
helloWorldHandler := http.HandlerFunc(handle)
http.Handle("/welcome", injectTraceID(helloWorldHandler))
_ = http.ListenAndServe(":8080", nil)
}
func handle(_ http.ResponseWriter, r *http.Request) {
value, ok := r.Context().Value("trace_id").(string)
if ok {
fmt.Println(value)
}
makeRequest(r.Context())
}
func makeRequest(_ context.Context) {
// requesting to database with context
}
func injectTraceID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "trace_id", "12-21-33")
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
})
}