diff pkg/geoserver/boot.go @ 913:876d1f5433be geo-style

Started with direct applying style after modification via controller. WIP.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 02 Oct 2018 23:29:55 +0200
parents 0a563fef64a9
children 4bf3a3a20ce1
line wrap: on
line diff
--- a/pkg/geoserver/boot.go	Tue Oct 02 22:25:44 2018 +0200
+++ b/pkg/geoserver/boot.go	Tue Oct 02 23:29:55 2018 +0200
@@ -318,9 +318,24 @@
 	return err
 }
 
-func ensureStyles() error {
-	log.Println("info: creating styles")
+type styles struct {
+	Styles struct {
+		Style []struct {
+			Name string `json:"name"`
+		} `json:"style"`
+	} `json:"styles"`
+}
 
+func (s *styles) hasStyle(name string) bool {
+	for i := range s.Styles.Style {
+		if s.Styles.Style[i].Name == name {
+			return true
+		}
+	}
+	return false
+}
+
+func (s *styles) load() error {
 	var (
 		geoURL   = config.GeoServerURL()
 		user     = config.GeoServerUser()
@@ -328,25 +343,6 @@
 		auth     = basicAuth(user, password)
 	)
 
-	type styles struct {
-		Styles struct {
-			Style []struct {
-				Name string `json:"name"`
-			} `json:"style"`
-		} `json:"styles"`
-	}
-
-	var stls styles
-
-	hasStyle := func(name string) bool {
-		for i := range stls.Styles.Style {
-			if stls.Styles.Style[i].Name == name {
-				return true
-			}
-		}
-		return false
-	}
-
 	req, err := http.NewRequest(
 		http.MethodGet,
 		geoURL+"/rest/workspaces/"+workspaceName+"/styles.json",
@@ -359,18 +355,36 @@
 	if err != nil {
 		return err
 	}
+	defer resp.Body.Close()
 
 	// Fetch all styles
-	if err := json.NewDecoder(resp.Body).Decode(&stls); err != nil {
+	if err := json.NewDecoder(resp.Body).Decode(s); err != nil {
 		// XXX: Same quirk as with featuretypes.
 	}
-	resp.Body.Close()
+	return nil
+}
+
+func ensureStyles() error {
+	log.Println("info: creating styles")
+
+	var (
+		geoURL   = config.GeoServerURL()
+		user     = config.GeoServerUser()
+		password = config.GeoServerPassword()
+		auth     = basicAuth(user, password)
+	)
+
+	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 {
-		if hasStyle(entries[i].Name) {
+		if stls.hasStyle(entries[i].Name) {
 			log.Printf("already has style for %s\n", entries[i].Name)
 			continue
 		}
@@ -515,3 +529,31 @@
 
 	return nil
 }
+
+func ReconfigureStyle(name string) {
+	Reconfigure(func() error {
+		var stls styles
+		if err := stls.load(); err != nil {
+			return err
+		}
+
+		entries := models.InternalServices.Filter(
+			models.IntAnd(
+				models.IntWMS,
+				models.IntWithStyle,
+				models.IntByName(name)))
+
+		for i := range entries {
+			entry := &entries[i]
+			// TODO: Implement me!
+
+			if stls.hasStyle(entry.Name) {
+				// Update
+			} else {
+				// Create new style
+			}
+		}
+
+		return nil
+	})
+}