-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (41 loc) · 1.29 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"time"
)
type Film struct {
Title string
Director string
}
func main() {
fmt.Println("Go app...")
// handler function #1 - returns the index.html template, with film data
h1 := func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("index.html"))
films := map[string][]Film{
"Films": {
{Title: "The Godfather", Director: "Francis Ford Coppola"},
{Title: "Blade Runner", Director: "Ridley Scott"},
{Title: "The Thing", Director: "John Carpenter"},
},
}
tmpl.Execute(w, films)
}
// handler function #2 - returns the template block with the newly added film, as an HTMX response
h2 := func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
title := r.PostFormValue("title")
director := r.PostFormValue("director")
// htmlStr := fmt.Sprintf("<li class='list-group-item bg-primary text-white'>%s - %s</li>", title, director)
// tmpl, _ := template.New("t").Parse(htmlStr)
tmpl := template.Must(template.ParseFiles("index.html"))
tmpl.ExecuteTemplate(w, "film-list-element", Film{Title: title, Director: director})
}
// define handlers
http.HandleFunc("/", h1)
http.HandleFunc("/add-film/", h2)
log.Fatal(http.ListenAndServe(":8000", nil))
}