Mercurial > gemma
annotate pkg/controllers/system.go @ 3630:2467e619cf67 configuration
Fixed more SQL typos.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 11 Jun 2019 12:38:25 +0200 |
parents | 1825a1bc9fb1 |
children | 70a881045efd |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
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:
854
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:
854
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
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:
854
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:
854
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
diff
changeset
|
12 // * Sascha Wilde <sascha.wilde@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
854
diff
changeset
|
13 |
722
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
14 package controllers |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
15 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
16 import ( |
3104
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
17 "bytes" |
722
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
18 "database/sql" |
840
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
19 "fmt" |
722
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
20 "io/ioutil" |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
21 "net/http" |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
22 "strings" |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
23 |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
24 "github.com/gorilla/mux" |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
25 |
3104
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
26 "gemma.intevation.de/gemma/pkg/config" |
840
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
27 "gemma.intevation.de/gemma/pkg/models" |
722
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
28 ) |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
29 |
840
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
30 const ( |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
31 getFeatureColourSQL = `SELECT r,g,b,a |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
32 FROM systemconf.feature_colours |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
33 WHERE feature_name = $1 AND style_attr = $2` |
846
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
34 setFeatureColourSQL = `UPDATE systemconf.feature_colours |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
35 SET (r, g, b, a) = ($3, $4, $5, $6) |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
36 WHERE feature_name = $1 AND style_attr = $2` |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
37 |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
38 getSettingsSQL = ` |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
39 SELECT config_key, config_val |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
40 FROM sys_admin.system_config` |
840
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
41 |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
42 updateSettingSQL = ` |
3630
2467e619cf67
Fixed more SQL typos.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3629
diff
changeset
|
43 INSERT INTO sys_admin.system_config (config_key, config_val) |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
44 VALUES ($1, $2) |
3629
1825a1bc9fb1
Fixed typo in SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3628
diff
changeset
|
45 ON CONFLICT (config_key) DO UPDATE SET config_val = $2` |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
46 ) |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
47 |
840
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
48 // System status end points |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
49 |
722
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
50 func showSystemLog( |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
51 _ interface{}, req *http.Request, |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 _ *sql.Conn, |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 ) (jr JSONResult, err error) { |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 serviceName := mux.Vars(req)["service"] |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
56 fileName := mux.Vars(req)["file"] |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
57 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
58 // The following check is currently most likely unnecessary as I wasn't |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
59 // able to inject a verbatim '/' via the middleware, but better be on |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
60 // the safe site... |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
61 if strings.Contains(fileName, "/") { |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
62 err = JSONError{http.StatusBadRequest, |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
63 "error: no slashes allowed in file name"} |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
64 return |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
65 } |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
66 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
67 var path string |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
69 switch serviceName { |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
70 case "apache2", "postgresql": |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
71 path = "/var/log/" + serviceName + "/" + fileName |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
72 default: |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
73 err = JSONError{http.StatusBadRequest, |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
74 "error: invalid service: " + serviceName} |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
75 return |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
76 } |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
77 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
78 var txt []byte |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
79 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
80 if txt, err = ioutil.ReadFile(path); err != nil { |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
81 return |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
82 } |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
83 |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
84 jr = JSONResult{ |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
85 Result: struct { |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
86 Path string `json:"path"` |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
87 Content string `json:"content"` |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
88 }{path, string(txt)}, |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
89 } |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
90 return |
815f5e2ed974
Added simple endpoint to view system logs.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
91 } |
840
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
92 |
3104
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
93 func getSystemConfig( |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
94 _ interface{}, req *http.Request, |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
95 _ *sql.Conn, |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
96 ) (jr JSONResult, err error) { |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
97 |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
98 cfg := config.PublishedConfig() |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
99 if cfg == "" { |
3108
8a4fb02ee60a
Send empty JSON document when calling GET /api/system/config and "published-config" is not configure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3104
diff
changeset
|
100 jr = JSONResult{Result: strings.NewReader("{}")} |
3104
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
101 return |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
102 } |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
103 |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
104 var data []byte |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
105 if data, err = ioutil.ReadFile(cfg); err != nil { |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
106 return |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
107 } |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
108 |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
109 jr = JSONResult{Result: bytes.NewReader(data)} |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
110 return |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
111 } |
19fc84a98479
Added "published-config" field to configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
112 |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
113 func getSystemSettings( |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
114 _ interface{}, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
115 req *http.Request, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
116 conn *sql.Conn, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
117 ) (jr JSONResult, err error) { |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
118 |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
119 var rows *sql.Rows |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
120 if rows, err = conn.QueryContext(req.Context(), getSettingsSQL); err != nil { |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
121 return |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
122 } |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
123 defer rows.Close() |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
124 |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
125 settings := map[string]string{} |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
126 |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
127 for rows.Next() { |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
128 var key, val string |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
129 if err = rows.Scan(&key, &val); err != nil { |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
130 return |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
131 } |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
132 settings[key] = val |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
133 } |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
134 if err = rows.Err(); err != nil { |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
135 return |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
136 } |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
137 |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
138 jr = JSONResult{Result: settings} |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
139 return |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
140 } |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
141 |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
142 func setSystemSettings( |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
143 input interface{}, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
144 req *http.Request, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
145 conn *sql.Conn, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
146 ) (jr JSONResult, err error) { |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
147 |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
148 settings := input.(*map[string]string) |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
149 |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
150 ctx := req.Context() |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
151 var tx *sql.Tx |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
152 if tx, err = conn.BeginTx(ctx, nil); err != nil { |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
153 return |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
154 } |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
155 defer tx.Rollback() |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
156 |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
157 var setStmt *sql.Stmt |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
158 if setStmt, err = tx.PrepareContext(ctx, updateSettingSQL); err != nil { |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
159 return |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
160 } |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
161 defer setStmt.Close() |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
162 |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
163 for key, value := range *settings { |
3630
2467e619cf67
Fixed more SQL typos.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3629
diff
changeset
|
164 if _, err = setStmt.ExecContext(ctx, key, value); err != nil { |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
165 return |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
166 } |
3628
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
167 } |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
168 |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
169 if err = tx.Commit(); err != nil { |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
170 return |
6693be57b7a2
Re-worked the Go part a little bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3625
diff
changeset
|
171 } |
3625
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
172 |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
173 jr = JSONResult{ |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
174 Code: http.StatusCreated, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
175 Result: struct { |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
176 Result string `json:"result"` |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
177 }{"success"}, |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
178 } |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
179 return |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
180 } |
a688a478e35f
implemented configuration backend and frontend
Markus Kottlaender <markus@intevation.de>
parents:
3108
diff
changeset
|
181 |
840
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
182 // Map/Feature style end points |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
183 |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
184 func getFeatureStyle( |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
185 _ interface{}, |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
186 req *http.Request, |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
187 db *sql.Conn, |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
188 ) (jr JSONResult, err error) { |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
189 |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
190 feature := mux.Vars(req)["feature"] |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
191 attr := mux.Vars(req)["attr"] |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
192 |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
193 c := models.Colour{} |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
194 err = db.QueryRowContext( |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
195 req.Context(), |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
196 getFeatureColourSQL, |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
197 feature, attr, |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
198 ).Scan(&c.R, &c.G, &c.B, &c.A) |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
199 |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
200 switch { |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
201 case err == sql.ErrNoRows: |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
202 err = JSONError{ |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
203 Code: http.StatusNotFound, |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
204 Message: "Requestes style not found.", |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
205 } |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
206 return |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
207 case err != nil: |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
208 return |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
209 } |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
210 |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
211 jr.Result = &struct { |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
212 Colour models.Colour `json:"colour"` |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
213 Code string `json:"code"` |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
214 }{c, fmt.Sprintf("rgba(%d, %d, %d, %g)", c.R, c.G, c.B, c.A)} |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
215 return |
0f61bfc21041
Added end point to get style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
722
diff
changeset
|
216 } |
846
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
217 |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
218 func setFeatureStyle( |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
219 input interface{}, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
220 req *http.Request, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
221 db *sql.Conn, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
222 ) (jr JSONResult, err error) { |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
223 |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
224 feature := mux.Vars(req)["feature"] |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
225 attr := mux.Vars(req)["attr"] |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
226 |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
227 c := input.(*models.Colour) |
854
83c271cb2344
Reverted fcb38cedc680 (More tightened color model.)
Sascha Wilde <wilde@intevation.de>
parents:
850
diff
changeset
|
228 if !c.IsValid() { |
83c271cb2344
Reverted fcb38cedc680 (More tightened color model.)
Sascha Wilde <wilde@intevation.de>
parents:
850
diff
changeset
|
229 err = JSONError{http.StatusBadRequest, "error: invalid colours"} |
83c271cb2344
Reverted fcb38cedc680 (More tightened color model.)
Sascha Wilde <wilde@intevation.de>
parents:
850
diff
changeset
|
230 return |
83c271cb2344
Reverted fcb38cedc680 (More tightened color model.)
Sascha Wilde <wilde@intevation.de>
parents:
850
diff
changeset
|
231 } |
846
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
232 |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
233 var res sql.Result |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
234 res, err = db.ExecContext( |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
235 req.Context(), |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
236 setFeatureColourSQL, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
237 feature, attr, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
238 c.R, c.G, c.B, c.A) |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
239 |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
240 if err != nil { |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
241 return |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
242 } |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
243 |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
244 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 { |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
245 err = JSONError{ |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
246 Code: http.StatusNotFound, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
247 Message: "Requestes style not found.", |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
248 } |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
249 return |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
250 } |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
251 |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
252 jr = JSONResult{ |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
253 Code: http.StatusCreated, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
254 Result: struct { |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
255 Result string `json:"result"` |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
256 }{"success"}, |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
257 } |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
258 return |
6902032757e4
Added end point to set style (colour) information for feature.
Sascha Wilde <wilde@intevation.de>
parents:
840
diff
changeset
|
259 } |