/
handlers.go
57 lines (49 loc) · 1.38 KB
/
handlers.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
package gorgonzola
import (
"net/http"
"github.com/gorilla/mux"
)
const firstPageJobsLimit = 100
func (g *Gorgonzola) indexHandler(w http.ResponseWriter, r *http.Request) error {
tm := NewTemplate(w)
jobs, err := g.storage.GetJobs(r, firstPageJobsLimit)
if err != nil {
return err
}
tm.set("jobs", jobs)
return tm.render("templates/layout.html", "templates/index.html")
}
func (g *Gorgonzola) jobHandler(w http.ResponseWriter, r *http.Request) error {
tm := NewTemplate(w)
vars := mux.Vars(r)
job, err := g.storage.GetJob(r, vars["key"])
if err != nil {
return err
}
tm.set("job", job)
return tm.render("templates/layout.html", "templates/job.html")
}
func (g *Gorgonzola) addHandler(w http.ResponseWriter, r *http.Request) error {
tm := NewTemplate(w)
if r.Method == "POST" {
url := r.FormValue("url")
if url != "" {
if err := validateURL(url); err != nil {
return err
}
err := g.storage.AddURL(r, url)
if err != nil {
return err
}
http.Redirect(w, r, "/thankyou.html", http.StatusFound)
}
}
return tm.render("templates/layout.html", "templates/add.html")
}
func (g *Gorgonzola) thankyouHandler(w http.ResponseWriter, r *http.Request) error {
tm := NewTemplate(w)
return tm.render("templates/layout.html", "templates/thankyou.html")
}
func (g *Gorgonzola) taskUpdateHandler(w http.ResponseWriter, r *http.Request) error {
return g.storage.Update(r)
}