changeset 915:2ebf677fc2e1 geo-style

Load style data only on demand to not waste menory.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 03 Oct 2018 13:08:23 +0200
parents 4bf3a3a20ce1
children 9e210b00ace9
files pkg/geoserver/boot.go pkg/models/intservices.go
diffstat 2 files changed, 41 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/geoserver/boot.go	Wed Oct 03 02:04:20 2018 +0200
+++ b/pkg/geoserver/boot.go	Wed Oct 03 13:08:23 2018 +0200
@@ -366,6 +366,14 @@
 
 func updateStyle(entry *models.IntEntry, create bool) error {
 
+	log.Printf("creating style %s\n", entry.Name)
+
+	// Try to load the style data.
+	data, err := entry.LoadStyle()
+	if err != nil {
+		return err
+	}
+
 	var (
 		geoURL   = config.GeoServerURL()
 		user     = config.GeoServerUser()
@@ -373,8 +381,6 @@
 		auth     = basicAuth(user, password)
 	)
 
-	log.Printf("creating style %s\n", entry.Name)
-
 	styleURL := geoURL + "/rest/workspaces/" + workspaceName +
 		"/styles"
 
@@ -422,12 +428,12 @@
 	req, err := http.NewRequest(
 		http.MethodPut,
 		styleURL+"/"+url.PathEscape(entry.Name),
-		strings.NewReader(entry.Style.String))
+		strings.NewReader(data))
 	if err != nil {
 		return err
 	}
 	auth(req)
-	if isSymbologyEncoding(entry.Style.String) {
+	if isSymbologyEncoding(data) {
 		asContentType(req, "application/vnd.ogc.se+xml")
 	} else {
 		asContentType(req, "application/vnd.ogc.sld+xml")
--- a/pkg/models/intservices.go	Wed Oct 03 02:04:20 2018 +0200
+++ b/pkg/models/intservices.go	Wed Oct 03 13:08:23 2018 +0200
@@ -12,10 +12,10 @@
 )
 
 type IntEntry struct {
-	Name  string         `json:"name"`
-	Style sql.NullString `json:"-"` // This should be done separately.
-	WMS   bool           `json:"wms"`
-	WFS   bool           `json:"wfs"`
+	Name  string `json:"name"`
+	Style bool   `json:"style"`
+	WMS   bool   `json:"wms"`
+	WFS   bool   `json:"wfs"`
 }
 
 type IntServices struct {
@@ -24,8 +24,16 @@
 }
 
 const (
-	selectPublishedServices = `SELECT relname, style, as_wms, as_wfs
-FROM sys_admin.published_services JOIN pg_class ON name = oid ORDER by relname`
+	selectServicesSQL = `
+SELECT relname, style IS NOT NULL, as_wms, as_wfs
+FROM sys_admin.published_services
+JOIN pg_class ON name = oid ORDER by relname`
+
+	selectStyleSQL = `
+SELECT style IS NOT NULL
+FROM sys_admin.published_services
+JOIN pg_class ON name = oid
+WHERE relname = $1`
 
 	updateStyleSQL = `
 UPDATE sys_admin.published_services
@@ -35,6 +43,18 @@
 
 var InternalServices = &IntServices{}
 
+func (e *IntEntry) LoadStyle() (string, error) {
+	var style string
+	err := auth.RunAs("sys_admin", context.Background(),
+		func(conn *sql.Conn) error {
+			return conn.QueryRowContext(
+				context.Background(),
+				selectStyleSQL,
+				e.Name).Scan(&style)
+		})
+	return style, err
+}
+
 func UpdateInternalStyle(req *http.Request, name, style string) error {
 	return auth.RunAsSessionUser(req, func(conn *sql.Conn) error {
 		_, err := conn.ExecContext(
@@ -88,7 +108,7 @@
 	return auth.RunAs("sys_admin", context.Background(),
 		func(conn *sql.Conn) error {
 			rows, err := conn.QueryContext(
-				context.Background(), selectPublishedServices)
+				context.Background(), selectServicesSQL)
 			if err != nil {
 				return err
 			}
@@ -113,9 +133,10 @@
 	ps.mu.Unlock()
 }
 
-func InternalAll(IntEntry) bool  { return true }
-func IntWMS(entry IntEntry) bool { return entry.WMS }
-func IntWFS(entry IntEntry) bool { return entry.WFS }
+func InternalAll(IntEntry) bool        { return true }
+func IntWMS(entry IntEntry) bool       { return entry.WMS }
+func IntWFS(entry IntEntry) bool       { return entry.WFS }
+func IntWithStyle(entry IntEntry) bool { return entry.Style }
 
 func IntByName(name string) func(IntEntry) bool {
 	return func(entry IntEntry) bool { return entry.Name == name }
@@ -132,10 +153,6 @@
 	}
 }
 
-func IntWithStyle(entry IntEntry) bool {
-	return entry.Style.Valid
-}
-
 func (ps *IntServices) Filter(accept func(IntEntry) bool) []IntEntry {
 	ps.mu.Lock()
 	defer ps.mu.Unlock()