From 332be181efdf02858d17ff092348470e8e0fd1db Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Fri, 23 Feb 2024 00:27:12 +0200 Subject: [PATCH] Implement state export endpoint --- api/handlers/cosmium.go | 12 ++++++++++++ api/handlers/middleware/authentication.go | 4 +++- api/router.go | 2 ++ internal/repositories/state.go | 8 ++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 api/handlers/cosmium.go diff --git a/api/handlers/cosmium.go b/api/handlers/cosmium.go new file mode 100644 index 0000000..0844652 --- /dev/null +++ b/api/handlers/cosmium.go @@ -0,0 +1,12 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/pikami/cosmium/internal/repositories" +) + +func CosmiumExport(c *gin.Context) { + c.IndentedJSON(http.StatusOK, repositories.GetState()) +} diff --git a/api/handlers/middleware/authentication.go b/api/handlers/middleware/authentication.go index ad4f26b..56aa4b5 100644 --- a/api/handlers/middleware/authentication.go +++ b/api/handlers/middleware/authentication.go @@ -13,7 +13,9 @@ import ( func Authentication() gin.HandlerFunc { return func(c *gin.Context) { requestUrl := c.Request.URL.String() - if config.Config.DisableAuth || strings.HasPrefix(requestUrl, "/_explorer") { + if config.Config.DisableAuth || + strings.HasPrefix(requestUrl, "/_explorer") || + strings.HasPrefix(requestUrl, "/cosmium") { return } diff --git a/api/router.go b/api/router.go index ea26953..9cfc3a3 100644 --- a/api/router.go +++ b/api/router.go @@ -37,6 +37,8 @@ func CreateRouter() *gin.Engine { router.GET("/offers", handlers.GetOffers) router.GET("/", handlers.GetServerInfo) + router.GET("/cosmium/export", handlers.CosmiumExport) + handlers.RegisterExplorerHandlers(router) return router diff --git a/internal/repositories/state.go b/internal/repositories/state.go index 80345c5..f73e00a 100644 --- a/internal/repositories/state.go +++ b/internal/repositories/state.go @@ -29,3 +29,11 @@ func LoadStateFS(filePath string) { collections = state.Collections documents = state.Documents } + +func GetState() map[string]interface{} { + return map[string]interface{}{ + "databases": databases, + "collections": collections, + "documents": documents, + } +}