Mercurial > gemma
annotate pkg/models/extservices.go @ 473:b2dea4e56ff1
/api/published (GET) returns a JSON document of the geo-services published by the gemma server.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 23 Aug 2018 13:24:26 +0200 |
parents | 25dd96101aeb |
children | c10c76c92797 |
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 ( |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 "database/sql" |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 "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
|
6 "sort" |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 "sync" |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 "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
|
10 ) |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 |
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
|
12 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
|
13 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
|
14 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
|
15 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
|
16 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 |
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
|
18 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
|
19 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
|
20 entries []ExtEntry |
439
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 |
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
|
23 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
|
24 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 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
|
26 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
|
27 |
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
|
28 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
|
29 es.mu.Lock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 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
|
31 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 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
|
33 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
|
34 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
|
35 return "", false |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 } |
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
|
38 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
|
39 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
|
40 }) |
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 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
|
42 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
|
43 } |
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 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
|
45 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 |
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
|
47 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
|
48 // 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
|
49 es.entries = []ExtEntry{} |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 return auth.RunAs("sys_admin", func(db *sql.DB) error { |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 rows, err := db.Query(selectExternalServices) |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 if err != nil { |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 return err |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 defer rows.Close() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 for rows.Next() { |
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
|
57 var entry ExtEntry |
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
|
58 if err := rows.Scan( |
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
|
59 &entry.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
|
60 &entry.URL, |
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
|
61 &entry.WFS, |
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 ); err != nil { |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 return err |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 } |
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
|
65 es.entries = append(es.entries, entry) |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 return rows.Err() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 }) |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 |
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
|
71 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
|
72 es.mu.Lock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 es.entries = nil |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 es.mu.Unlock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 } |
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
|
76 |
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
|
77 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
|
78 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
|
79 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
|
80 |
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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 } |
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 } |
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 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
|
91 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
|
92 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
|
93 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
|
94 } |
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 } |
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 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
|
97 } |