Mercurial > gemma
annotate pkg/models/extservices.go @ 442:fc37e7072022
Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 21 Aug 2018 16:57:55 +0200 |
parents | pkg/controllers/extservices.go@3739aa34f838 |
children | 659c04feb2dc |
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" |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 "sync" |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 "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
|
9 ) |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
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
|
11 type ExtEntry struct { |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 url string |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 isWFS bool |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
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
|
16 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
|
17 mu sync.Mutex |
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 entries map[string]ExtEntry |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 |
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
|
21 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
|
22 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 const selectExternalServices = `SELECT local_name, remote_url, is_wfs |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 FROM sys_admin.external_services` |
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 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
|
27 es.mu.Lock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 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
|
29 |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 if es.entries == nil { |
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
|
31 es.entries = make(map[string]ExtEntry) |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 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
|
33 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
|
34 return "", false |
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 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 e, found := es.entries[name] |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 return e.url, found |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 |
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
|
41 func (es *ExtServices) load() error { |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 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
|
43 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
|
44 if err != nil { |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 return err |
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 defer rows.Close() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 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
|
49 var entry ExtEntry |
439
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 var key string |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 if err := rows.Scan(&key, &entry.url, &entry.isWFS); err != nil { |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 return err |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 es.entries[key] = entry |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 return rows.Err() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 }) |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 } |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 |
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
|
60 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
|
61 es.mu.Lock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 es.entries = nil |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 es.mu.Unlock() |
1504f8eff12e
Load the configuration of the proxied external services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 } |