Browse Source

:notebook: Allow to store arbitrary data in ledger

Ettore Di Giacinto 3 years ago
parent
commit
7e34057fe4
1 changed files with 33 additions and 0 deletions
  1. 33 0
      api/api.go

+ 33 - 0
api/api.go

@@ -1,9 +1,11 @@
 package api
 
 import (
+	"context"
 	"embed"
 	"io/fs"
 	"net/http"
+	"time"
 
 	"github.com/labstack/echo/v4"
 	"github.com/mudler/edgevpn/pkg/blockchain"
@@ -86,5 +88,36 @@ func API(l string, ledger *blockchain.Ledger) error {
 		return c.JSON(http.StatusOK, ledger.BlockChain())
 	})
 
+	ec.GET("/api/ledger/:bucket/:key", func(c echo.Context) error {
+		bucket := c.Param("bucket")
+		key := c.Param("key")
+		//		c.SetHandler()
+		return c.JSON(http.StatusOK, ledger.CurrentData()[bucket][key])
+	})
+
+	ec.GET("/api/ledger/:bucket", func(c echo.Context) error {
+		bucket := c.Param("bucket")
+		//		c.SetHandler()
+		return c.JSON(http.StatusOK, ledger.CurrentData()[bucket])
+	})
+
+	ec.PUT("/api/ledger/:bucket/:key/:value", func(c echo.Context) error {
+		bucket := c.Param("bucket")
+		key := c.Param("key")
+		value := c.Param("value")
+
+		put, cancel := context.WithCancel(context.Background())
+		ledger.Announce(put, 5*time.Second, func() {
+			v, exists := ledger.CurrentData()[bucket][key]
+			if !exists || string(v) != value {
+				ledger.Add(bucket, map[string]interface{}{key: value})
+			}
+			if exists && string(v) == value {
+				cancel()
+			}
+		})
+		//		c.SetHandler()
+		return c.JSON(http.StatusOK, "Announcing")
+	})
 	return ec.Start(l)
 }