Author: Paolo Lulli <paolo@lulli.net>
Add config to format/unformat output
server/config/config.go | 2 ++ server/rest/outputs.go | 10 ++++++---- 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/config/config.go b/server/config/config.go index 4e63b02f697cb7ec37185035e20483cf2ebb9d8a..ff7454aed3812e2931fd27fceadbc669020ec8c0 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -50,6 +50,8 @@ TlsCA string `json:"tlsCA"` LogFile string `json:"logFile"` GrafanaActive string `json:"grafanaActive"` + + FormatOutput string `json:"formatOutput"` } func GetConfig(fileName string) Configuration { diff --git a/server/rest/outputs.go b/server/rest/outputs.go index b6d65c80a97d1acc2af3980f0fb17798204704fe..b3afb8330ed63c16fd564e6c8097acc172638604 100644 --- a/server/rest/outputs.go +++ b/server/rest/outputs.go @@ -1,10 +1,12 @@ package rest -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "yats-server/config" +) -func JsonPrint(c *gin.Context, status int, h map[string]any) { - formatted := false - if formatted { +func JsonPrint(cfg config.Configuration, c *gin.Context, status int, h map[string]any) { + if "true" == cfg.FormatOutput { c.IndentedJSON(status, h) } else { c.JSON(status, h) diff --git a/server/rest/rest-event.go b/server/rest/rest-event.go index f136b2e75c2021d32b149bb71d013e46addde67d..bd7e0049d2d4d7036df328809b547450daa83ef4 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 { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) } - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK"}) + JsonPrint(cfg, 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 { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-2"}) + JsonPrint(cfg, 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) } - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) + JsonPrint(cfg, 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 { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) + JsonPrint(cfg, c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } } diff --git a/server/rest/rest-metric.go b/server/rest/rest-metric.go index 29f0ec21c4686da12651eca032f2b138008fdc1b..2acfa37e3cb70c50910b68ab5ec3b71a55efeb0a 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 { - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "-1"}) + JsonPrint(cfg, c, http.StatusAccepted, gin.H{"ret": "-1"}) return } @@ -61,7 +61,7 @@ } else { db.SaveMetric(clientCN, metric.Name, metric.Value) } - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK"}) + JsonPrint(cfg, 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 { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-2"}) + JsonPrint(cfg, 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) } - JsonPrint(c, http.StatusAccepted, gin.H{"data": metricsPack}) + JsonPrint(cfg, 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 { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) - JsonPrint(c, http.StatusAccepted, gin.H{"data": metricsPack}) + JsonPrint(cfg, c, http.StatusAccepted, gin.H{"data": metricsPack}) } } diff --git a/server/rest/rest-permissions.go b/server/rest/rest-permissions.go index 0899f6b61df55eedea2c8d8ee9191de31e1f33dd..b51d3bc191cebe60914b0945b5fc0b28a0640b63 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) - JsonPrint(c, http.StatusAccepted, gin.H{"metrics": availableMetrics, "externalSources": externalSources}) + JsonPrint(cfg, c, http.StatusAccepted, gin.H{"metrics": availableMetrics, "externalSources": externalSources}) } } diff --git a/server/rest/rest-position.go b/server/rest/rest-position.go index fb855174c848194e43f5994c9a3cd82d75e5ccf4..2bf257294c8cefa43486905f7ba15c16720a48b9 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 { - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) } - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK"}) + JsonPrint(cfg, 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 { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) } - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) + JsonPrint(cfg, 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 { - JsonPrint(c, http.StatusBadRequest, gin.H{"ret": "-1"}) + JsonPrint(cfg, 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) - JsonPrint(c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) + JsonPrint(cfg, c, http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } }