nats-server
nats-server copied to clipboard
[ADDED] Custom Muxer for websocket httpserver
Closes #3994
- [X] Link to issue, e.g.
Resolves #3994
- https://github.com/nats-io/nats-server/issues/3994
- [x] Documentation added (if applicable)
- [x] Tests added
- [X] Branch rebased on top of current main (
git pull --rebase origin main
) - [ ] Changes squashed to a single commit (described here)
- [x] Build is green in Travis CI
- [X] You have certified that the contribution is your original work and that you license the work to the project under the Apache 2 license
Changes proposed in this pull request
This PR adds the ability to provide the HTTP Server powering the Websocket with a custom muxer. The reason for this change is that it allows people to add a NATs powered websocket to an existing webapp without having multiple ports. Sample code below
Example usage
package main
import (
"fmt"
"net/http"
"github.com/nats-io/nats-server/v2/server"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, _ = w.Write([]byte("hello"))
})
wsOpts := server.WebsocketOpts{
Port: 8000,
Muxer: mux,
Endpoint: "/ws",
NoTLS: true,
}
s, err := server.NewServer(
&server.Options{
Port: 0,
Websocket: wsOpts,
Debug: true,
},
)
if err != nil {
server.PrintAndDie(fmt.Sprintf("%s: %s", "nats-server", err))
}
s.ConfigureLogger()
if err := server.Run(s); err != nil {
server.PrintAndDie(err.Error())
}
s.WaitForShutdown()
}
Custom Endpoint usage
nats-server on feat/websocket-mux via 🐹 v1.20.2
❯ http localhost:8000
HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain; charset=utf-8
Date: Tue, 02 May 2023 15:40:08 GMT
hello
Websocket usage
nats-server on feat/websocket-mux via 🐹 v1.20.2
❯ websocat ws://localhost:8000/ws
INFO {"server_id":"ND476XYY3IBTTTUYKTX7QIY55ZRBBOTYDQYAXNVGDVX4GGZPW6CBHJXN","server_name":"ND476XYY3IBTTTUYKTX7QIY55ZRBBOTYDQYAXNVGDVX4GGZPW6CBHJXN","version":"2.9.17-beta.3","proto":1,"go":"go1.20.2","host":"0.0.0.0","port":4222,"headers":true,"max_payload":1048576,"client_id":4,"client_ip":"::1"}
^C
@jordan-rash thanks for the contribution! Could you retarget this into the dev
branch? 🙏
~I'm a little confused as the tests that are failing in Travis are passing locally...~
=== RUN TestJetStreamLeafNodeClusterExtensionWithSystemAccount
=== RUN TestJetStreamLeafNodeClusterExtensionWithSystemAccount/true-true
=== RUN TestJetStreamLeafNodeClusterExtensionWithSystemAccount/true-false
=== RUN TestJetStreamLeafNodeClusterExtensionWithSystemAccount/false-true
=== RUN TestJetStreamLeafNodeClusterExtensionWithSystemAccount/false-false
--- PASS: TestJetStreamLeafNodeClusterExtensionWithSystemAccount (20.96s)
--- PASS: TestJetStreamLeafNodeClusterExtensionWithSystemAccount/true-true (2.25s)
--- PASS: TestJetStreamLeafNodeClusterExtensionWithSystemAccount/true-false (7.82s)
--- PASS: TestJetStreamLeafNodeClusterExtensionWithSystemAccount/false-true (3.08s)
--- PASS: TestJetStreamLeafNodeClusterExtensionWithSystemAccount/false-false (7.80s)
Edit: looks like it was just a little hiccup in Travis!
@wallyqs I will wait until this PR gets review and accepted/rejected before I squash all the commits. Thanks a ton team!
Went ahead and rebased onto lastest dev
@wallyqs is there any desire to get this rebased or should i just close??