-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
99 lines (88 loc) · 3.02 KB
/
app.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
package ResumeFodder
import (
"appengine"
"fmt"
"gitlab.com/steve-perkins/ResumeFodder/command"
"gitlab.com/steve-perkins/ResumeFodder/data"
"html/template"
"io/ioutil"
"net/http"
"path"
"path/filepath"
)
func init() {
http.Handle("/", http.FileServer(http.Dir("static")))
http.HandleFunc("/init", initHandler)
http.HandleFunc("/generate", generateHandler)
}
func initHandler(w http.ResponseWriter, r *http.Request) {
json, err := command.InitResumeJson()
if err != nil {
errorHandler(w, r, err.Error())
return
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment;filename=\"resume.json\"")
w.Write([]byte(json))
}
func generateHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
// Update JSON-Resume data file
const MAX_UPLOAD_BYTES = 100000
file, _, err := r.FormFile("file")
if err != nil {
errorHandler(w, r, fmt.Sprintf("An error occurred uploading the file: %s\n", err))
return
}
ctx.Infof("file == %s\n", file)
uploadBuffer := make([]byte, MAX_UPLOAD_BYTES)
bytesRead, _ := file.Read(uploadBuffer)
if bytesRead >= MAX_UPLOAD_BYTES {
errorHandler(w, r, fmt.Sprintf("JSON Resume upload exceeds the %d byte cap\n", MAX_UPLOAD_BYTES))
return
}
ctx.Infof("Uploaded %d bytes of JSON-Resume data\n", bytesRead)
contents := string(uploadBuffer[:bytesRead])
// Parse the (hopefully) JSON
resumeData, err := data.FromJsonString(contents)
if err != nil {
errorHandler(w, r, fmt.Sprintf("An error occurred parsing the uploaded file: %s\n", err))
return
}
// Load the selected template
templateParam := r.Form.Get("template")
if templateParam != "standard" && templateParam != "professional" && templateParam != "iconic" && templateParam != "refined" {
errorHandler(w, r, fmt.Sprintf("An unrecognized template was selected: %s\n", templateParam))
return
}
ctx.Infof("Exporting a resume with template: %s\n", templateParam)
templateBytes, err := ioutil.ReadFile(filepath.Join("templates", templateParam+".xml"))
if err != nil {
errorHandler(w, r, fmt.Sprintf("An error occurred loading the template file: %s\n", err))
return
}
templateString := string(templateBytes)
// Generate the resume
exportBuffer, err := command.ExportResume(resumeData, templateString)
if err != nil {
errorHandler(w, r, fmt.Sprintf("An error occurred exporting the resume: %s\n", err))
return
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment;filename=\"resume.doc\"")
w.Write(exportBuffer.Bytes())
}
func errorHandler(w http.ResponseWriter, r *http.Request, message string) {
ctx := appengine.NewContext(r)
ctx.Errorf("Handling error: %s\n", message)
w.Header().Set("Content-Type", "text/html")
t, err := template.ParseFiles(path.Join("static", "error.html"))
if err != nil {
ctx.Errorf("Error loading 'error.html': %s\n", err)
w.Write([]byte("Unable to render the error page"))
return
}
data := make(map[string]string)
data["message"] = message
t.Execute(w, data)
}