changeset 914:4bf3a3a20ce1 geo-style

Finished direct updating when style is uploaded via controller. Still needs testing.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 03 Oct 2018 02:04:20 +0200
parents 876d1f5433be
children 2ebf677fc2e1
files pkg/controllers/geostyling.go pkg/geoserver/boot.go
diffstat 2 files changed, 88 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/geostyling.go	Tue Oct 02 23:29:55 2018 +0200
+++ b/pkg/controllers/geostyling.go	Wed Oct 03 02:04:20 2018 +0200
@@ -7,9 +7,10 @@
 	"log"
 	"net/http"
 
+	"github.com/gorilla/mux"
+
 	"gemma.intevation.de/gemma/pkg/geoserver"
 	"gemma.intevation.de/gemma/pkg/models"
-	"github.com/gorilla/mux"
 )
 
 const (
@@ -35,7 +36,9 @@
 
 func supportedWMSFeature(name string) bool {
 	return len(models.InternalServices.Filter(
-		models.IntAnd(models.IntWMS, models.IntByName(name)))) > 0
+		models.IntAnd(
+			models.IntWMS,
+			models.IntByName(name)))) > 0
 }
 
 func uploadStyle(rw http.ResponseWriter, req *http.Request) {
@@ -66,4 +69,7 @@
 	}
 
 	geoserver.ReconfigureStyle(feature)
+
+	// Nothing to return
+	rw.WriteHeader(http.StatusNoContent)
 }
--- a/pkg/geoserver/boot.go	Tue Oct 02 23:29:55 2018 +0200
+++ b/pkg/geoserver/boot.go	Wed Oct 03 02:04:20 2018 +0200
@@ -364,8 +364,7 @@
 	return nil
 }
 
-func ensureStyles() error {
-	log.Println("info: creating styles")
+func updateStyle(entry *models.IntEntry, create bool) error {
 
 	var (
 		geoURL   = config.GeoServerURL()
@@ -374,46 +373,32 @@
 		auth     = basicAuth(user, password)
 	)
 
-	var stls styles
+	log.Printf("creating style %s\n", entry.Name)
+
+	styleURL := geoURL + "/rest/workspaces/" + workspaceName +
+		"/styles"
 
-	if err := stls.load(); err != nil {
-		return err
+	// First create style
+
+	type Style struct {
+		Name     string `json:"name"`
+		Filename string `json:"filename"`
 	}
 
-	entries := models.InternalServices.Filter(
-		models.IntAnd(models.IntWMS, models.IntWithStyle))
-
-	for i := range entries {
-		if stls.hasStyle(entries[i].Name) {
-			log.Printf("already has style for %s\n", entries[i].Name)
-			continue
-		}
-
-		log.Printf("creating style %s\n", entries[i].Name)
-
-		styleURL := geoURL + "/rest/workspaces/" + workspaceName +
-			"/styles"
+	var styleFilename = struct {
+		Style Style `json:"style"`
+	}{
+		Style: Style{
+			Name:     entry.Name,
+			Filename: entry.Name + ".sld",
+		},
+	}
 
-		// First create style
-
-		type Style struct {
-			Name     string `json:"name"`
-			Filename string `json:"filename"`
-		}
-
-		var layer = struct {
-			Style Style `json:"style"`
-		}{
-			Style: Style{
-				Name:     entries[i].Name,
-				Filename: entries[i].Name + ".sld",
-			},
-		}
-
+	if create {
 		req, err := http.NewRequest(
 			http.MethodPost,
 			styleURL,
-			toStream(&layer))
+			toStream(&styleFilename))
 
 		if err != nil {
 			return err
@@ -427,43 +412,45 @@
 
 		if resp.StatusCode != http.StatusCreated {
 			return fmt.Errorf("Unable to create style %s (%s)",
-				entries[i].Name,
+				entry.Name,
 				http.StatusText(resp.StatusCode))
 		}
+	}
 
-		// Second upload data
+	// Second upload data
 
-		req, err = http.NewRequest(
-			http.MethodPut,
-			styleURL+"/"+url.PathEscape(entries[i].Name),
-			strings.NewReader(entries[i].Style.String))
-		if err != nil {
-			return err
-		}
-		auth(req)
-		if isSymbologyEncoding(entries[i].Style.String) {
-			asContentType(req, "application/vnd.ogc.se+xml")
-		} else {
-			asContentType(req, "application/vnd.ogc.sld+xml")
-		}
-		resp, err = http.DefaultClient.Do(req)
-		if err != nil {
-			return err
-		}
+	req, err := http.NewRequest(
+		http.MethodPut,
+		styleURL+"/"+url.PathEscape(entry.Name),
+		strings.NewReader(entry.Style.String))
+	if err != nil {
+		return err
+	}
+	auth(req)
+	if isSymbologyEncoding(entry.Style.String) {
+		asContentType(req, "application/vnd.ogc.se+xml")
+	} else {
+		asContentType(req, "application/vnd.ogc.sld+xml")
+	}
+	resp, err := http.DefaultClient.Do(req)
+	if err != nil {
+		return err
+	}
 
-		if resp.StatusCode != http.StatusOK {
-			return fmt.Errorf("cannot upload style %s (%s)",
-				entries[i].Name, http.StatusText(resp.StatusCode))
-		}
+	if resp.StatusCode != http.StatusOK {
+		return fmt.Errorf("cannot upload style %s (%s)",
+			entry.Name, http.StatusText(resp.StatusCode))
+	}
 
-		// Third associate with layer
+	// Third associate with layer
 
-		req, err = http.NewRequest(
+	if create {
+		req, err := http.NewRequest(
 			http.MethodPost,
 			geoURL+"/rest/layers/"+
-				url.PathEscape(workspaceName+":"+entries[i].Name)+
+				url.PathEscape(workspaceName+":"+entry.Name)+
 				"/styles?default=true",
-			toStream(&layer))
+			toStream(&styleFilename))
 		if err != nil {
 			return err
 		}
@@ -477,7 +464,7 @@
 
 		if resp.StatusCode != http.StatusCreated {
 			return fmt.Errorf("cannot connect style %s with layer (%s)",
-				entries[i].Name, http.StatusText(resp.StatusCode))
+				entry.Name, http.StatusText(resp.StatusCode))
 		}
 	}
 
@@ -503,6 +490,33 @@
 	}
 }
 
+func ensureStyles() error {
+	log.Println("info: creating styles")
+
+	var stls styles
+	if err := stls.load(); err != nil {
+		return err
+	}
+
+	entries := models.InternalServices.Filter(
+		models.IntAnd(
+			models.IntWMS,
+			models.IntWithStyle))
+
+	for i := range entries {
+		entry := &entries[i]
+		if stls.hasStyle(entry.Name) {
+			log.Printf("already has style for %s\n", entry.Name)
+			continue
+		}
+		if err := updateStyle(entry, true); err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 func PrepareGeoServer() error {
 
 	if config.DBUser() == "" {
@@ -545,12 +559,9 @@
 
 		for i := range entries {
 			entry := &entries[i]
-			// TODO: Implement me!
-
-			if stls.hasStyle(entry.Name) {
-				// Update
-			} else {
-				// Create new style
+			create := !stls.hasStyle(entry.Name)
+			if err := updateStyle(entry, create); err != nil {
+				return err
 			}
 		}