-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from thejoeejoee/feat-sub-http-response-status
[watermill-http] Custom HTTP response status
- Loading branch information
Showing
3 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"github.com/ThreeDotsLabs/watermill/message" | ||
) | ||
|
||
// ctxResponseStatusCodeKey is a context key for the http status code in the message context | ||
type ctxResponseStatusCodeKey struct{} | ||
|
||
// StatusCodeFromContext returns the status code from the context. | ||
func StatusCodeFromContext(ctx context.Context, otherwise int) int { | ||
if v := ctx.Value(ctxResponseStatusCodeKey{}); v != nil { | ||
if code, ok := v.(int); ok { | ||
return code | ||
} | ||
} | ||
return otherwise | ||
} | ||
|
||
// WithResponseStatusCode returns a new context with the status code. | ||
func WithResponseStatusCode(ctx context.Context, code int) context.Context { | ||
return context.WithValue(ctx, ctxResponseStatusCodeKey{}, code) | ||
} | ||
|
||
// SetResponseStatusCode sets a http status code to the given message. | ||
func SetResponseStatusCode(m *message.Message, code int) *message.Message { | ||
m.SetContext(WithResponseStatusCode(m.Context(), code)) | ||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters