From 39fd7ab5992d28c9f595cbf9224e3214066a8f8b Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Wed, 31 Jul 2024 15:44:28 -0400 Subject: [PATCH] Add manual router function --- main.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 53488c9..8321afe 100644 --- a/main.go +++ b/main.go @@ -14,9 +14,22 @@ 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) { + switch r.URL.Path { + case "/": + homeHandler(w, r) + case "/contact": + contactHandler(w, r) + default: + w.Header().Set("Content-Type", "text/html; charset=utf8") + w.WriteHeader(http.StatusNotFound) + fmt.Fprint(w, "404 page not found") + } + fmt.Fprintf(w, "

%s

", r.URL.Path) +} + func main() { - http.HandleFunc("/", homeHandler) - http.HandleFunc("/contact", contactHandler) + http.HandleFunc("/", pathHandler) fmt.Println("Starting the server on :3000...") http.ListenAndServe(":3000", nil) }