Convert manual router func into http.Handler type
This commit is contained in:
parent
39fd7ab599
commit
300cf4cbbb
8
main.go
8
main.go
@ -14,7 +14,9 @@ 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) {
|
type Router struct{}
|
||||||
|
|
||||||
|
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)
|
||||||
@ -29,7 +31,7 @@ func pathHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", pathHandler)
|
var router Router
|
||||||
fmt.Println("Starting the server on :3000...")
|
fmt.Println("Starting the server on :3000...")
|
||||||
http.ListenAndServe(":3000", nil)
|
http.ListenAndServe(":3000", router)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user