Compare commits
2 Commits
0afafd97fd
...
2d2bea6543
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d2bea6543 | |||
| 34df46e0d5 |
20
main.go
20
main.go
@ -13,13 +13,28 @@ func contactHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
||||
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 faqHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
||||
fmt.Fprint(w, `
|
||||
<h1>FAQ</h1>
|
||||
<hr>
|
||||
<h3>Is this a real website?</h3>
|
||||
<p>No.</p>
|
||||
<h3>I Can Has Cheezburger?</h3>
|
||||
<p>No.</p>
|
||||
`)
|
||||
}
|
||||
|
||||
var pathHandler http.HandlerFunc = func(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)
|
||||
case "/contact":
|
||||
contactHandler(w, r)
|
||||
case "/faq":
|
||||
faqHandler(w, r)
|
||||
default:
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
@ -29,6 +44,7 @@ var pathHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var router Router
|
||||
fmt.Println("Starting the server on :3000...")
|
||||
http.ListenAndServe(":3000", pathHandler)
|
||||
http.ListenAndServe(":3000", router)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user