Mercurial > gemma
comparison 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 |
comparison
equal
deleted
inserted
replaced
441:76a76691a298 | 442:fc37e7072022 |
---|---|
1 package models | |
2 | |
3 import ( | |
4 "database/sql" | |
5 "log" | |
6 "sync" | |
7 | |
8 "gemma.intevation.de/gemma/pkg/auth" | |
9 ) | |
10 | |
11 type ExtEntry struct { | |
12 url string | |
13 isWFS bool | |
14 } | |
15 | |
16 type ExtServices struct { | |
17 mu sync.Mutex | |
18 entries map[string]ExtEntry | |
19 } | |
20 | |
21 var ExternalServices = &ExtServices{} | |
22 | |
23 const selectExternalServices = `SELECT local_name, remote_url, is_wfs | |
24 FROM sys_admin.external_services` | |
25 | |
26 func (es *ExtServices) Find(name string) (string, bool) { | |
27 es.mu.Lock() | |
28 defer es.mu.Unlock() | |
29 | |
30 if es.entries == nil { | |
31 es.entries = make(map[string]ExtEntry) | |
32 if err := es.load(); err != nil { | |
33 log.Printf("error: %v\n", err) | |
34 return "", false | |
35 } | |
36 } | |
37 e, found := es.entries[name] | |
38 return e.url, found | |
39 } | |
40 | |
41 func (es *ExtServices) load() error { | |
42 return auth.RunAs("sys_admin", func(db *sql.DB) error { | |
43 rows, err := db.Query(selectExternalServices) | |
44 if err != nil { | |
45 return err | |
46 } | |
47 defer rows.Close() | |
48 for rows.Next() { | |
49 var entry ExtEntry | |
50 var key string | |
51 if err := rows.Scan(&key, &entry.url, &entry.isWFS); err != nil { | |
52 return err | |
53 } | |
54 es.entries[key] = entry | |
55 } | |
56 return rows.Err() | |
57 }) | |
58 } | |
59 | |
60 func (es *ExtServices) Invalidate() { | |
61 es.mu.Lock() | |
62 es.entries = nil | |
63 es.mu.Unlock() | |
64 } |