Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added headers subdirective for nats_request and nats_subscribe #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ This way, the NATS request header `X-NatsBridge-UrlQuery` can be used to set URL
```nginx
nats_request [matcher] [serverAlias] subject {
[timeout 42ms]
[headers true|false]
}
```

Expand All @@ -383,8 +384,8 @@ sends the NATS reply back as HTTP response. It is a terminal handler,
meaning it does not make sense to place Caddy handlers after this one because
they will never be called.

HTTP request headers are converted to NATS headers. NATS reply headers are converted
to HTTP response headers.
HTTP request headers are converted to NATS headers if the headers subdirective is set to true.
NATS reply headers are converted to HTTP response headers.

For `matcher`, all registered [Caddy request matchers](https://caddyserver.com/docs/json/apps/http/servers/routes/match/)
can be used - and the `nats_request` handler is only triggered if the request matches the matcher.
Expand Down Expand Up @@ -437,20 +438,20 @@ Additionally, the following placeholders are available:

(same as for `nats_publish`)

All HTTP headers will become NATS Message headers. On top if this, the following headers are automatically set:
If the subdirective 'headers' is set to true (default), then all HTTP headers will become NATS Message headers. On top if this, the following headers are automatically set:

- `X-NatsBridge-Method` header: contains the HTTP header `GET,POST,HEAD,...`
- `X-NatsBridge-UrlPath` header: URI path without query string
- `X-NatsBridge-UrlQuery` header: encoded query values, without `?`

> We might want to support setting arbitrary headers later :) (from Caddy expressions). Create an issue if you need this :)


---
## HTTP -> NATS via `nats_publish` (fire-and-forget)

```nginx
nats_publish [matcher] [serverAlias] subject
nats_publish [matcher] [serverAlias] subject {
[headers true|false]
}
```

`nats_publish` publishes the HTTP request to the specified NATS subject. This
Expand Down Expand Up @@ -511,14 +512,12 @@ Additionally, the following placeholders are available:

(same as for `nats_request`)

All HTTP headers will become NATS Message headers. On top if this, the following headers are automatically set:
If the subdirective 'headers' is set to true (default), then all HTTP headers will become NATS Message headers. On top if this, the following headers are automatically set:

- `X-NatsBridge-Method` header: contains the HTTP header `GET,POST,HEAD,...`
- `X-NatsBridge-UrlPath` header: URI path without query string
- `X-NatsBridge-UrlQuery` header: encoded query values, without `?`

> We might want to support setting arbitrary headers later :) (from Caddy expressions). Create an issue if you need this :)


---
## large HTTP payloads with store_body_to_jetstream
Expand Down
39 changes: 25 additions & 14 deletions common/http_msg_to_nats_msg.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
package common

import (
"github.com/nats-io/nats.go"
"io"
"net/http"

"github.com/nats-io/nats.go"
)

