Commit 8a81bda58b6aadf3955ead9e59eeecb0bec0a341
1 parent
6faf94f857
Exists in
master
Moved Get/Post JSON to seperate file
Showing
3 changed files
with
107 additions
and
70 deletions
Show diff stats
diff.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | // Diff ... | 3 | // Diff ... |
4 | func Diff(old, new []int64) (added, removed []int64) { | 4 | func Diff(old, new []int64) (added, removed []int64) { |
5 | for i := range old { | 5 | for i := range old { |
6 | isRemoved := true | 6 | isRemoved := true |
7 | for j := range new { | 7 | for j := range new { |
8 | if old[i] == new[j] { | 8 | if old[i] == new[j] { |
9 | isRemoved = false | 9 | isRemoved = false |
10 | break | 10 | break |
11 | } | 11 | } |
12 | } | 12 | } |
13 | if isRemoved { | 13 | if isRemoved { |
14 | removed = append(removed, old[i]) | 14 | removed = append(removed, old[i]) |
15 | } | 15 | } |
16 | } | 16 | } |
17 | 17 | ||
18 | for i := range new { | 18 | for i := range new { |
19 | isAdded := true | 19 | isAdded := true |
20 | for j := range old { | 20 | for j := range old { |
21 | if new[i] == old[j] { | 21 | if new[i] == old[j] { |
22 | isAdded = false | 22 | isAdded = false |
23 | break | 23 | break |
24 | } | 24 | } |
25 | } | 25 | } |
26 | if isAdded { | 26 | if isAdded { |
27 | added = append(added, new[i]) | 27 | added = append(added, new[i]) |
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||
31 | return added, removed | 31 | return added, removed |
32 | } | 32 | } |
33 | |||
34 | // DiffString ... | ||
35 | func DiffString(old, new []string) (added, removed []string) { | ||
36 | for i := range old { | ||
37 | isRemoved := true | ||
38 | for j := range new { | ||
39 | if old[i] == new[j] { | ||
40 | isRemoved = false | ||
41 | break | ||
42 | } | ||
43 | } | ||
44 | if isRemoved { | ||
45 | removed = append(removed, old[i]) | ||
46 | } | ||
47 | } | ||
48 | |||
49 | for i := range new { | ||
50 | isAdded := true | ||
51 | for j := range old { | ||
52 | if new[i] == old[j] { | ||
53 | isAdded = false | ||
54 | break | ||
55 | } | ||
56 | } | ||
57 | if isAdded { | ||
58 | added = append(added, new[i]) | ||
59 | } | ||
60 | } | ||
61 | |||
62 | return added, removed | ||
63 | } | ||
33 | 64 |
http.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "bytes" | ||
5 | "encoding/json" | 4 | "encoding/json" |
6 | "fmt" | 5 | "fmt" |
7 | "io" | 6 | "io" |
8 | "net/http" | 7 | "net/http" |
9 | "net/url" | ||
10 | ) | 8 | ) |
11 | 9 | ||
12 | // StatusRecorder ... | 10 | // StatusRecorder ... |
13 | type StatusRecorder struct { | 11 | type StatusRecorder struct { |
14 | writer http.ResponseWriter | 12 | writer http.ResponseWriter |
15 | status int | 13 | status int |
16 | size int | 14 | size int |
17 | } | 15 | } |
18 | 16 | ||
19 | // NewStatusRecorder ... | 17 | // NewStatusRecorder ... |
20 | func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder { | 18 | func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder { |
21 | return &StatusRecorder{ | 19 | return &StatusRecorder{ |
22 | writer: w, | 20 | writer: w, |
23 | status: 0, | 21 | status: 0, |
24 | size: 0, | 22 | size: 0, |
25 | } | 23 | } |
26 | } | 24 | } |
27 | 25 | ||
28 | // WriteHeader is a wrapper http.ResponseWriter interface | 26 | // WriteHeader is a wrapper http.ResponseWriter interface |
29 | func (r *StatusRecorder) WriteHeader(code int) { | 27 | func (r *StatusRecorder) WriteHeader(code int) { |
30 | r.status = code | 28 | r.status = code |
31 | r.writer.WriteHeader(code) | 29 | r.writer.WriteHeader(code) |
32 | } | 30 | } |
33 | 31 | ||
34 | // Write is a wrapper for http.ResponseWriter interface | 32 | // Write is a wrapper for http.ResponseWriter interface |
35 | func (r *StatusRecorder) Write(in []byte) (int, error) { | 33 | func (r *StatusRecorder) Write(in []byte) (int, error) { |
36 | r.size = len(in) | 34 | r.size = len(in) |
37 | return r.writer.Write(in) | 35 | return r.writer.Write(in) |
38 | } | 36 | } |
39 | 37 | ||
40 | // Header is a wrapper for http.ResponseWriter interface | 38 | // Header is a wrapper for http.ResponseWriter interface |
41 | func (r *StatusRecorder) Header() http.Header { | 39 | func (r *StatusRecorder) Header() http.Header { |
42 | return r.writer.Header() | 40 | return r.writer.Header() |
43 | } | 41 | } |
44 | 42 | ||
45 | // Status ... | 43 | // Status ... |
46 | func (r *StatusRecorder) Status() int { | 44 | func (r *StatusRecorder) Status() int { |
47 | return r.status | 45 | return r.status |
48 | } | 46 | } |
49 | 47 | ||
50 | // Size ... | 48 | // Size ... |
51 | func (r *StatusRecorder) Size() int { | 49 | func (r *StatusRecorder) Size() int { |
52 | return r.size | 50 | return r.size |
53 | } | 51 | } |
54 | 52 | ||
55 | // NotFoundHandlerFunc writes HTTP error 404 to w. | 53 | // NotFoundHandlerFunc writes HTTP error 404 to w. |
56 | func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) { | 54 | func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) { |
57 | SetDefaultHeaders(w) | 55 | SetDefaultHeaders(w) |
58 | if req.Method == "OPTIONS" { | 56 | if req.Method == "OPTIONS" { |
59 | return | 57 | return |
60 | } | 58 | } |
61 | NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String())) | 59 | NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String())) |
62 | } | 60 | } |
63 | 61 | ||
64 | // SetContentType ... | 62 | // SetContentType ... |
65 | func SetContentType(w http.ResponseWriter, ctype string) { | 63 | func SetContentType(w http.ResponseWriter, ctype string) { |
66 | w.Header().Set("Content-Type", ctype) | 64 | w.Header().Set("Content-Type", ctype) |
67 | } | 65 | } |
68 | 66 | ||
69 | // SetResponseStatus ... | 67 | // SetResponseStatus ... |
70 | func SetResponseStatus(w http.ResponseWriter, status int) { | 68 | func SetResponseStatus(w http.ResponseWriter, status int) { |
71 | w.WriteHeader(status) | 69 | w.WriteHeader(status) |
72 | } | 70 | } |
73 | 71 | ||
74 | // WriteResponse ... | 72 | // WriteResponse ... |
75 | func WriteResponse(w http.ResponseWriter, content []byte) { | 73 | func WriteResponse(w http.ResponseWriter, content []byte) { |
76 | w.Write(content) | 74 | w.Write(content) |
77 | } | 75 | } |
78 | 76 | ||
79 | // SetDefaultHeaders set's default headers for an HTTP response. | 77 | // SetDefaultHeaders set's default headers for an HTTP response. |
80 | func SetDefaultHeaders(w http.ResponseWriter) { | 78 | func SetDefaultHeaders(w http.ResponseWriter) { |
81 | w.Header().Set("Access-Control-Allow-Origin", "*") | 79 | w.Header().Set("Access-Control-Allow-Origin", "*") |
82 | w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") | 80 | w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") |
83 | w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") | 81 | w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") |
84 | SetContentType(w, "application/json; charset=utf-8") | 82 | SetContentType(w, "application/json; charset=utf-8") |
85 | } | 83 | } |
86 | 84 | ||
87 | // GetLocale ... | 85 | // GetLocale ... |
88 | func GetLocale(req *http.Request, dflt string) string { | 86 | func GetLocale(req *http.Request, dflt string) string { |
89 | loc := req.FormValue("locale") | 87 | loc := req.FormValue("locale") |
90 | if loc == "" { | 88 | if loc == "" { |
91 | return dflt | 89 | return dflt |
92 | } | 90 | } |
93 | return loc | 91 | return loc |
94 | } | 92 | } |
95 | 93 | ||
96 | // Success ... | 94 | // Success ... |
97 | func Success(w http.ResponseWriter, payload interface{}, code int) { | 95 | func Success(w http.ResponseWriter, payload interface{}, code int) { |
98 | w.WriteHeader(code) | 96 | w.WriteHeader(code) |
99 | if payload != nil { | 97 | if payload != nil { |
100 | json.NewEncoder(w).Encode(payload) | 98 | json.NewEncoder(w).Encode(payload) |
101 | } | 99 | } |
102 | } | 100 | } |
103 | 101 | ||
104 | // OK ... | 102 | // OK ... |
105 | func OK(w http.ResponseWriter, payload interface{}) { | 103 | func OK(w http.ResponseWriter, payload interface{}) { |
106 | Success(w, payload, http.StatusOK) | 104 | Success(w, payload, http.StatusOK) |
107 | } | 105 | } |
108 | 106 | ||
109 | // Created ... | 107 | // Created ... |
110 | func Created(w http.ResponseWriter, payload interface{}) { | 108 | func Created(w http.ResponseWriter, payload interface{}) { |
111 | Success(w, payload, http.StatusCreated) | 109 | Success(w, payload, http.StatusCreated) |
112 | } | 110 | } |
113 | 111 | ||
114 | type weberror struct { | 112 | type weberror struct { |
115 | Request string `json:"request"` | 113 | Request string `json:"request"` |
116 | Error string `json:"error"` | 114 | Error string `json:"error"` |
117 | } | 115 | } |
118 | 116 | ||
119 | // Error ... | 117 | // Error ... |
120 | func Error(w http.ResponseWriter, r *http.Request, code int, err string) { | 118 | func Error(w http.ResponseWriter, r *http.Request, code int, err string) { |
121 | werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI} | 119 | werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI} |
122 | w.WriteHeader(code) | 120 | w.WriteHeader(code) |
123 | json.NewEncoder(w).Encode(werr) | 121 | json.NewEncoder(w).Encode(werr) |
124 | } | 122 | } |
125 | 123 | ||
126 | // BadRequest ... | 124 | // BadRequest ... |
127 | func BadRequest(w http.ResponseWriter, r *http.Request, err string) { | 125 | func BadRequest(w http.ResponseWriter, r *http.Request, err string) { |
128 | Error(w, r, http.StatusBadRequest, err) | 126 | Error(w, r, http.StatusBadRequest, err) |
129 | } | 127 | } |
130 | 128 | ||
131 | // Unauthorized ... | 129 | // Unauthorized ... |
132 | func Unauthorized(w http.ResponseWriter, r *http.Request, err string) { | 130 | func Unauthorized(w http.ResponseWriter, r *http.Request, err string) { |
133 | Error(w, r, http.StatusUnauthorized, err) | 131 | Error(w, r, http.StatusUnauthorized, err) |
134 | } | 132 | } |
135 | 133 | ||
136 | // Forbidden ... | 134 | // Forbidden ... |
137 | func Forbidden(w http.ResponseWriter, r *http.Request, err string) { | 135 | func Forbidden(w http.ResponseWriter, r *http.Request, err string) { |
138 | Error(w, r, http.StatusForbidden, err) | 136 | Error(w, r, http.StatusForbidden, err) |
139 | } | 137 | } |
140 | 138 | ||
141 | // NotFound ... | 139 | // NotFound ... |
142 | func NotFound(w http.ResponseWriter, r *http.Request, err string) { | 140 | func NotFound(w http.ResponseWriter, r *http.Request, err string) { |
143 | Error(w, r, http.StatusNotFound, err) | 141 | Error(w, r, http.StatusNotFound, err) |
144 | } | 142 | } |
145 | 143 | ||
146 | // Conflict ... | 144 | // Conflict ... |
147 | func Conflict(w http.ResponseWriter, r *http.Request, err string) { | 145 | func Conflict(w http.ResponseWriter, r *http.Request, err string) { |
148 | Error(w, r, http.StatusConflict, err) | 146 | Error(w, r, http.StatusConflict, err) |
149 | } | 147 | } |
150 | 148 | ||
151 | // InternalServerError ... | 149 | // InternalServerError ... |
152 | func InternalServerError(w http.ResponseWriter, r *http.Request, err string) { | 150 | func InternalServerError(w http.ResponseWriter, r *http.Request, err string) { |
153 | Error(w, r, http.StatusInternalServerError, err) | 151 | Error(w, r, http.StatusInternalServerError, err) |
154 | } | 152 | } |
155 | 153 | ||
156 | // DecodeJSON decodes JSON data from r to v. | 154 | // DecodeJSON decodes JSON data from r to v. |
157 | // Returns an error if it fails. | 155 | // Returns an error if it fails. |
158 | func DecodeJSON(r io.Reader, v interface{}) error { | 156 | func DecodeJSON(r io.Reader, v interface{}) error { |
159 | return json.NewDecoder(r).Decode(v) | 157 | return json.NewDecoder(r).Decode(v) |
160 | } | 158 | } |
161 | |||
162 | func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) { | ||
163 | p := params.Encode() | ||
164 | if p != "" { | ||
165 | url += "?" + p | ||
166 | } | ||
167 | |||
168 | req, err := http.NewRequest(http.MethodGet, url, nil) | ||
169 | if err != nil { | ||
170 | return 0, err | ||
171 | } | ||
172 | |||
173 | if headers != nil { | ||
174 | for k, head := range headers { | ||
175 | for i, h := range head { | ||
176 | if i == 0 { | ||
177 | req.Header.Set(k, h) | ||
178 | } else { | ||
179 | req.Header.Add(k, h) | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | } | ||
184 | |||
185 | resp, err := http.DefaultClient.Do(req) | ||
186 | if err != nil { | ||
187 | return 0, err | ||
188 | } | ||
189 | status = resp.StatusCode | ||
190 | |||
191 | return status, DecodeJSON(resp.Body, v) | ||
192 | } | ||
193 | |||
194 | func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) { | ||
195 | buffer := bytes.NewBuffer(make([]byte, 0)) | ||
196 | json.NewEncoder(buffer).Encode(v) | ||
197 | |||
198 | p := params.Encode() | ||
199 | if p != "" { | ||
200 | url += "?" + p | ||
201 | } | ||
202 | |||
203 | req, err := http.NewRequest(http.MethodPost, url, buffer) | ||
204 | if err != nil { | ||
205 | return 0, err | ||
206 | } | ||
207 | |||
208 | if headers != nil { | ||
209 | for k, head := range headers { | ||
210 | for i, h := range head { | ||
211 | if i == 0 { | ||
212 | req.Header.Set(k, h) | ||
213 | } else { | ||
214 | req.Header.Add(k, h) | ||
215 | } | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | req.Header.Set("Content-Type", "application/json") | ||
220 | |||
221 | resp, err := http.DefaultClient.Do(req) | ||
222 | if err != nil { | ||
223 | return 0, err | ||
224 | } | ||
225 | status = resp.StatusCode | ||
226 | |||
227 | return status, DecodeJSON(resp.Body, r) | ||
228 | } | ||
229 | 159 |
json.go
File was created | 1 | package webutility | |
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "encoding/json" | ||
6 | "net/http" | ||
7 | "net/url" | ||
8 | ) | ||
9 | |||
10 | func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) { | ||
11 | p := params.Encode() | ||
12 | if p != "" { | ||
13 | url += "?" + p | ||
14 | } | ||
15 | |||
16 | req, err := http.NewRequest(http.MethodGet, url, nil) | ||
17 | if err != nil { | ||
18 | return 0, err | ||
19 | } | ||
20 | |||
21 | if headers != nil { | ||
22 | for k, head := range headers { | ||
23 | for i, h := range head { | ||
24 | if i == 0 { | ||
25 | req.Header.Set(k, h) | ||
26 | } else { | ||
27 | req.Header.Add(k, h) | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | } | ||
32 | |||
33 | resp, err := http.DefaultClient.Do(req) | ||
34 | if err != nil { | ||
35 | return 0, err | ||
36 | } | ||
37 | status = resp.StatusCode | ||
38 | |||
39 | return status, DecodeJSON(resp.Body, v) | ||
40 | } | ||
41 | |||
42 | func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) { | ||
43 | buffer := bytes.NewBuffer(make([]byte, 0)) | ||
44 | json.NewEncoder(buffer).Encode(v) | ||
45 | |||
46 | p := params.Encode() | ||
47 | if p != "" { | ||
48 | url += "?" + p | ||
49 | } | ||
50 | |||
51 | req, err := http.NewRequest(http.MethodPost, url, buffer) | ||
52 | if err != nil { | ||
53 | return 0, err | ||
54 | } | ||
55 | |||
56 | if headers != nil { | ||
57 | for k, head := range headers { | ||
58 | for i, h := range head { | ||
59 | if i == 0 { | ||
60 | req.Header.Set(k, h) | ||
61 | } else { | ||
62 | req.Header.Add(k, h) | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | req.Header.Set("Content-Type", "application/json") | ||
68 | |||
69 | resp, err := http.DefaultClient.Do(req) | ||
70 | if err != nil { | ||
71 | return 0, err | ||
72 | } | ||
73 | status = resp.StatusCode | ||
74 | |||
75 | return status, DecodeJSON(resp.Body, r) | ||
76 | } | ||
77 |