Compare commits

...

7 Commits

3 changed files with 27 additions and 15 deletions

34
main.go
View File

@ -3,36 +3,40 @@ package main
import (
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"github.com/go-chi/chi/v5"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
func executeTemplate(w http.ResponseWriter, filepath string) {
w.Header().Set("Content-Type", "text/html; charset=utf8")
tpl, err := template.ParseFiles("templates/home.gohtml")
tpl, err := template.ParseFiles(filepath)
if err != nil {
panic(err)
log.Printf("Error parsing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = tpl.Execute(w, nil)
if err != nil {
panic(err)
log.Printf("Error executing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
path := filepath.Join("templates", "home.gohtml")
executeTemplate(w, path)
}
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>")
path := filepath.Join("templates", "contact.gohtml")
executeTemplate(w, path)
}
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>
`)
path := filepath.Join("templates", "faq.gohtml")
executeTemplate(w, path)
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {

2
templates/contact.gohtml Normal file
View File

@ -0,0 +1,2 @@
<h1>Contact Page</h1>
<p>To get in touch, email me at <a href="mailto:example@example.com">example@example.com</a></p>

6
templates/faq.gohtml Normal file
View File

@ -0,0 +1,6 @@
<h1>FAQ</h1>
<hr>
<h3>Is this a real website?</h3>
<p>No.</p>
<h3>I Can Has Cheezburger?</h3>
<p>No.</p>