changeset 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 3739aa34f838
children fc37e7072022
files pkg/controllers/proxy.go pkg/controllers/pubservices.go pkg/controllers/routes.go
diffstat 3 files changed, 67 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/proxy.go	Tue Aug 21 14:45:38 2018 +0200
+++ b/pkg/controllers/proxy.go	Tue Aug 21 14:59:36 2018 +0200
@@ -35,29 +35,6 @@
 	"http://schemas.opengis.net/gml":            struct{}{},
 }
 
-func findProxy(key string) func(string) (string, bool) {
-	entries := config.Proxies(key)
-	return func(entry string) (string, bool) {
-		if entries == nil || len(entries) == 0 {
-			return "", false
-		}
-		alias, found := entries[entry]
-		if !found {
-			return "", false
-		}
-		data, ok := alias.(map[string]interface{})
-		if !ok {
-			return "", false
-		}
-		urlS, found := data["url"]
-		if !found {
-			return "", false
-		}
-		url, ok := urlS.(string)
-		return url, ok
-	}
-}
-
 func proxyDirector(lookup func(string) (string, bool)) func(*http.Request) {
 
 	return func(req *http.Request) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/pubservices.go	Tue Aug 21 14:59:36 2018 +0200
@@ -0,0 +1,66 @@
+package controllers
+
+import (
+	"database/sql"
+	"log"
+	"sync"
+
+	"gemma.intevation.de/gemma/pkg/auth"
+)
+
+type pubEntry struct {
+	style sql.NullString
+	asWMS bool
+	asWFS bool
+}
+
+type pubServices struct {
+	entries map[string]pubEntry
+	mu      sync.Mutex
+}
+
+const selectPublishedServices = `SELECT name, style, as_wms, as_wfs
+FROM sys_admin.published_services`
+
+var publishedServices = &pubServices{}
+
+func (ps *pubServices) find(name string) (string, bool) {
+	ps.mu.Lock()
+	defer ps.mu.Unlock()
+
+	if ps.entries == nil {
+		ps.entries = make(map[string]pubEntry)
+		if err := ps.load(); err != nil {
+			log.Printf("error: %v\n", err)
+			return "", false
+		}
+	}
+	_, found := ps.entries[name]
+	if !found {
+		name = ""
+	}
+	return name, found
+}
+
+func (ps *pubServices) load() error {
+	return auth.RunAs("sys_admin", func(db *sql.DB) error {
+		rows, err := db.Query(selectPublishedServices)
+		if err != nil {
+			return err
+		}
+		defer rows.Close()
+		for rows.Next() {
+			var entry pubEntry
+			var key string
+			if err := rows.Scan(
+				&key, &entry.style,
+				&entry.asWFS, &entry.asWFS,
+			); err != nil {
+				return err
+			}
+			ps.entries[key] = entry
+		}
+		return rows.Err()
+	})
+	return nil
+}
--- a/pkg/controllers/routes.go	Tue Aug 21 14:45:38 2018 +0200
+++ b/pkg/controllers/routes.go	Tue Aug 21 14:59:36 2018 +0200
@@ -70,7 +70,7 @@
 
 	// Internal proxies.
 	internal := &httputil.ReverseProxy{
-		Director:       proxyDirector(findProxy("internal")),
+		Director:       proxyDirector(publishedServices.find),
 		ModifyResponse: proxyModifyResponse("/api/internal/"),
 	}