Compare commits
5 Commits
d556efa15b
...
0afafd97fd
| Author | SHA1 | Date | |
|---|---|---|---|
| 0afafd97fd | |||
| 300cf4cbbb | |||
| 39fd7ab599 | |||
| 428263c34e | |||
| 7816f43ca7 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
tmp
|
||||
|
||||
24
main.go
24
main.go
@ -5,12 +5,30 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func handlerFunc(w http.ResponseWriter, r *http.Request) {
|
||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
||||
fmt.Fprint(w, "<h1>Welcome to my awesome site!</h1>")
|
||||
}
|
||||
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>")
|
||||
}
|
||||
|
||||
var pathHandler http.HandlerFunc = func(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() {
|
||||
http.HandleFunc("/", handlerFunc)
|
||||
fmt.Println("Starting the server on :3000...")
|
||||
http.ListenAndServe(":3000", nil)
|
||||
http.ListenAndServe(":3000", pathHandler)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user