annotate pkg/models/pubservices.go @ 469:788c87b99bae

465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 22 Aug 2018 18:25:51 +0200
parents a8e217119085
children b2dea4e56ff1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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: 441
diff changeset
1 package models
441
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"
456
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
9 "gemma.intevation.de/gemma/pkg/config"
441
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
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: 441
diff changeset
12 type PubEntry struct {
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
13 Name string
444
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
14 Style sql.NullString
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
15 WMS bool
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
16 WFS bool
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
17 }
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
18
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: 441
diff changeset
19 type PubServices struct {
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
20 entries []PubEntry
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 mu sync.Mutex
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 }
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 const selectPublishedServices = `SELECT name, style, as_wms, as_wfs
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
25 FROM sys_admin.published_services ORDER by name`
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
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: 441
diff changeset
27 var PublishedServices = &PubServices{}
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
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: 441
diff changeset
29 func (ps *PubServices) Find(name string) (string, bool) {
469
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
30 ps.mu.Lock()
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
31 defer ps.mu.Unlock()
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
32
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
33 if ps.entries == nil {
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
34 if err := ps.load(); err != nil {
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
35 log.Printf("error: %v\n", err)
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
36 return "", false
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
37 }
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
38 }
788c87b99bae 465:a8e217119085 removed the mutex and the lazy loading from the internal publishing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 456
diff changeset
39
456
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
40 if ps.has(name) {
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
41 return config.GeoServerURL() + "/" + name, true
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
42 }
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
43 return "", false
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
44 }
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
456
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
46 func (ps *PubServices) has(service string) bool {
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
47 var check func(*PubEntry) bool
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
48 switch service {
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
49 case "wms":
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
50 check = func(e *PubEntry) bool { return e.WMS }
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
51 case "wfs":
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
52 check = func(e *PubEntry) bool { return e.WFS }
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
53 default:
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
54 return false
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
55 }
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
56 for i := range ps.entries {
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
57 if check(&ps.entries[i]) {
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
58 return true
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 }
456
a8e217119085 Fixed internal proxy resolution.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 446
diff changeset
61 return false
441
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
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: 441
diff changeset
64 func (ps *PubServices) 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: 445
diff changeset
65 // 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: 445
diff changeset
66 ps.entries = []PubEntry{}
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 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
68 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
69 if err != nil {
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 return err
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 }
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 defer rows.Close()
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 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: 441
diff changeset
74 var entry PubEntry
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 if err := rows.Scan(
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
76 &entry.Name, &entry.Style,
444
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
77 &entry.WFS, &entry.WFS,
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 ); err != nil {
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 return err
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 }
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
81 ps.entries = append(ps.entries, entry)
441
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 }
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 return rows.Err()
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 })
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 return nil
76a76691a298 Load the configuration of the published services from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 }
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: 441
diff changeset
87
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: 441
diff changeset
88 func (ps *PubServices) Invalidate() {
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: 441
diff changeset
89 ps.mu.Lock()
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: 441
diff changeset
90 ps.entries = nil
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: 441
diff changeset
91 ps.mu.Unlock()
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: 441
diff changeset
92 }
444
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
93
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
94 func PublishedWMS(entry PubEntry) bool { return entry.WMS }
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
95 func PublishedWFS(entry PubEntry) bool { return entry.WFS }
444
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
96
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
97 func (ps *PubServices) Filter(accept func(PubEntry) bool) []PubEntry {
444
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
98 ps.mu.Lock()
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
99 defer ps.mu.Unlock()
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
100 if ps.entries == nil {
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
101 if err := ps.load(); err != nil {
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
102 log.Printf("error: %v\n", err)
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
103 return nil
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
104 }
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
105 }
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
106 pe := make([]PubEntry, 0, len(ps.entries))
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
107 for _, e := range ps.entries {
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
108 if accept(e) {
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
109 pe = append(pe, e)
444
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
110 }
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
111 }
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
112
445
37742dd72fdb Use a sorted slice for published layers to get rid of extra sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 444
diff changeset
113 return pe
444
c315885825e5 Publish WFS layers on GeoServer from database configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
114 }