lenslocked/main.go

45 lines
1.2 KiB
Go

package main
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
)
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>")
}
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>
`)
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "404 page not found")
}
func main() {
r := chi.NewRouter()
r.Get("/", homeHandler)
r.Get("/contact", contactHandler)
r.Get("/faq", faqHandler)
r.NotFound(notFoundHandler)
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r)
}