Mercurial > gemma
annotate pkg/models/extservices.go @ 823:d7ae7338872d
client: add morphtool again
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Thu, 27 Sep 2018 19:26:38 +0200 |
parents | c10c76c92797 |
children | a244b18cb916 |
rev | line source |
---|---|
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
|
1 package models |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import ( |
501
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
4 "context" |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 "database/sql" |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 "log" |
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
|
7 "sort" |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 "sync" |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 "gemma.intevation.de/gemma/pkg/auth" |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 ) |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 |
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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 |
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
|
19 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
|
20 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
|
21 entries []ExtEntry |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
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
|
24 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
|
25 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 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
|
27 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
|
28 |
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
|
29 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
|
30 es.mu.Lock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 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
|
32 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 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
|
34 if err := es.load(); err != nil { |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 log.Printf("error: %v\n", err) |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 return "", false |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 } |
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
|
39 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
|
40 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
|
41 }) |
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
|
42 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
|
43 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
|
44 } |
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
|
45 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
|
46 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 |
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
|
48 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
|
49 // 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
|
50 es.entries = []ExtEntry{} |
501
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
51 return auth.RunAs("sys_admin", context.Background(), |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
52 func(conn *sql.Conn) error { |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
53 rows, err := conn.QueryContext( |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
54 context.Background(), selectExternalServices) |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
55 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
|
56 return err |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 } |
501
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
58 defer rows.Close() |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
59 for rows.Next() { |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
60 var entry ExtEntry |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
61 if err := rows.Scan( |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
62 &entry.Name, |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
63 &entry.URL, |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
64 &entry.WFS, |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
65 ); err != nil { |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
66 return err |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
67 } |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
68 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
|
69 } |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
70 return rows.Err() |
c10c76c92797
Use metamorphic database connections for auth.RunAs().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
473
diff
changeset
|
71 }) |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 |
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
|
74 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
|
75 es.mu.Lock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 es.entries = nil |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 es.mu.Unlock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 } |
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
|
79 |
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
|
80 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
|
81 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
|
82 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
|
83 |
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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 if err := es.load(); err != 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
|
89 log.Printf("error: %v\n", err) |
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
|
90 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
|
91 } |
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 } |
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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 } |
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 } |
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 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
|
100 } |