summaryrefslogtreecommitdiff
path: root/ctxlib
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-09-17 01:19:25 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-09-17 01:19:25 +0300
commita054f480adf26e90ddcb66408688486704ab7094 (patch)
tree4600b6e7633979e1a4b94cfcaeb7ddf156733575 /ctxlib
parent5094ebf213243023c6325310b3f710a0974025dd (diff)
simple handlerv0.0.2
Diffstat (limited to 'ctxlib')
-rw-r--r--ctxlib/context.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/ctxlib/context.go b/ctxlib/context.go
new file mode 100644
index 0000000..79851ce
--- /dev/null
+++ b/ctxlib/context.go
@@ -0,0 +1,44 @@
+package ctxlib
+
+import (
+ "context"
+ "net/http"
+)
+
+type ctxKey int
+
+const (
+ Method ctxKey = iota
+ Headers
+ Request
+ RequestID
+ Response
+)
+
+func ResponseFromContext(ctx context.Context) http.ResponseWriter {
+ return ctx.Value(Response).(http.ResponseWriter)
+}
+
+func RequestFromContext(ctx context.Context) *http.Request {
+ c := ctx.Value(Request)
+ if c == nil {
+ return &http.Request{}
+ }
+ return c.(*http.Request)
+}
+
+func HeadersFromContext(ctx context.Context) http.Header {
+ c := ctx.Value(Headers)
+ if c == nil {
+ return http.Header{}
+ }
+ return c.(http.Header)
+}
+
+func MethodFromContext(ctx context.Context) string {
+ c := ctx.Value(Method)
+ if c == nil {
+ return http.MethodGet
+ }
+ return c.(string)
+}