annotate pkg/models/extservices.go @ 5490:5f47eeea988d logging

Use own logging package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Sep 2021 17:45:39 +0200
parents cabf4789e02b
children 1222b777f51f
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: 501
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: 501
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: 501
diff changeset
3 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
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: 501
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: 501
diff changeset
6 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
diff changeset
7 // Copyright (C) 2018 by via donau
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
diff changeset
9 // Software engineering by Intevation GmbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
diff changeset
10 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
diff changeset
11 // Author(s):
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
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: 501
diff changeset
13
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 440
diff changeset
14 package models
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
501
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
17 "context"
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
446
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
19 "sort"
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "sync"
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "gemma.intevation.de/gemma/pkg/auth"
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1327
diff changeset
23 "gemma.intevation.de/gemma/pkg/log"
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 )
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 440
diff changeset
26 type ExtEntry struct {
473
b2dea4e56ff1 /api/published (GET) returns a JSON document of the geo-services published by the gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
27 Name string `json:"name"`
b2dea4e56ff1 /api/published (GET) returns a JSON document of the geo-services published by the gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
28 URL string `json:"url"`
b2dea4e56ff1 /api/published (GET) returns a JSON document of the geo-services published by the gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
29 WFS bool `json:"wfs"`
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 }
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 440
diff changeset
32 type ExtServices struct {
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 mu sync.Mutex
446
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
34 entries []ExtEntry
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 440
diff changeset
37 var ExternalServices = &ExtServices{}
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 const selectExternalServices = `SELECT local_name, remote_url, is_wfs
448
25dd96101aeb Fixed ordering in external proxied services.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 446
diff changeset
40 FROM sys_admin.external_services ORDER BY local_name`
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 440
diff changeset
42 func (es *ExtServices) Find(name string) (string, bool) {
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 es.mu.Lock()
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 defer es.mu.Unlock()
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 if es.entries == nil {
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 if err := es.load(); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1327
diff changeset
48 log.Errorf("%v\n", err)
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 return "", false
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 }
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 }
446
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
52 n := sort.Search(len(es.entries), func(i int) bool {
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
53 return es.entries[i].Name >= name
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
54 })
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
55 if n == len(es.entries) || es.entries[n].Name != name {
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
56 return "", false
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
57 }
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
58 return es.entries[n].URL, true
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 440
diff changeset
61 func (es *ExtServices) load() error {
446
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
62 // make empty slice to prevent retry if slice is empty.
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
63 es.entries = []ExtEntry{}
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
64 return auth.RunAs(context.Background(), "sys_admin",
501
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
65 func(conn *sql.Conn) error {
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
66 rows, err := conn.QueryContext(
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
67 context.Background(), selectExternalServices)
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
68 if err != nil {
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 return err
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
501
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
71 defer rows.Close()
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
72 for rows.Next() {
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
73 var entry ExtEntry
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
74 if err := rows.Scan(
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
75 &entry.Name,
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
76 &entry.URL,
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
77 &entry.WFS,
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
78 ); err != nil {
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
79 return err
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
80 }
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
81 es.entries = append(es.entries, entry)
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
82 }
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
83 return rows.Err()
c10c76c92797 Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 473
diff changeset
84 })
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 440
diff changeset
87 func (es *ExtServices) Invalidate() {
439
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 es.mu.Lock()
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 es.entries = nil
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 es.mu.Unlock()
1504f8eff12e Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 }
446
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
92
473
b2dea4e56ff1 /api/published (GET) returns a JSON document of the geo-services published by the gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
93 func ExternalAll(ExtEntry) bool { return true }
b2dea4e56ff1 /api/published (GET) returns a JSON document of the geo-services published by the gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
94 func ExternalWMS(entry ExtEntry) bool { return !entry.WFS }
b2dea4e56ff1 /api/published (GET) returns a JSON document of the geo-services published by the gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
95 func ExternalWFS(entry ExtEntry) bool { return entry.WFS }
446
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
96
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
97 func (es *ExtServices) Filter(accept func(ExtEntry) bool) []ExtEntry {
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
98 es.mu.Lock()
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
99 defer es.mu.Unlock()
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
100 if es.entries == nil {
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
101 if err := es.load(); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1327
diff changeset
102 log.Errorf("%v\n", err)
446
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
103 return nil
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
104 }
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
105 }
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
106 ee := make([]ExtEntry, 0, len(es.entries))
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
107 for _, e := range es.entries {
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
108 if accept(e) {
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
109 ee = append(ee, e)
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
110 }
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
111 }
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
112 return ee
659c04feb2dc Made use of sorted slice in external proxied services symmetric to published services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
113 }