changeset 5491:09346efa7f69 logging

Do ogr2ogr check only once when it's needed the first time.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Sep 2021 18:02:09 +0200
parents 5f47eeea988d
children b4f59aef3f9e
files pkg/wfs/global.go
diffstat 1 files changed, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/wfs/global.go	Mon Sep 20 17:45:39 2021 +0200
+++ b/pkg/wfs/global.go	Mon Sep 20 18:02:09 2021 +0200
@@ -21,6 +21,7 @@
 	"os"
 	"os/exec"
 	"path/filepath"
+	"sync"
 
 	"gemma.intevation.de/gemma/pkg/config"
 	"gemma.intevation.de/gemma/pkg/log"
@@ -53,17 +54,30 @@
 	GMLDownloader []string
 )
 
-// GetFeatures is the default Downloader in this Gemma server.
-var GetFeatures = setup()
+var (
+	getFeaturesOnce sync.Once
+	getFeatures     func(*Capabilities, string, string) (Downloader, error)
+)
 
-func setup() func(*Capabilities, string, string) (Downloader, error) {
+func getFeaturesOnceFunc() {
 	path, err := exec.LookPath("ogr2ogr")
 	if err != nil {
 		log.Infoln("ogr2ogr not installed. Using direct GeoJSON WFS download.")
-		return getFeaturesGeoJSON
+		getFeatures = getFeaturesGeoJSON
+	} else {
+		log.Infof("ogr2ogr found at %s. Using GML WFS download.\n", path)
+		getFeatures = getFeaturesGML
 	}
-	log.Infof("ogr2ogr found at %s. Using GML WFS download.\n", path)
-	return getFeaturesGML
+}
+
+// GetFeatures is the default Downloader in this Gemma server.
+func GetFeatures(
+	caps *Capabilities,
+	featureTypeName string,
+	sortBy string,
+) (Downloader, error) {
+	getFeaturesOnce.Do(getFeaturesOnceFunc)
+	return getFeatures(caps, featureTypeName, sortBy)
 }
 
 func getFeaturesGeoJSON(