Author: Paolo Lulli <paolo@lulli.net>
Read metric via REST ok
db/connection.go | 19 +++++++++++++++++++ db/metric.go | 9 ++++----- rest.go | 45 --------------------------------------------- rest/rest-metric.go | 33 +++++++++++++++++++++++++++++++++ script/postClient.sh | 7 +++++++ service-rest.go | 26 ++++++++++++++++++++++++++ yats.go | 14 +++-----------
diff --git a/db/connection.go b/db/connection.go new file mode 100644 index 0000000000000000000000000000000000000000..3f893759a6e90cb79af748dda85d74898810a881 --- /dev/null +++ b/db/connection.go @@ -0,0 +1,19 @@ +package db + +import ( + "yats/config" + + "github.com/gocql/gocql" +) + +var Session *gocql.Session + +func InitializeDb(configuration config.Configuration) *gocql.Session { + cluster := gocql.NewCluster(configuration.DB_HOST) + cluster.Authenticator = gocql.PasswordAuthenticator{Username: configuration.DB_USERNAME, Password: configuration.DB_PASSWORD} + cluster.Keyspace = configuration.DB_NAME + + Session, _ := cluster.CreateSession() + + return Session +} diff --git a/db/metric.go b/db/metric.go index 1eaa0d36685b5ab2026678ccfb19854b6cf4e554..f98d3be487ce2a8a27448799032452649a41d12f 100644 --- a/db/metric.go +++ b/db/metric.go @@ -17,7 +17,6 @@ func PrintMetrics(metrics []Metric) { for _, metric := range metrics { fmt.Printf("%s\n", metric.Name) - } } @@ -38,15 +37,15 @@ } return metrics } -func SaveMetric(session *gocql.Session, idClient string, metricName string, metricValue string) { +func SaveMetric(idClient string, metricName string, metricValue string) { q := fmt.Sprintf("insert into metric ( id_client, mtime, name, value) values ('%s',toTimestamp(now()),'%s','%s');", idClient, metricName, metricValue) - session.Query(q).Exec() + Session.Query(q).Exec() } -func SaveMetricAt(session *gocql.Session, idClient string, tstamp time.Time, metricName string, metricValue string) { +func SaveMetricAt(idClient string, tstamp time.Time, metricName string, metricValue string) { timeAsBytes, _ := time.Now().UTC().MarshalText() q := fmt.Sprintf("insert into metric ( id_client, mtime, name, value) values ('%s','%s','%s','%s');", idClient, string(timeAsBytes), metricName, metricValue) - session.Query(q).Exec() + Session.Query(q).Exec() } diff --git a/rest/rest-metric.go b/rest/rest-metric.go new file mode 100644 index 0000000000000000000000000000000000000000..b7807c707d07bfd97948c242303382047e302b7e --- /dev/null +++ b/rest/rest-metric.go @@ -0,0 +1,33 @@ +package rest + +import ( + "fmt" + "net/http" + "yats/db" + + "github.com/gin-gonic/gin" +) + +func WriteMetric(c *gin.Context) { + idClient := c.Param("idClient") + metricName := c.Param("metricName") + metricValue := c.Param("metricValue") + + fmt.Printf("%s / %s / %s", idClient, metricName, metricValue) + db.SaveMetric(idClient, metricName, metricValue) + + c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"}) +} + +func WriteMetricAt(c *gin.Context) { + var metric db.Metric + + if err := c.BindJSON(&metric); err != nil { + return + } + + fmt.Printf("%s / %s / %s", metric.ID_client, metric.Name, metric.Value) + db.SaveMetric(metric.ID_client, metric.Name, metric.Value) + + c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"}) +} diff --git a/rest.go b/rest.go deleted file mode 100644 index dae7679fc5af27f3a84c7ab740d5a009f71e1eca..0000000000000000000000000000000000000000 --- a/rest.go +++ /dev/null @@ -1,45 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - "yats/config" - "yats/db" - - "github.com/gin-gonic/gin" - "github.com/gocql/gocql" -) - -var session *gocql.Session - -func RestService(c config.Configuration, sess *gocql.Session) { - - if sess == nil { - fmt.Println("sess is NULL") - } - session = sess - address := c.REST_ADDRESS - - router := gin.Default() - router.Use(CorsHeaders()) - - router.SetTrustedProxies([]string{"127.0.0.1"}) - gin.SetMode(gin.ReleaseMode) - - router.GET("/metric/:idClient/:metricName/:metricValue", writeMetric) - - router.Run(address) - //router.RunTLS(address, c.CertFile, c.KeyFile) -} - -func writeMetric(c *gin.Context) { - idClient := c.Param("idClient") - metricName := c.Param("metricName") - metricValue := c.Param("metricValue") - - fmt.Printf("%s / %s / %s", idClient, metricName, metricValue) - db.SaveMetric(session, idClient, metricName, metricValue) - - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"}) - -} diff --git a/script/postClient.sh b/script/postClient.sh new file mode 100755 index 0000000000000000000000000000000000000000..bcb719676bf263ad973a2f1dbdd612dbe7d9625d --- /dev/null +++ b/script/postClient.sh @@ -0,0 +1,7 @@ +#! /bin/bash +# +curl \ + -X POST\ + --header "Content-Type: application/json"\ + -d '{"id_client":"cli1","name":"mone","value":"vone"}'\ + http://127.0.0.1:18081/at diff --git a/service-rest.go b/service-rest.go new file mode 100644 index 0000000000000000000000000000000000000000..8dd269760c3642b54a4413af6083c4733a085f3a --- /dev/null +++ b/service-rest.go @@ -0,0 +1,26 @@ +package main + +import ( + "yats/config" + "yats/rest" + + "github.com/gin-gonic/gin" +) + +func RestService(c config.Configuration) { + + // session = DB + address := c.REST_ADDRESS + + router := gin.Default() + router.Use(CorsHeaders()) + + router.SetTrustedProxies([]string{"127.0.0.1"}) + gin.SetMode(gin.ReleaseMode) + + router.GET("/metric/:idClient/:metricName/:metricValue", rest.WriteMetric) + router.POST("/at", rest.WriteMetricAt) + + router.Run(address) + //router.RunTLS(address, c.CertFile, c.KeyFile) +} diff --git a/yats.go b/yats.go index d9659421e9bab29504f4d06b9e35f2aa9043550e..f944170fddf70a04b6feefeef53fa56eca5c70b1 100644 --- a/yats.go +++ b/yats.go @@ -3,21 +3,13 @@ import ( "os" "yats/config" - - "github.com/gocql/gocql" + "yats/db" ) func main() { configuration := config.GetConfig(os.Getenv("HOME") + "/.yats.json") - cluster := gocql.NewCluster(configuration.DB_HOST) - cluster.Authenticator = gocql.PasswordAuthenticator{Username: configuration.DB_USERNAME, Password: configuration.DB_PASSWORD} - cluster.Keyspace = configuration.DB_NAME - - session, _ := cluster.CreateSession() - - defer session.Close() - + db.Session = db.InitializeDb(configuration) //idClient := "ClientZero" /* @@ -31,6 +23,6 @@ var metrics = db.LoadMetrics(session) db.PrintMetrics(metrics) */ - RestService(configuration, session) + RestService(configuration) }