Mercurial > gemma
annotate pkg/controllers/pubservices.go @ 441:76a76691a298
Load the configuration of the published services from database.
TODO: Use this to do the publishing.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 21 Aug 2018 14:59:36 +0200 |
parents | |
children |
rev | line source |
---|---|
441
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package controllers |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import ( |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 "database/sql" |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 "log" |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 "sync" |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 "gemma.intevation.de/gemma/pkg/auth" |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 ) |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 type pubEntry struct { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 style sql.NullString |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 asWMS bool |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 asWFS bool |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 type pubServices struct { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 entries map[string]pubEntry |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 mu sync.Mutex |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 const selectPublishedServices = `SELECT name, style, as_wms, as_wfs |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 FROM sys_admin.published_services` |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 var publishedServices = &pubServices{} |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 func (ps *pubServices) find(name string) (string, bool) { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 ps.mu.Lock() |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 defer ps.mu.Unlock() |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 if ps.entries == nil { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 ps.entries = make(map[string]pubEntry) |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 if err := ps.load(); err != nil { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 log.Printf("error: %v\n", err) |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 return "", false |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 _, found := ps.entries[name] |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 if !found { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 name = "" |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 return name, found |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 func (ps *pubServices) load() error { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 return auth.RunAs("sys_admin", func(db *sql.DB) error { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 rows, err := db.Query(selectPublishedServices) |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 if err != nil { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 return err |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 defer rows.Close() |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 for rows.Next() { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 var entry pubEntry |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 var key string |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 if err := rows.Scan( |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 &key, &entry.style, |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 &entry.asWFS, &entry.asWFS, |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 ); err != nil { |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 return err |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 ps.entries[key] = entry |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 } |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 return rows.Err() |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 }) |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 return nil |
76a76691a298
Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 } |