Convert http.Handler router into http.HandlerFunc type

This commit is contained in:
Lucas Schumacher 2024-07-31 16:24:34 -04:00
parent 300cf4cbbb
commit 0afafd97fd

View File

@ -14,9 +14,7 @@ func contactHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<h1>Contact Page</h1><p>To get in touch, email me at <a href=\"mailto:example@example.com\">example@example.com</a></p>") fmt.Fprint(w, "<h1>Contact Page</h1><p>To get in touch, email me at <a href=\"mailto:example@example.com\">example@example.com</a></p>")
} }
type Router struct{} var pathHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path { switch r.URL.Path {
case "/": case "/":
homeHandler(w, r) homeHandler(w, r)
@ -31,7 +29,6 @@ func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
var router Router
fmt.Println("Starting the server on :3000...") fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", router) http.ListenAndServe(":3000", pathHandler)
} }