-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (104 loc) · 2.57 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
type Film struct {
Title string
Director string
}
var (
//go:embed templates
templates embed.FS
//go:embed static
static embed.FS
pages = map[string]string{
"/": "templates/index.html",
"/add-film/": "templates/index.html",
}
)
func main() {
fmt.Println("start serving...")
router := chi.NewRouter()
router.Use(middleware.Logger)
fileServer := http.FileServer(http.FS(static))
router.Handle("/static/*", fileServer)
// GET /
getPage := func(w http.ResponseWriter, r *http.Request) {
page, ok := pages[r.RequestURI]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
tmpl, err := template.ParseFS(templates, page)
if err != nil {
log.Printf("page %s not found in pages caches...", r.RequestURI)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
films := map[string][]Film{
"Films": {
{Title: "The Godfather", Director: "Francis Ford Coppola"},
{Title: "Blade Runner", Director: "Ridley Scott"},
{Title: "The Thing", Director: "John Carpenter"},
},
}
if err := tmpl.Execute(w, films); err != nil {
log.Printf("template %s execute error: %s", r.RequestURI, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
// POST /add-film
addFilm := func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
page, ok := pages[r.RequestURI]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
tmpl, err := template.ParseFS(templates, page)
if err != nil {
log.Printf("page %s not found in pages caches...", r.RequestURI)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
title := r.PostFormValue("title")
director := r.PostFormValue("director")
if err := tmpl.ExecuteTemplate(w, "film-list-element", Film{
Title: title,
Director: director,
}); err != nil {
log.Printf("template %s execute error: %s", r.RequestURI, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
// Router and handlers
router.Get("/", getPage)
router.Post("/add-film/", addFilm)
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
server := &http.Server{
Addr: ":" + port,
Handler: router,
}
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
defer server.Shutdown(nil)
}