Skip to content

Allow to use upgradeWebsocket hander in an golang app embedding NATS - export handle function of server/websocket.go#1327 #8090

@a-hennig

Description

@a-hennig

Proposed change

move

func(w http.ResponseWriter, r *http.Request) {
		res, err := s.wsUpgrade(w, r)
		if err != nil {
			s.Errorf(err.Error())
			return
		}
		switch res.kind {
		case CLIENT:
			s.createWSClient(res.conn, res.ws)
		case MQTT:
			s.createMQTTClient(res.conn, res.ws)
		case LEAF:
			if !hasLeaf {
				s.Errorf("Not configured to accept leaf node connections")
				// Silently close for now. If we want to send an error back, we would
				// need to create the leafnode client anyway, so that is handling websocket
				// frames, then send the error to the remote.
				res.conn.Close()
				return
			}
			s.createLeafNode(res.conn, nil, nil, res.ws)
		}
	}

into a dedicated, exported function like (similar to e.g. server.HandleVarz)
func (s *Server) HandleWsUpgrade(w http.ResponseWriter, r *http.Request) {

Use case

I want to embedd into my application.
This will also run in environments like windows in customer networks, where it is administratively challenging to open additional ports.
My app serves browser clients, where I would like to connect to nats via WSS (maybe also mqtt).
I would like to include selective nats REST endpoints into my existing "outward" facing http router, so I have to secure only one.

Contribution

sure, can contribute .. but feels a bit trivial .. except if there are any indirect effects, e.g. if anywhere is a redirect/forward/origin URL that would also have to be adjusted, which I would not know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalEnhancement idea or proposal

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions