Implement state export endpoint

This commit is contained in:
Pijus Kamandulis 2024-02-23 00:27:12 +02:00
parent 16f41a5479
commit 332be181ef
4 changed files with 25 additions and 1 deletions

12
api/handlers/cosmium.go Normal file
View File

@ -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())
}

View File

@ -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
}

View File

@ -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

View File

@ -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,
}
}