-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
297 lines (255 loc) · 7.43 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"regexp"
"runtime/debug"
"strconv"
log "github.com/sirupsen/logrus"
"github.com/ghodss/yaml"
"github.com/gorilla/mux"
publiccode "github.com/italia/publiccode-parser-go"
"github.com/italia/publiccode-validator/apiv1"
"github.com/italia/publiccode-validator/utils"
)
var (
version string
date string
)
// App localize type
type App utils.App
func init() {
if version == "" {
version = "devel"
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
}
}
if date == "" {
date = "(latest)"
}
log.Infof("version %s compiled %s\n", version, date)
}
// main server start
func main() {
app := App{}
app.Port = "5000"
app.DisableNetwork = false
app.initializeRouters()
// server run here because of tests
// https://github.com/gorilla/mux#testing-handlers
log.Infof("server is starting at port %s", app.Port)
log.Fatal(http.ListenAndServe(":"+app.Port, app.Router))
}
func (app *App) initializeRouters() {
app.Router = mux.NewRouter()
var api = app.Router.PathPrefix("/api").Subrouter()
var api1 = api.PathPrefix("/v1").Subrouter()
// v0
app.Router.
HandleFunc("/pc/validate", app.validateParam).
Methods("POST", "OPTIONS").
Queries("disableNetwork", "{disableNetwork}")
app.Router.
HandleFunc("/pc/validate", app.validate).
Methods("POST", "OPTIONS")
app.Router.
HandleFunc("/pc/validateURL", app.validateRemoteURL).
Methods("POST", "OPTIONS").
Queries("url", "{url}")
// v1
api1.
HandleFunc("/validate", apiv1.ValidateParam).
Methods("POST", "OPTIONS").
Queries("disableNetwork", "{disableNetwork}")
api1.
HandleFunc("/validate", apiv1.Validate).
Methods("POST", "OPTIONS")
api1.
HandleFunc("/validateURL", apiv1.ValidateRemoteURL).
Methods("POST", "OPTIONS").
Queries("url", "{url}")
}
// parse returns new parsed and validated buffer and errors if any
func (app *App) parse(b []byte) ([]byte, error, error) {
url, err := utils.GetURLFromYMLBuffer(b)
if err != nil {
// this error should not be blocking because it just means
// that no url is inside the request body.
// one case for that: partial validation during editing
log.Warnf("url not found in body (useful to get RemoteBaseURL): %s", err)
}
p := publiccode.NewParser()
p.DisableNetwork = app.DisableNetwork
if url != nil {
p.DisableNetwork = app.DisableNetwork
p.RemoteBaseURL = utils.GetRawURL(url)
}
log.Debugf("parse() called with disableNetwork: %v, and remoteBaseUrl: %s", p.DisableNetwork, p.RemoteBaseURL)
errParse := p.Parse(b)
pc, err := p.ToYAML()
// hack to reset global vars to default values
app.DisableNetwork = false
return pc, errParse, err
}
func (app *App) parseRemoteURL(urlString string) ([]byte, error, error) {
log.Infof("called parseRemoteURL() url: %s", urlString)
p := publiccode.NewParser()
urlString, err := utils.GetRawFile(urlString)
if err != nil {
return nil, nil, err
}
errParse := p.ParseRemoteFile(urlString)
pc, err := p.ToYAML()
return pc, errParse, err
}
func promptError(err error, w http.ResponseWriter,
httpStatus int, mess string) {
log.Errorf(mess+": %v", err)
message := utils.Message{
Status: httpStatus,
Message: mess,
Error: err.Error(),
}
log.Debugf("message: %v", message)
w.Header().Set("Content-type", "application/json")
w.WriteHeader(message.Status)
json.NewEncoder(w).Encode(message)
}
func promptValidationErrors(err error, w http.ResponseWriter,
httpStatus int, mess string) {
log.Errorf(mess+": %v", err)
message := utils.Message{
Status: httpStatus,
Message: mess,
ValidationError: utils.ErrorsToValidationErrors(err),
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(message.Status)
messageJSON, _ := json.Marshal(message)
w.Write(messageJSON)
}
func (app *App) validateRemoteURL(w http.ResponseWriter, r *http.Request) {
log.Info("called validateFromURL()")
utils.SetupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
// Getting vars from parameters
vars := mux.Vars(r)
urlString := vars["url"]
if urlString == "" {
promptError(errors.New("Not found"), w, http.StatusNotFound, "URL error")
return
}
// parsing
pc, errParse, errConverting := app.parseRemoteURL(urlString)
if errConverting != nil {
promptError(errConverting, w, http.StatusBadRequest, "Error converting")
return
}
if errParse != nil {
if match, _ := regexp.MatchString(`404`, errParse.Error()); match {
promptError(errors.New("Not found"), w, http.StatusNotFound, "URL error")
return
}
promptValidationErrors(errParse, w, http.StatusUnprocessableEntity, "Validation Errors")
} else {
// set response CT based on client accept header
// and return respectively content
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-type", "application/json")
w.Write(utils.Yaml2json(pc))
return
}
// default choice
w.Header().Set("Content-type", "application/x-yaml")
w.Write(pc)
}
}
// validateParam will take a query parameter to enable
// or disable network layer on parsing process
// and then call normal validation
func (app *App) validateParam(w http.ResponseWriter, r *http.Request) {
log.Info("called validateParam()")
utils.SetupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
// Getting vars from parameters
vars := mux.Vars(r)
disableNetwork, err := strconv.ParseBool(vars["disableNetwork"])
app.DisableNetwork = disableNetwork
if err != nil {
app.DisableNetwork = false
log.Info("var disableNetwork not set, default to true")
}
app.validate(w, r)
}
// validate returns a YML or JSON onbject validated and upgraded
// to latest PublicCode version specs.
// It accepts both format as input YML|JSON
func (app *App) validate(w http.ResponseWriter, r *http.Request) {
log.Info("called validate()")
utils.SetupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
if r.Body == nil {
log.Info("empty payload")
return
}
// Closing body after operations
defer r.Body.Close()
// reading request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
promptError(err, w, http.StatusBadRequest, "Error reading body")
return
}
// these functions take as argument a request body
// convert content in YAML format if needed
// and return a bytes array needed from Parser
// to validate correctly
// here, based on content-type header must convert
// [yaml/json] content into []byte
var m []byte
if r.Header.Get("Content-Type") == "application/json" {
//converting to YML
m, err = yaml.JSONToYAML(body)
if err != nil {
promptError(err, w, http.StatusBadRequest, "Conversion to json ko")
return
}
} else {
m = body
}
// parsing
pc, errParse, errConverting := app.parse(m)
if errConverting != nil {
promptError(errConverting, w, http.StatusBadRequest, "Error converting")
return
}
if errParse != nil {
log.Debugf("Validation Errors: %s", errParse)
// consider switch to promptError()
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusUnprocessableEntity)
json, _ := json.Marshal(errParse)
w.Write(json)
// promptError(errParse, w, http.StatusUnprocessableEntity, "Error parsing")
} else {
// set response CT based on client accept header
// and return respectively content
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-type", "application/json")
w.Write(utils.Yaml2json(pc))
return
}
// default choice
w.Header().Set("Content-type", "application/x-yaml")
w.Write(pc)
}
}