annotate pkg/middleware/jsonhandler.go @ 5591:0011f50cf216 surveysperbottleneckid

Removed no longer used alternative api for surveys/ endpoint. As bottlenecks in the summary for SR imports are now identified by their id and no longer by the (not guarantied to be unique!) name, there is no longer the need to request survey data by the name+date tuple (which isn't reliable anyway). So the workaround was now reversed.
author Sascha Wilde <wilde@sha-bang.de>
date Wed, 06 Apr 2022 13:30:29 +0200
parents 5f47eeea988d
children 6270951dda28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
14 package middleware
237
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 (
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
17 "context"
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "encoding/json"
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "fmt"
739
b800eb2a0846 JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 536
diff changeset
21 "io"
237
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"
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4829
diff changeset
27 "gemma.intevation.de/gemma/pkg/log"
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 )
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
30 // 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
31 type JSONResult struct {
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
32 // 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
33 Code int
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
34 // 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
35 // 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
36 Result interface{}
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
39 // 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
40 // 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
41 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
42
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
43 // 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
44 // 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
45 type JSONHandler struct {
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
46 // 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
47 // 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
48 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
49 // 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
50 // in is the data structure returned by Input. Its nil if Input is nil.
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
51 Handle func(rep *http.Request) (JSONResult, error)
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
52 // 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
53 // 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
54 NoConn bool
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
55 // 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
56 // 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
57 // Handle with care!
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
58 Limit int64
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
61 // 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
62 // 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
63 type JSONError struct {
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
64 // 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
65 // 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
66 Code int
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
67 // 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
68 Message string
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
71 // 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
72 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
73 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
74 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
76 type jsonHandlerType int
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
77
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
78 const (
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
79 jsonHandlerConnKey jsonHandlerType = iota
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
80 jsonHandlerInputKey
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
81 )
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
82
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
83 // JSONConn extracts the impersonated sql.Conn from the context of the request.
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
84 func JSONConn(req *http.Request) *sql.Conn {
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
85 if conn, ok := req.Context().Value(jsonHandlerConnKey).(*sql.Conn); ok {
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
86 return conn
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
87 }
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
88 return nil
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
89 }
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
90
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
91 // JSONInput extracts the de-serialized input from the context of the request.
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
92 func JSONInput(req *http.Request) interface{} {
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
93 return req.Context().Value(jsonHandlerInputKey)
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
94 }
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
95
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
96 // 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
97 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
98
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 if j.Input != nil {
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
100 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
101 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
102 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
103 switch {
8f5a5c86f2a9 JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1327
diff changeset
104 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
105 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
106 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
107 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
108 default:
8f5a5c86f2a9 JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1327
diff changeset
109 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
110 }
8f5a5c86f2a9 JSONHandler: Limited input JSON size to 2048 bytes by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1327
diff changeset
111 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
112 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
113 return
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 }
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
115 parent := req.Context()
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
116 ctx := context.WithValue(parent, jsonHandlerInputKey, input)
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
117 req = req.WithContext(ctx)
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 var jr JSONResult
302
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
121 var err error
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
122
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
123 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
124 if session := auth.Sessions.Session(token); session != nil {
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
125 parent := req.Context()
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2058
diff changeset
126 err = auth.RunAs(parent, session.User, func(conn *sql.Conn) error {
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
127 ctx := context.WithValue(parent, jsonHandlerConnKey, conn)
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
128 req = req.WithContext(ctx)
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
129 jr, err = j.Handle(req)
514
4a1db55a9920 Use auth.RunAs in JSON controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 508
diff changeset
130 return err
4a1db55a9920 Use auth.RunAs in JSON controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 508
diff changeset
131 })
498
22e1bf563a04 Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 493
diff changeset
132 } else {
22e1bf563a04 Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 493
diff changeset
133 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
134 }
302
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
135 } else {
4243
d776110b4db0 Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4242
diff changeset
136 jr, err = j.Handle(req)
302
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
137 }
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 if err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4829
diff changeset
140 log.Errorf("%v\n", err)
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 switch e := err.(type) {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 case pgx.PgError:
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 var res = struct {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 Result string `json:"result"`
241
3b688fe04c39 No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 240
diff changeset
145 Code string `json:"code"`
3b688fe04c39 No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 240
diff changeset
146 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
147 }{
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 Result: "failure",
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 Code: e.Code,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 Message: e.Message,
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 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
153 rw.WriteHeader(http.StatusInternalServerError)
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 if err := json.NewEncoder(rw).Encode(&res); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4829
diff changeset
155 log.Errorf("%v\n", err)
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 case JSONError:
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 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
159 if e.Code == 0 {
889517f254f5 Use code of JSONError as HTTP code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 302
diff changeset
160 e.Code = http.StatusInternalServerError
889517f254f5 Use code of JSONError as HTTP code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 302
diff changeset
161 }
889517f254f5 Use code of JSONError as HTTP code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 302
diff changeset
162 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
163 var res = struct {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 Message string `json:"message"`
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 }{
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 Message: e.Message,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 if err := json.NewEncoder(rw).Encode(&res); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4829
diff changeset
169 log.Errorf("%v\n", err)
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 default:
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 http.Error(rw,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 "error: "+err.Error(),
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 http.StatusInternalServerError)
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 return
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178
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
179 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
180 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
181 }
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
182
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 239
diff changeset
183 if jr.Code != http.StatusNoContent {
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 239
diff changeset
184 rw.Header().Set("Content-Type", "application/json")
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 239
diff changeset
185 }
4829
f4ec3558460e Set some nosniff http headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
186 rw.Header().Set("X-Content-Type-Options", "nosniff")
f4ec3558460e Set some nosniff http headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
187
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 rw.WriteHeader(jr.Code)
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 239
diff changeset
189 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
190 var err error
741
8bb2e48e2dfd Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 739
diff changeset
191 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
192 _, 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
193 } else {
b800eb2a0846 JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 536
diff changeset
194 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
195 }
b800eb2a0846 JSON handler: if result is an io.Reader copyit through.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 536
diff changeset
196 if err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4829
diff changeset
197 log.Errorf("%v\n", err)
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 239
diff changeset
198 }
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 }
979
7934b5c1a910 Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 741
diff changeset
201
1693
cdc8933949f2 Controllers: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1685
diff changeset
202 // 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
203 // 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
204 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
205 rw.Header().Set("Content-Type", "application/json")
4829
f4ec3558460e Set some nosniff http headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
206 rw.Header().Set("X-Content-Type-Options", "nosniff")
979
7934b5c1a910 Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 741
diff changeset
207 rw.WriteHeader(code)
7934b5c1a910 Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 741
diff changeset
208 if err := json.NewEncoder(rw).Encode(data); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4829
diff changeset
209 log.Errorf("%v\n", err)
979
7934b5c1a910 Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 741
diff changeset
210 }
7934b5c1a910 Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 741
diff changeset
211 }