This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpublish_handler.go
131 lines (122 loc) · 3.59 KB
/
publish_handler.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
package main
import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"io"
"io/ioutil"
"net/http"
)
type publishHandler struct {
publishService *publisher
}
func newPublishHandler(publishService *publisher) publishHandler {
return publishHandler{publishService: publishService}
}
func (j createJobRequest) String() string {
return fmt.Sprintf("URL=\"%s\" Throttle=%d", j.URL, j.Throttle)
}
func (h publishHandler) createJob(w http.ResponseWriter, r *http.Request) {
var jobRequest createJobRequest
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&jobRequest); err != nil {
err := fmt.Sprintf("Invalid payload: (%v)", err)
log.Warn(err)
http.Error(w, err, http.StatusBadRequest)
return
}
if jobRequest.ConceptType == "" {
err := "Concept type not specified. You need the field 'concept' as e.g. organisations, special-reports"
log.Warn(err)
http.Error(w, err, http.StatusBadRequest)
return
}
if jobRequest.URL == "" {
err := "'url' field can't be empty"
log.Warn(err)
http.Error(w, err, http.StatusBadRequest)
return
}
log.Infof("message=\"Concept publish request received\" %v", jobRequest)
theJob, err := (*h.publishService).createJob(jobRequest.ConceptType, jobRequest.IDS, jobRequest.URL, jobRequest.GtgURL, jobRequest.Throttle)
if err != nil {
log.Errorf("%v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
w.Header().Add("Content-Type", "application/json")
type shortJob struct {
JobID string `json:"jobID"`
}
sj := shortJob{JobID: theJob.jobID}
enc := json.NewEncoder(w)
err = enc.Encode(sj)
if err != nil {
log.Errorf("Error on json encoding=%v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go (*h.publishService).runJob(theJob, jobRequest.Authorization)
}
func (h publishHandler) status(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
theJob, err := (*h.publishService).getJob(id)
if err != nil {
log.Errorf("message=\"Error returning job\" %v\n", err)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
var filteredJob *job
if _, ok := r.URL.Query()["full"]; !ok {
filteredJob = theJob.getJobFiltered()
} else {
filteredJob = theJob.getJob()
}
w.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(w)
theJob.RLock()
defer theJob.RUnlock()
if err := enc.Encode(filteredJob); err != nil {
log.Errorf("message=\"Error on json encoding\" %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h publishHandler) listJobs(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err := enc.Encode((*h.publishService).getJobIds()); err != nil {
log.Errorf("message=\"Error on json encoding\" %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h publishHandler) deleteJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
err := (*h.publishService).deleteJob(id)
if err != nil {
_, ok := err.(*notFoundError)
code := http.StatusInternalServerError
if ok {
code = http.StatusNotFound
}
log.Infof("message=\"Error deleting job\" %v\n", err)
http.Error(w, err.Error(), code)
return
}
w.WriteHeader(http.StatusNoContent)
}
func closeNice(resp *http.Response) {
_, err := io.Copy(ioutil.Discard, resp.Body)
if err != nil {
log.Warnf("message=\"couldn't read response body\" %v", err)
}
err = resp.Body.Close()
if err != nil {
log.Warnf("message=\"couldn't close response body\" %v", err)
}
}