From 300cf4cbbb09956799a4f5b7493e5a9aecbddbb9 Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Wed, 31 Jul 2024 16:00:33 -0400 Subject: [PATCH] Convert manual router func into http.Handler type --- main.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 8321afe..3833cc4 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,9 @@ func contactHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "

Contact Page

To get in touch, email me at example@example.com

") } -func pathHandler(w http.ResponseWriter, r *http.Request) { +type Router struct{} + +func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/": homeHandler(w, r) @@ -29,7 +31,7 @@ func pathHandler(w http.ResponseWriter, r *http.Request) { } func main() { - http.HandleFunc("/", pathHandler) + var router Router fmt.Println("Starting the server on :3000...") - http.ListenAndServe(":3000", nil) + http.ListenAndServe(":3000", router) }