-
Notifications
You must be signed in to change notification settings - Fork 10
/
routes.go
152 lines (130 loc) · 3.53 KB
/
routes.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package web
import (
"encoding/gob"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/hack4impact/transcribe4all/config"
"github.com/hack4impact/transcribe4all/tasks"
"github.com/hack4impact/transcribe4all/transcription"
)
type route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type transcriptionJobData struct {
AudioURL string `json:"audioURL"`
EmailAddresses []string `json:"emailAddresses"`
SearchWords []string `json:"searchWords"`
}
type flash struct {
Title string
Body string
}
var routes = []route{
route{
"add_job",
"POST",
"/add_job",
initiateTranscriptionJobHandler,
},
route{
"add_job_json",
"POST",
"/add_job_json",
initiateTranscriptionJobHandlerJSON,
},
route{
"health",
"GET",
"/health",
healthHandler,
},
route{
"job_status",
"GET",
"/job_status/{id}",
jobStatusHandler,
},
route{
"form",
"GET",
"/",
formHandler,
},
}
var (
store = sessions.NewCookieStore([]byte(config.Config.SecretKey))
flashSession = "flash"
)
func init() {
// register the flash struct with gob so that it can be stored in sessions
gob.Register(&flash{})
}
// initiateTranscriptionJobHandlerJSON takes a POST request containing a json object,
// decodes it into a transcriptionJobData struct, and starts a transcription task.
func initiateTranscriptionJobHandlerJSON(w http.ResponseWriter, r *http.Request) {
jsonData := new(transcriptionJobData)
// unmarshal from the response body directly into our struct
if err := json.NewDecoder(r.Body).Decode(jsonData); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
executer := tasks.DefaultTaskExecuter
executer.QueueTask(transcription.MakeIBMTaskFunction(jsonData.AudioURL, jsonData.EmailAddresses, jsonData.SearchWords))
}
// initiateTranscriptionJobHandler takes a POST request from a form,
// decodes it into a transcriptionJobData struct, and starts a transcription task.
func initiateTranscriptionJobHandler(w http.ResponseWriter, r *http.Request) {
executer := tasks.DefaultTaskExecuter
emails := strings.Split(r.FormValue("emails"), ",")
words := strings.Split(r.FormValue("words"), ",")
log.Println(emails, words, len(emails), len(words))
id := executer.QueueTask(transcription.MakeIBMTaskFunction(r.FormValue("url"), emails, words))
session, err := store.Get(r, flashSession)
if err != nil {
log.Fatal(err)
}
session.AddFlash(flash{
Title: "Task Started!",
Body: fmt.Sprintf("Task %s was successfully started. The results will be emailed to you upon completion.", id),
})
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
// healthHandler returns a 200 response to the client if the server is healthy.
func healthHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "OK :)")
}
// jobStatusHandler returns the status of a task with given id.
func jobStatusHandler(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r)
id := args["id"]
executer := tasks.DefaultTaskExecuter
status := executer.GetTaskStatus(id)
io.WriteString(w, status.String())
}
func formHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/form.html")
if err != nil {
log.Fatal(err)
}
session, err := store.Get(r, flashSession)
if err != nil {
log.Fatal(err)
}
flashes := session.Flashes()
session.Save(r, w)
err = t.Execute(w, flashes)
if err != nil {
log.Fatal(err)
}
}