/
movie.go
67 lines (58 loc) · 1.53 KB
/
movie.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
package search
import (
"io/ioutil"
"net/http"
"strings"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/search"
"google.golang.org/appengine/urlfetch"
)
type movie struct {
Name string
URL string
Summary search.HTML
}
func handleAdd(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
var err error
if req.Method == "POST" {
name := strings.TrimSpace(req.FormValue("name"))
summary, err := getHTML(ctx, req.FormValue("summary"))
if err != nil {
http.Error(res, "Server error", http.StatusInternalServerError)
log.Errorf(ctx, "%v\n", err)
return
}
mov := &movie{
Name: name,
Summary: summary,
URL: strings.ToLower(strings.Replace(name, " ", "", -1)),
}
err = addMovie(ctx, mov)
if err != nil {
http.Error(res, "Server error", http.StatusInternalServerError)
log.Errorf(ctx, "%v\n", err)
return
}
http.Redirect(res, req, "/", http.StatusSeeOther)
return
}
err = tpl.ExecuteTemplate(res, "addMovie", nil)
if err != nil {
http.Error(res, "Server error", http.StatusInternalServerError)
log.Errorf(ctx, "%v\n", err)
return
}
}
func getHTML(ctx context.Context, markdown string) (search.HTML, error) {
client := urlfetch.Client(ctx)
resp, err := client.Post("https://api.github.com/markdown/raw", "text/plain", strings.NewReader(markdown))
if err != nil {
return "", err
}
defer resp.Body.Close()
bts, err := ioutil.ReadAll(resp.Body)
return search.HTML(bts), err
}