-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
50 lines (42 loc) · 1.05 KB
/
helper.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
package main
import (
"encoding/json"
"net/http"
"strconv"
)
// show returns JSON HTTP response and status code
func show(response http.ResponseWriter, statusCode int, v interface{}) {
response.Header().Set("Content-Type", "application/json")
response.WriteHeader(statusCode)
err, ok := v.(error)
if ok {
v = ServiceError{
Message: err.Error(),
Level: "error",
Code: statusCode,
}
}
json.NewEncoder(response).Encode(v)
}
// mapQueries map cqueries parameters with fizzBuzz struct
func mapQueries(request *http.Request) *FizzBuzzParams {
queryValues := request.URL.Query()
query1 := queryValues.Get("int1")
int1, _ := parseInt(query1)
query2 := queryValues.Get("int2")
int2, _ := parseInt(query2)
query3 := queryValues.Get("limit")
limit, _ := parseInt(query3)
fizzBuzz := FizzBuzzParams{
Int1: int1,
Int2: int2,
Limit: limit,
Str1: queryValues.Get("str1"),
Str2: queryValues.Get("str2"),
}
return &fizzBuzz
}
// parseInt convetes string to interger
func parseInt(str string) (int, error) {
return strconv.Atoi(str)
}