0

I have the following code in a small golang server:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})

Basically allowing you to hit the root of the server without anything scary happening.

The issue is that you can also hit /foo/bar/blah... and it still works. Which I don't want.

How can I explicitly restrict it to what I say?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

1 Answers1

4

Add the following code to the beginning of your handler:

if r.URL.Path != "/" {
     http.NotFound(w, r)
     return
}
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242