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] Custom Muxer for websocket httpserver #4122

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

jordan-rash
Copy link

@jordan-rash jordan-rash commented May 2, 2023

Closes #3994

  • Link to issue, e.g. Resolves #3994
  • Provide HTTP Mux Option to Websocket #3994
  • Documentation added (if applicable)
  • Tests added
  • Branch rebased on top of current main (git pull --rebase origin main)
  • Changes squashed to a single commit (described here)
  • Build is green in Travis CI
  • 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 jordan-rash requested a review from a team as a code owner May 2, 2023 15:42
@wallyqs
Copy link
Member

wallyqs commented May 2, 2023

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

@jordan-rash jordan-rash changed the base branch from main to dev May 2, 2023 15:58
@jordan-rash
Copy link
Author

jordan-rash commented May 2, 2023

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
Copy link
Author

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

@jordan-rash
Copy link
Author

Went ahead and rebased onto lastest dev

@neilalexander neilalexander added the post-freeze We'll come back to this after the freeze period label Aug 21, 2023
@jordan-rash jordan-rash changed the base branch from dev to main November 30, 2023 19:12
@jordan-rash
Copy link
Author

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

Signed-off-by: jordan-rash <jordan-rash@users.noreply.github.com>
Signed-off-by: jordan-rash <jordan-rash@users.noreply.github.com>
Signed-off-by: jordan-rash <jordan-rash@users.noreply.github.com>
Signed-off-by: jordan-rash <jordan-rash@users.noreply.github.com>
Signed-off-by: jordan-rash <jordan-rash@users.noreply.github.com>
Signed-off-by: jordan-rash <jordan-rash@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
post-freeze We'll come back to this after the freeze period
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide HTTP Mux Option to Websocket
3 participants