yats.git

commit 3f53081b87c15553113f659da3b2a6b4bab157ba

Author: Paolo Lulli <paolo@lulli.net>

Formatted/unformatted json

 server/rest/outputs.go | 12 ++++++++++++
 server/rest/rest-event.go | 14 +++++++-------
 server/rest/rest-metric.go | 14 +++++++-------
 server/rest/rest-permissions.go | 2 +-
 server/rest/rest-position.go | 14 +++++++-------


diff --git a/server/rest/outputs.go b/server/rest/outputs.go
new file mode 100644
index 0000000000000000000000000000000000000000..b6d65c80a97d1acc2af3980f0fb17798204704fe
--- /dev/null
+++ b/server/rest/outputs.go
@@ -0,0 +1,12 @@
+package rest
+
+import "github.com/gin-gonic/gin"
+
+func JsonPrint(c *gin.Context, status int, h map[string]any) {
+	formatted := false
+	if formatted {
+		c.IndentedJSON(status, h)
+	} else {
+		c.JSON(status, h)
+	}
+}




diff --git a/server/rest/rest-event.go b/server/rest/rest-event.go
index f6c930f9c700110dc8583c9d2ab173c0c09b060d..f136b2e75c2021d32b149bb71d013e46addde67d 100644
--- a/server/rest/rest-event.go
+++ b/server/rest/rest-event.go
@@ -38,7 +38,7 @@ 	return func(c *gin.Context) {
 		var event model.EventRequest
 
 		if err := c.BindJSON(&event); err != nil {
-			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 			return
 		}
 
@@ -51,7 +51,7 @@ 		} else {
 			unixTimeUTC := time.Unix(event.Etime, 0)
 			db.SaveEventAt(clientCN, unixTimeUTC, event.Name)
 		}
-		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
+		JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK"})
 	}
 }
 
@@ -71,7 +71,7 @@ 	return func(c *gin.Context) {
 		var event model.EventSearchRequest
 
 		if err := c.BindJSON(&event); err != nil {
-			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 			return
 		}
 
@@ -80,7 +80,7 @@ 		fmt.Printf("Client ID: %s\n ", clientCN)
 
 		if event.SourceApplication != "" {
 			if !db.CanReadSourceEvent(clientCN, event.SourceApplication) {
-				c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-2"})
+				JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-2"})
 				return
 			}
 			clientCN = event.SourceApplication
@@ -92,7 +92,7 @@ 			eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(event.From), 100)
 		} else if event.From != 0 {
 			eventsPack = db.EventsBetween(db.Session, clientCN, util.Int64ToTimeUTC(event.From), util.Int64ToTimeUTC(event.To), 100)
 		}
-		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
+		JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
 	}
 }
 
@@ -114,7 +114,7 @@
 		fromParam := c.Param("from")
 		fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64)
 		if err != nil {
-			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 		}
 
 		clientCN := GetClientCN(c, cfg)
@@ -123,6 +123,6 @@
 		var eventsPack []model.EventModel
 		eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(fromParamInt64), 100)
 
-		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
+		JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
 	}
 }




diff --git a/server/rest/rest-metric.go b/server/rest/rest-metric.go
index 0f9634459c51677db3cb0e326eae79ceaeef5a36..29f0ec21c4686da12651eca032f2b138008fdc1b 100644
--- a/server/rest/rest-metric.go
+++ b/server/rest/rest-metric.go
@@ -47,7 +47,7 @@ 	return func(c *gin.Context) {
 		var metric model.MetricRequest
 
 		if err := c.BindJSON(&metric); err != nil {
-			c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusAccepted, gin.H{"ret": "-1"})
 			return
 		}
 
@@ -61,7 +61,7 @@ 		} else {
 			db.SaveMetric(clientCN, metric.Name, metric.Value)
 		}
 
-		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
+		JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK"})
 	}
 }
 
