Mercurial > gemma
annotate pkg/controllers/json.go @ 2685:39a05f8c34e6 import-overview-rework
import_overview: Refactoring of detailed information.
When the entry is opened, a request is made to retrieve the
detailed information for this entry. It contains the log
protocol information as well as the executive summary.
This is passed down to child components of the entry.
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Fri, 15 Mar 2019 13:42:49 +0100 |
parents | 09f9ae3d0526 |
children | 1458c9b0fdaa |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
979
diff
changeset
|
13 |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 "database/sql" |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "encoding/json" |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "fmt" |
739
b800eb2a0846
JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
536
diff
changeset
|
20 "io" |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "log" |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 "net/http" |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
241
3b688fe04c39
No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
240
diff
changeset
|
24 "github.com/jackc/pgx" |
3b688fe04c39
No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
240
diff
changeset
|
25 |
414
c1047fd04a3a
Moved project specific Go packages to new pkg folder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
341
diff
changeset
|
26 "gemma.intevation.de/gemma/pkg/auth" |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 ) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
29 // JSONResult defines the return type of JSONHandler handler function. |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 type JSONResult struct { |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
31 // Code is the HTTP status code to be set which defaults to http.StatusOK (200). |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
32 Code int |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
33 // Result is serialized to JSON. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
34 // If the type is an io.Reader its copied through. |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 Result interface{} |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
38 // JSONDefaultLimit is default size limit in bytes of an accepted |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
39 // input document. |
1685
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
40 const JSONDefaultLimit = 2048 |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
41 |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
42 // JSONHandler implements a middleware to ease the handing JSON input |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
43 // streams and return JSON documents as output. |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 type JSONHandler struct { |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
45 // Input (if not nil) is called to fill a data structure |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
46 // returned by this function. |
2058
09f9ae3d0526
Imports: Handle manual imports with a single route using the shortnames of the imports to distiquish between them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1693
diff
changeset
|
47 Input func(*http.Request) interface{} |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
48 // Handle is called to handle the incoming HTTP request. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
49 // in is the data structure returned by Input. Its nil if Input is nil. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
50 // req is the incoming HTTP request. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
51 // conn is the impersonated connection to the database. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
52 Handle func(in interface{}, rep *http.Request, conn *sql.Conn) (JSONResult, error) |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
53 // NoConn if set to true no database connection is established and |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
54 // the conn parameter of the Handle call is nil. |
486
b2dc9c2f69e0
First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
55 NoConn bool |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
56 // Limit overides the default size of accepted input documents. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
57 // Set to a negative value to allow an arbitrary size. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
58 // Handle with care! |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
59 Limit int64 |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
62 // JSONError is an error if returned by the JSONHandler.Handle function |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
63 // which ends up encoded as a JSON document. |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 type JSONError struct { |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
65 // Code is the HTTP status code of the result defaults |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
66 // to http.StatusInternalServerError if not set. |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
67 Code int |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
68 // The message of the error. |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 Message string |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
72 // Error implements the error interface. |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 func (je JSONError) Error() string { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 return fmt.Sprintf("%d: %s", je.Code, je.Message) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
77 // ServeHTTP makes the JSONHandler a middleware. |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 func (j *JSONHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 var input interface{} |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 if j.Input != nil { |
2058
09f9ae3d0526
Imports: Handle manual imports with a single route using the shortnames of the imports to distiquish between them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1693
diff
changeset
|
82 input = j.Input(req) |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 defer req.Body.Close() |
1685
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
84 var r io.Reader |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
85 switch { |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
86 case j.Limit == 0: |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
87 r = io.LimitReader(req.Body, JSONDefaultLimit) |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
88 case j.Limit > 0: |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
89 r = io.LimitReader(req.Body, j.Limit) |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
90 default: |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
91 r = req.Body |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
92 } |
8f5a5c86f2a9
JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1327
diff
changeset
|
93 if err := json.NewDecoder(r).Decode(input); err != nil { |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 http.Error(rw, "error: "+err.Error(), http.StatusBadRequest) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 return |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 var jr JSONResult |
302
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
257
diff
changeset
|
100 var err error |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
257
diff
changeset
|
101 |
486
b2dc9c2f69e0
First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
102 if token, ok := auth.GetToken(req); ok && !j.NoConn { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
493
diff
changeset
|
103 if session := auth.Sessions.Session(token); session != nil { |
1327
cabf4789e02b
To make golint happier made context.Context to be the first argument of auth.RunAs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
104 err = auth.RunAs(req.Context(), session.User, func(conn *sql.Conn) error { |
486
b2dc9c2f69e0
First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
105 jr, err = j.Handle(input, req, conn) |
514
4a1db55a9920
Use auth.RunAs in JSON controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
508
diff
changeset
|
106 return err |
4a1db55a9920
Use auth.RunAs in JSON controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
508
diff
changeset
|
107 }) |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
493
diff
changeset
|
108 } else { |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
493
diff
changeset
|
109 err = auth.ErrNoSuchToken |
486
b2dc9c2f69e0
First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
110 } |
302
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
257
diff
changeset
|
111 } else { |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
257
diff
changeset
|
112 jr, err = j.Handle(input, req, nil) |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
257
diff
changeset
|
113 } |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 if err != nil { |
536
d9dbb6139760
Log errors in JSON handler and login controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
514
diff
changeset
|
116 log.Printf("error: %v\n", err) |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 switch e := err.(type) { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 case pgx.PgError: |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 var res = struct { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 Result string `json:"result"` |
241
3b688fe04c39
No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
240
diff
changeset
|
121 Code string `json:"code"` |
3b688fe04c39
No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
240
diff
changeset
|
122 Message string `json:"message"` |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 }{ |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 Result: "failure", |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 Code: e.Code, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
126 Message: e.Message, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 rw.Header().Set("Content-Type", "application/json") |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 rw.WriteHeader(http.StatusInternalServerError) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 if err := json.NewEncoder(rw).Encode(&res); err != nil { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 log.Printf("error: %v\n", err) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
132 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
133 case JSONError: |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
134 rw.Header().Set("Content-Type", "application/json") |
341
889517f254f5
Use code of JSONError as HTTP code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
302
diff
changeset
|
135 if e.Code == 0 { |
889517f254f5
Use code of JSONError as HTTP code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
302
diff
changeset
|
136 e.Code = http.StatusInternalServerError |
889517f254f5
Use code of JSONError as HTTP code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
302
diff
changeset
|
137 } |
889517f254f5
Use code of JSONError as HTTP code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
302
diff
changeset
|
138 rw.WriteHeader(e.Code) |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
139 var res = struct { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 Message string `json:"message"` |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 }{ |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 Message: e.Message, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 if err := json.NewEncoder(rw).Encode(&res); err != nil { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 log.Printf("error: %v\n", err) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 default: |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 http.Error(rw, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 "error: "+err.Error(), |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 http.StatusInternalServerError) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 return |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
154 |
238
2b39bf2bf1fd
If no error code was given in a JSONResult assume Status OK (200).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
155 if jr.Code == 0 { |
2b39bf2bf1fd
If no error code was given in a JSONResult assume Status OK (200).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
156 jr.Code = http.StatusOK |
2b39bf2bf1fd
If no error code was given in a JSONResult assume Status OK (200).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
157 } |
2b39bf2bf1fd
If no error code was given in a JSONResult assume Status OK (200).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
158 |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
239
diff
changeset
|
159 if jr.Code != http.StatusNoContent { |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
239
diff
changeset
|
160 rw.Header().Set("Content-Type", "application/json") |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
239
diff
changeset
|
161 } |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
162 rw.WriteHeader(jr.Code) |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
239
diff
changeset
|
163 if jr.Code != http.StatusNoContent { |
739
b800eb2a0846
JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
536
diff
changeset
|
164 var err error |
741
8bb2e48e2dfd
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
739
diff
changeset
|
165 if r, ok := jr.Result.(io.Reader); ok { |
739
b800eb2a0846
JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
536
diff
changeset
|
166 _, err = io.Copy(rw, r) |
b800eb2a0846
JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
536
diff
changeset
|
167 } else { |
b800eb2a0846
JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
536
diff
changeset
|
168 err = json.NewEncoder(rw).Encode(jr.Result) |
b800eb2a0846
JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
536
diff
changeset
|
169 } |
b800eb2a0846
JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
536
diff
changeset
|
170 if err != nil { |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
239
diff
changeset
|
171 log.Printf("error: %v\n", err) |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
239
diff
changeset
|
172 } |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 } |
979
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
175 |
1693
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
176 // SendJSON sends data JSON encoded to the response writer |
cdc8933949f2
Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
1685
diff
changeset
|
177 // with a given HTTP status code. |
979
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
178 func SendJSON(rw http.ResponseWriter, code int, data interface{}) { |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
179 rw.Header().Set("Content-Type", "application/json") |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
180 rw.WriteHeader(code) |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
181 if err := json.NewEncoder(rw).Encode(data); err != nil { |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
182 log.Printf("error: %v\n", err) |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
183 } |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
741
diff
changeset
|
184 } |