Add manual router function

This commit is contained in:
Lucas Schumacher 2024-07-31 15:44:28 -04:00
parent 428263c34e
commit 39fd7ab599

17
main.go
View File

@ -14,9 +14,22 @@ 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>")
} }
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, "<hr><p>%s</p>", r.URL.Path)
}
func main() { func main() {
http.HandleFunc("/", homeHandler) http.HandleFunc("/", pathHandler)
http.HandleFunc("/contact", contactHandler)
fmt.Println("Starting the server on :3000...") fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", nil) http.ListenAndServe(":3000", nil)
} }