Compare commits
7 Commits
3b4543c77a
...
1e61052ec1
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e61052ec1 | |||
| b3dbd23a8e | |||
| 8051f053c3 | |||
| aac5e45b3f | |||
| 4e2ad80fdf | |||
| eb6d144e92 | |||
| 393cc1f3c0 |
34
main.go
34
main.go
@ -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
2
templates/contact.gohtml
Normal 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
6
templates/faq.gohtml
Normal 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>
|
||||
Loading…
x
Reference in New Issue
Block a user