How to move an http endpoint to a different port

I have an experimental http service with two endpoints ("/" and"/heartbeat") on one port and graceful shutdown working well enough. I can’t figure out how to move the “/heartbeat” end point to another port.

Thanks in advance for any suggestions!


The important lines not shown in your snippet are these:

m := http.NewServeMux()

srv := http.Server{Addr: ":8080", Handler: m}
m.HandleFunc("/", TestEndpoint)

You set the port the http.Server binds to to 8080. If you want to bind to a second port, you will have to start a second server.

Note that m.HandleFunc("/", TestEndpoint) matches every URL that is not otherwise matched. If you want to match exactly /, you have to check this. See for how this can be done.

Thank you so much for the review! I just realized I linked the wrong version of the file. I updated it. Terribly sorry about that.

OK, no problem. The basic solution stays the same, you will have to start a second server which listens to a different port.

Thes are the relevant lines in you code using Gin:

	srv := &http.Server{
		Addr:    ":8080",
		Handler: router,

If you want to listen to another port besides 8080, you will have to create a second server.

See for how this can be done.

Thank you ! I’m giving that a shot now:)

Thanks for your help! I had tried your suggestion before, but it turns out I also had to put the new server into a different

go func ()

if you’re curious, the link now has the working change