-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (43 loc) · 1.42 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//new: log.Fatal, html templates
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strconv"
)
type Film struct {
Title string
Director string
ReleaseYear uint16
}
type FilmList struct {
ListTitle string
Films []Film
}
func getFilms(writer http.ResponseWriter, request *http.Request) {
pageTemplate := template.Must(template.ParseFiles("index.html"))
bestFilms := FilmList{
ListTitle: "Best Films Ever",
Films: []Film{
{Title: "Mean Girls", Director: "Mark Waters", ReleaseYear: 2004},
{Title: "Rope", Director: "Alfred Hitchcock", ReleaseYear: 1948},
{Title: "Night of the Living Dead", Director: "George A. Romero", ReleaseYear: 1968},
},
}
pageTemplate.Execute(writer, bestFilms)
}
func addFilm(writer http.ResponseWriter, request *http.Request) {
newFilmTitle := request.PostFormValue("title")
newFilmDirector := request.PostFormValue("director")
newFilmReleaseYear, _ := strconv.Atoi(request.PostFormValue("releaseyear"))
htmlString := fmt.Sprintf("<li class='list-group-item bg-primary text-white'>%s - %s, Released %d</li>", newFilmTitle, newFilmDirector, newFilmReleaseYear)
template, _ := template.New("t").Parse(htmlString) //wtf is 't'?
template.Execute(writer, nil)
}
func main() {
http.HandleFunc("/", getFilms)
http.HandleFunc("/add-film/", addFilm)
log.Fatal(http.ListenAndServe(":8000", nil)) //log.Fatal will write text if there is an issue with the function inside it
}