// NatsMsgForHttpRequest creates a nats.Msg from an existing http.Request: the HTTP Request Body is transferred
// to the NATS message Data, and the headers are transferred as well.
// to the NATS message Data, and the headers are transferred as well if 'headers' is true.
//
// Three special headers are added for the request method, URL path, and raw query.
func NatsMsgForHttpRequest(r *http.Request, subject string) (*nats.Msg, error) {
func NatsMsgForHttpRequest(r *http.Request, subject string, headers bool) (*nats.Msg, error) {
var msg *nats.Msg
b, _ := io.ReadAll(r.Body)

headers := nats.Header(r.Header)
for k, v := range ExtraNatsMsgHeadersFromContext(r.Context()) {
headers.Add(k, v)
}
msg = &nats.Msg{
Subject: subject,
Header: headers,
Data: b,
if headers {
headers := nats.Header(r.Header)
for k, v := range ExtraNatsMsgHeadersFromContext(r.Context()) {
headers.Add(k, v)
}

msg = &nats.Msg{
Subject: subject,
Header: headers,
Data: b,
}

msg.Header.Add("X-NatsBridge-Method", r.Method)
msg.Header.Add("X-NatsBridge-UrlPath", r.URL.Path)
msg.Header.Add("X-NatsBridge-UrlQuery", r.URL.RawQuery)

} else {
msg = &nats.Msg{
Subject: subject,
Data: b,
}
}

msg.Header.Add("X-NatsBridge-Method", r.Method)
msg.Header.Add("X-NatsBridge-UrlPath", r.URL.Path)
msg.Header.Add("X-NatsBridge-UrlQuery", r.URL.RawQuery)
return msg, nil
}
13 changes: 13 additions & 0 deletions publish/caddyfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package publish

import (
"strconv"

"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
Expand Down Expand Up @@ -31,6 +33,17 @@ func (p *Publish) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {

for d.NextBlock(0) {
switch d.Val() {
case "headers":
if !d.NextArg() {
return d.ArgErr()
}
h, err := strconv.ParseBool(d.Val())
if err != nil {
return d.Err("headers is not a boolean")
}

p.Headers = h

default:
return d.Errf("unrecognized subdirective: %s", d.Val())
}
Expand Down
7 changes: 5 additions & 2 deletions publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package publish

import (
"fmt"
"net/http"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/sandstorm/caddy-nats-bridge/common"
"github.com/sandstorm/caddy-nats-bridge/natsbridge"
"go.uber.org/zap"
"net/http"
)

type Publish struct {
Subject string `json:"subject,omitempty"`
ServerAlias string `json:"serverAlias,omitempty"`
Headers bool `json:"headers,omitempty"`

logger *zap.Logger
app *natsbridge.NatsBridgeApp
Expand All @@ -26,6 +28,7 @@ func (Publish) CaddyModule() caddy.ModuleInfo {
// Default values
return &Publish{
ServerAlias: "default",
Headers: true,
}
},
}
Expand Down Expand Up @@ -58,7 +61,7 @@ func (p Publish) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
return fmt.Errorf("NATS server alias %s not found", p.ServerAlias)
}

msg, err := common.NatsMsgForHttpRequest(r, subj)
msg, err := common.NatsMsgForHttpRequest(r, subj, p.Headers)
if err != nil {
return err
}
Expand Down
17 changes: 16 additions & 1 deletion request/caddyfile.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package request

import (
"strconv"
"time"

"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"time"
)

// ParseRequestHandler parses the nats_request directive. Syntax:
//
// nats_request [serverAlias] subject {
// [timeout 1s]
// [headers true|false]
// }
func ParseRequestHandler(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var p = Request{}
Expand Down Expand Up @@ -42,6 +45,18 @@ func (p *Request) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}

p.Timeout = t

case "headers":
if !d.NextArg() {
return d.ArgErr()
}
h, err := strconv.ParseBool(d.Val())
if err != nil {
return d.Err("headers is not a boolean")
}

p.Headers = h

default:
return d.Errf("unrecognized subdirective: %s", d.Val())
}
Expand Down
9 changes: 6 additions & 3 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package request

import (
"fmt"
"net/http"
"time"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/nats-io/nats.go"
"github.com/sandstorm/caddy-nats-bridge/common"
"github.com/sandstorm/caddy-nats-bridge/natsbridge"
"go.uber.org/zap"
"net/http"
"time"
)

type Request struct {
Subject string `json:"subject,omitempty"`
Timeout time.Duration `json:"timeout,omitempty"`
Headers bool `json:"Headers,omitempty"`
ServerAlias string `json:"serverAlias,omitempty"`

logger *zap.Logger
Expand All @@ -29,6 +31,7 @@ func (Request) CaddyModule() caddy.ModuleInfo {
// Default values
return &Request{
Timeout: 1 * time.Second,
Headers: true,
ServerAlias: "default",
}
},
Expand Down Expand Up @@ -63,7 +66,7 @@ func (p Request) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
return fmt.Errorf("NATS server alias %s not found", p.ServerAlias)
}

msg, err := common.NatsMsgForHttpRequest(r, subj)
msg, err := common.NatsMsgForHttpRequest(r, subj, p.Headers)
if err != nil {
return err
}
Expand Down