@@ -81,7 +81,7 @@ 	return func(c *gin.Context) {
 		var metric model.MetricSearchRequest
 
 		if err := c.BindJSON(&metric); err != nil {
-			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 			return
 		}
 
@@ -89,7 +89,7 @@ 		clientCN := GetClientCN(c, cfg)
 
 		if metric.SourceApplication != "" {
 			if !db.CanReadSourceMetric(clientCN, metric.SourceApplication, metric.Name) {
-				c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-2"})
+				JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-2"})
 				return
 			}
 			clientCN = metric.SourceApplication
@@ -104,7 +104,7 @@ 			metricsPack = db.MetricsFrom(db.Session, clientCN, metric.Name, util.Int64ToTimeUTC(metric.From), 100)
 		} else if metric.From != 0 {
 			metricsPack = db.MetricsBetween(db.Session, clientCN, metric.Name, util.Int64ToTimeUTC(metric.From), util.Int64ToTimeUTC(metric.To), 100)
 		}
-		c.IndentedJSON(http.StatusAccepted, gin.H{"data": metricsPack})
+		JsonPrint(c, http.StatusAccepted, gin.H{"data": metricsPack})
 	}
 }
 
@@ -128,7 +128,7 @@ 		nameParam := c.Param("name")
 		fromParam := c.Param("from")
 		fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64)
 		if err != nil {
-			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 			return
 		}
 
@@ -138,6 +138,6 @@
 		var metricsPack []model.MetricModel
 		metricsPack = db.MetricsFrom(db.Session, clientCN, nameParam, util.Int64ToTimeUTC(fromParamInt64), 100)
 
-		c.IndentedJSON(http.StatusAccepted, gin.H{"data": metricsPack})
+		JsonPrint(c, http.StatusAccepted, gin.H{"data": metricsPack})
 	}
 }




diff --git a/server/rest/rest-permissions.go b/server/rest/rest-permissions.go
index bae88af0c28b5932dffc0d83a79d6cbb4275dfa3..0899f6b61df55eedea2c8d8ee9191de31e1f33dd 100644
--- a/server/rest/rest-permissions.go
+++ b/server/rest/rest-permissions.go
@@ -34,6 +34,6 @@
 		availableMetrics := db.GetAvailableMetrics(db.Session, clientCN)
 		externalSources := db.GetExternalSources(db.Session, clientCN)
 
-		c.IndentedJSON(http.StatusAccepted, gin.H{"metrics": availableMetrics, "externalSources": externalSources})
+		JsonPrint(c, http.StatusAccepted, gin.H{"metrics": availableMetrics, "externalSources": externalSources})
 	}
 }




diff --git a/server/rest/rest-position.go b/server/rest/rest-position.go
index 6814bae186c1897de4e2d0ec13f9bf05c1ba4cb2..fb855174c848194e43f5994c9a3cd82d75e5ccf4 100644
--- a/server/rest/rest-position.go
+++ b/server/rest/rest-position.go
@@ -38,7 +38,7 @@ 	return func(c *gin.Context) {
 		var position model.PositionRequest
 
 		if err := c.BindJSON(&position); err != nil {
-			c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusAccepted, gin.H{"ret": "-1"})
 			return
 		}
 
@@ -51,7 +51,7 @@ 		} else {
 			unixTimeUTC := time.Unix(position.Ptime, 0)
 			db.SavePositionAt(clientCN, position.Lat, position.Lon, unixTimeUTC, position.Name)
 		}
-		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
+		JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK"})
 	}
 }
 
@@ -71,7 +71,7 @@ 	return func(c *gin.Context) {
 		var position model.PositionSearchRequest
 
 		if err := c.BindJSON(&position); err != nil {
-			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 			return
 		}
 
@@ -80,7 +80,7 @@ 		fmt.Printf("Client ID: %s\n ", clientCN)
 
 		if position.SourceApplication != "" {
 			if !db.CanReadSourcePosition(clientCN, position.SourceApplication) {
-				c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+				JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 				return
 			}
 			clientCN = position.SourceApplication
@@ -92,7 +92,7 @@ 			eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(position.From), 100)
 		} else if position.From != 0 {
 			eventsPack = db.EventsBetween(db.Session, clientCN, util.Int64ToTimeUTC(position.From), util.Int64ToTimeUTC(position.To), 100)
 		}
-		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
+		JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
 	}
 }
 
@@ -114,7 +114,7 @@
 		fromParam := c.Param("from")
 		fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64)
 		if err != nil {
-			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
+			JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"})
 		}
 
 		clientCN := GetClientCN(c, cfg)
@@ -123,6 +123,6 @@
 		var eventsPack []model.EventModel
 		eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(fromParamInt64), 100)
 
-		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
+		JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
 	}
 }