-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (110 loc) · 3.01 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
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
goresume "github.com/nii236/gopher-resume/models"
)
func errHandler(f func(http.ResponseWriter, *http.Request) error) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
w.WriteHeader(500)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
}
}
}
func findOne(doc *goquery.Selection, selector string) *goquery.Selection {
var result *goquery.Selection
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
result = s
})
return result
}
func fetchThemes() ([]string, error) {
doc, err := goquery.NewDocument("https://jsonresume.org/themes")
if err != nil {
return nil, err
}
themes := []string{}
doc.Find("#themes .theme").Each(func(i int, s *goquery.Selection) {
theme := s.Find(".col-sm-12.col-xs-6 a").First()
link, ok := theme.Attr("href")
if !ok {
return
}
themes = append(themes, strings.TrimPrefix(link, "http://themes.jsonresume.org/theme/"))
})
return themes, err
}
func main() {
port := flag.String("port", "8080", "port to listen on")
flag.Parse()
r := mux.NewRouter()
r.HandleFunc("/api/resume", errHandler(func(w http.ResponseWriter, r *http.Request) error {
url := r.URL.Query().Get("url")
w.Header().Add("Content-type", "application/json")
if url == "" {
w.WriteHeader(400)
json.NewEncoder(w).Encode(map[string]string{"error": "missing URL"})
}
resume, err := resumeForURL(url)
if err != nil {
w.WriteHeader(500)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
}
return json.NewEncoder(w).Encode(resume)
})).Methods("GET")
r.HandleFunc("/resume", errHandler(func(w http.ResponseWriter, r *http.Request) error {
var body struct {
URL string
Theme string
}
body.URL = r.FormValue("url")
body.Theme = r.FormValue("theme")
resume, err := resumeForURL(body.URL)
if err != nil {
w.WriteHeader(500)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
}
var postBody bytes.Buffer
if err := json.NewEncoder(&postBody).Encode(map[string]goresume.Resume{"resume": resume}); err != nil {
return err
}
log.Printf("posting resume %#v", resume)
resp, err := http.Post(
fmt.Sprintf("http://themes.jsonresume.org/theme/%s", body.Theme),
"application/json",
&postBody,
)
if err != nil {
return err
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
return nil
})).Methods("POST")
r.HandleFunc("/", errHandler(func(w http.ResponseWriter, r *http.Request) error {
themes, err := fetchThemes()
if err != nil {
return err
}
temp, err := template.ParseFiles("./index.html.tmpl")
if err != nil {
return err
}
return temp.Execute(w, struct {
Themes []string
}{Themes: themes})
}))
http.Handle("/", r)
log.Printf("Listening on port %s", *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}