nats-server icon indicating copy to clipboard operation
nats-server copied to clipboard

[ADDED] Custom Muxer for websocket httpserver

Open jordan-rash opened this issue 1 year ago • 5 comments

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 avatar May 02 '23 15:05 jordan-rash

@jordan-rash thanks for the contribution! Could you retarget this into the dev branch? 🙏

wallyqs avatar May 02 '23 15:05 wallyqs

~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!

jordan-rash avatar May 02 '23 19:05 jordan-rash

@wallyqs I will wait until this PR gets review and accepted/rejected before I squash all the commits. Thanks a ton team!

jordan-rash avatar May 08 '23 20:05 jordan-rash

Went ahead and rebased onto lastest dev

jordan-rash avatar Jun 05 '23 17:06 jordan-rash

@wallyqs is there any desire to get this rebased or should i just close??

jordan-rash avatar Apr 22 '24 12:04 jordan-rash