Home > Blockchain >  How to serve Go on a particular path?
How to serve Go on a particular path?

Time:09-22

How do I make the server listen on http://localhost:8000/api as the default URL instead of http:localhost:8000?

I've been using this

http.ListenAndServe(":8000")

What changes should I bring?

CodePudding user response:

You can't listen on a URL, you listen on a TCP port.

So 8080 in your case.

It's up to the server that listens on the port to react on specific URIs.

So to make APIs available on /api/..., their path should begin with /api/.

    http.Handle("/api/someAPI", apiHandler)
    http.Handle("/api/someOtherAPI", otherHandler)
    . . .

Alternatively when using Kubernetes ingress or some other reverse proxy setup, it's possible to configure URI rewriting (example). This is useful when dealing with an existing application that is hardcoded to a specific URI but needs to be exposed on a different URI.

  •  Tags:  
  • go
  • Related