Skip to content

Commit

Permalink
Add the ProxyPath
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-gold-apolicy committed May 3, 2022
1 parent 9c077d0 commit 3d5be9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ type Options struct {
// supports compression. If the server does too, then data will be compressed.
Compression bool

// For websocket connections, adds a path to connections url.
// This is useful when connecting to NATS behind a proxy.
ProxyPath string

// InboxPrefix allows the default _INBOX prefix to be customized
InboxPrefix string
}
Expand Down Expand Up @@ -1147,6 +1151,15 @@ func Compression(enabled bool) Option {
}
}

// ProxyPath is an option for websocket connections that adds a path to connections url.
// This is useful when connecting to NATS behind a proxy.
func ProxyPath(path string) Option {
return func(o *Options) error {
o.ProxyPath = path
return nil
}
}

// CustomInboxPrefix configures the request + reply inbox prefix
func CustomInboxPrefix(p string) Option {
return func(o *Options) error {
Expand Down
9 changes: 9 additions & 0 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ func (nc *Conn) wsInitHandshake(u *url.URL) error {
scheme = "https"
}
ustr := fmt.Sprintf("%s://%s", scheme, u.Host)

if nc.Opts.ProxyPath != "" {
proxyPath := nc.Opts.ProxyPath
if !strings.HasPrefix(proxyPath, "/") {
proxyPath = "/" + proxyPath
}
ustr += proxyPath
}

u, err = url.Parse(ustr)
if err != nil {
return err
Expand Down

0 comments on commit 3d5be9a

Please sign in to comment.