HTTP servers Flashcards
Processing HTTP requests with Go is primarily about which two things?
Handlers & Servemuxes
What are handlers fairly analogous to in MVC?
Controllers
Generally speaking, what are handlers responsible?
Carrying out your application logic and writing response headers and bodies.
What is a servemux also known as?
Router
What does a servemux store?
A mapping between the predefined URL paths for your application and the corresponding handlers.
How many servemuxes do you usually have?
One, containing all your routes
Which Go package ships with the simple but effective http.ServeMux servemux?
net/http
Which servemux is available from the net/http package?
http.ServeMux
The net/http package has functions to generate common handlers. Give 3 examples.
http.FileServer(), http.NotFoundHandler() and http.RedirectHandler().
Which function creates an empty servemux?
http.NewServeMux
What parameters does http.NewServeMux take?
None e.g. mux := http.NewServeMux()
How would you create a handler which 307 redirects all requests it receives to http://example.org?
rh := http.RedirectHandler(“http://example.org”, 307)
How would you register a handler, rh, for all incoming requests with the URL path /foo?
mux.Handle(“/foo”, rh)
What does the http.ListenAndServefunction do?
Creates a new server and starts listening for incoming requests
What parameters does the http.ListenAndServe function take?
A port specification and a reference to our servemux e.g. http.ListenAndServe(“:3000”, mux)