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 ( import (
"fmt" "fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"path/filepath"
"github.com/go-chi/chi/v5" "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") w.Header().Set("Content-Type", "text/html; charset=utf8")
tpl, err := template.ParseFiles("templates/home.gohtml") tpl, err := template.ParseFiles(filepath)
if err != nil { 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) err = tpl.Execute(w, nil)
if err != 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) { func contactHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8") path := filepath.Join("templates", "contact.gohtml")
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>") executeTemplate(w, path)
} }
func faqHandler(w http.ResponseWriter, r *http.Request) { func faqHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8") path := filepath.Join("templates", "faq.gohtml")
fmt.Fprint(w, ` executeTemplate(w, path)
<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) { 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>