-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
36 lines (26 loc) · 891 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"html/template"
"log"
"net/http"
)
func main(){
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
tmpl := template.Must(template.ParseFiles("templates/index.html"))
tmpl.Execute(w, nil)
})
http.HandleFunc("/poem", func(w http.ResponseWriter, r *http.Request) {
log.Println("Finding a poem...")
tmpl := template.Must(template.ParseFiles("templates/fragments/poem.html"))
data, err := GetRandomPoem()
if err != nil {
log.Println("Error getting poem: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, data)
})
log.Println("Listening on http://localhost:8000...")
log.Fatal(http.ListenAndServe(":8000", nil))
}