From 74d8cb8538fd8b5904202c6f00aafbd23335f2a5 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 2 Feb 2021 16:13:39 +0300 Subject: [PATCH] add helper options Signed-off-by: Vasiliy Tolstov --- options.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 options.go diff --git a/options.go b/options.go new file mode 100644 index 0000000..559e81d --- /dev/null +++ b/options.go @@ -0,0 +1,24 @@ +package http + +import "context" + +type rspCodeKey struct{} +type rspCodeVal struct { + code int +} + +// SetRspCode saves response code in context, must be used by handler to specify http code +func SetRspCode(ctx context.Context, code int) { + if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok { + rsp.code = code + } +} + +// GetRspCode used internally by generated http server handler +func GetRspCode(ctx context.Context) int { + var code int + if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok { + code = rsp.code + } + return code +}