Mercurial > gemma
comparison pkg/models/intservices.go @ 474:11d80120ed3d
Renamed published services to internal services to be more symmetrical to external services.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 23 Aug 2018 13:32:16 +0200 |
parents | pkg/models/pubservices.go@b2dea4e56ff1 |
children | c10c76c92797 |
comparison
equal
deleted
inserted
replaced
473:b2dea4e56ff1 | 474:11d80120ed3d |
---|---|
1 package models | |
2 | |
3 import ( | |
4 "database/sql" | |
5 "log" | |
6 "sync" | |
7 | |
8 "gemma.intevation.de/gemma/pkg/auth" | |
9 "gemma.intevation.de/gemma/pkg/config" | |
10 ) | |
11 | |
12 type IntEntry struct { | |
13 Name string `json:"name"` | |
14 Style sql.NullString `json:"-"` // This should be done separately. | |
15 WMS bool `json:"wms"` | |
16 WFS bool `json:"wfs"` | |
17 } | |
18 | |
19 type IntServices struct { | |
20 entries []IntEntry | |
21 mu sync.Mutex | |
22 } | |
23 | |
24 const selectPublishedServices = `SELECT name, style, as_wms, as_wfs | |
25 FROM sys_admin.published_services ORDER by name` | |
26 | |
27 var InternalServices = &IntServices{} | |
28 | |
29 func (ps *IntServices) Find(name string) (string, bool) { | |
30 ps.mu.Lock() | |
31 defer ps.mu.Unlock() | |
32 | |
33 if ps.entries == nil { | |
34 if err := ps.load(); err != nil { | |
35 log.Printf("error: %v\n", err) | |
36 return "", false | |
37 } | |
38 } | |
39 | |
40 if ps.has(name) { | |
41 return config.GeoServerURL() + "/" + name, true | |
42 } | |
43 return "", false | |
44 } | |
45 | |
46 func (ps *IntServices) has(service string) bool { | |
47 var check func(*IntEntry) bool | |
48 switch service { | |
49 case "wms": | |
50 check = func(e *IntEntry) bool { return e.WMS } | |
51 case "wfs": | |
52 check = func(e *IntEntry) bool { return e.WFS } | |
53 default: | |
54 return false | |
55 } | |
56 for i := range ps.entries { | |
57 if check(&ps.entries[i]) { | |
58 return true | |
59 } | |
60 } | |
61 return false | |
62 } | |
63 | |
64 func (ps *IntServices) load() error { | |
65 // make empty slice to prevent retry if slice is empty. | |
66 ps.entries = []IntEntry{} | |
67 return auth.RunAs("sys_admin", func(db *sql.DB) error { | |
68 rows, err := db.Query(selectPublishedServices) | |
69 if err != nil { | |
70 return err | |
71 } | |
72 defer rows.Close() | |
73 for rows.Next() { | |
74 var entry IntEntry | |
75 if err := rows.Scan( | |
76 &entry.Name, &entry.Style, | |
77 &entry.WFS, &entry.WFS, | |
78 ); err != nil { | |
79 return err | |
80 } | |
81 ps.entries = append(ps.entries, entry) | |
82 } | |
83 return rows.Err() | |
84 }) | |
85 return nil | |
86 } | |
87 | |
88 func (ps *IntServices) Invalidate() { | |
89 ps.mu.Lock() | |
90 ps.entries = nil | |
91 ps.mu.Unlock() | |
92 } | |
93 | |
94 func InternalAll(IntEntry) bool { return true } | |
95 func IntWMS(entry IntEntry) bool { return entry.WMS } | |
96 func IntWFS(entry IntEntry) bool { return entry.WFS } | |
97 | |
98 func (ps *IntServices) Filter(accept func(IntEntry) bool) []IntEntry { | |
99 ps.mu.Lock() | |
100 defer ps.mu.Unlock() | |
101 if ps.entries == nil { | |
102 if err := ps.load(); err != nil { | |
103 log.Printf("error: %v\n", err) | |
104 return nil | |
105 } | |
106 } | |
107 pe := make([]IntEntry, 0, len(ps.entries)) | |
108 for _, e := range ps.entries { | |
109 if accept(e) { | |
110 pe = append(pe, e) | |
111 } | |
112 } | |
113 | |
114 return pe | |
115 } |