mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 08:50:46 +00:00
Databases CRUD
This commit is contained in:
7
api/config/config.go
Normal file
7
api/config/config.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
var Config = ServerConfig{
|
||||
DatabaseAccount: "localhost",
|
||||
DatabaseDomain: "localhost",
|
||||
DatabaseEndpoint: "https://localhost:8081/",
|
||||
}
|
||||
7
api/config/models.go
Normal file
7
api/config/models.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
type ServerConfig struct {
|
||||
DatabaseAccount string
|
||||
DatabaseDomain string
|
||||
DatabaseEndpoint string
|
||||
}
|
||||
79
api/handlers/databases.go
Normal file
79
api/handlers/databases.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func GetAllDatabases(c *gin.Context) {
|
||||
databases, status := repositories.GetAllDatabases()
|
||||
if status == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Databases": databases})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func GetDatabase(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
database, status := repositories.GetDatabase(id)
|
||||
if status == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, database)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositories.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DeleteDatabase(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
status := repositories.DeleteDatabase(id)
|
||||
if status == repositories.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositories.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func CreateDatabase(c *gin.Context) {
|
||||
var newDatabase repositories.Database
|
||||
|
||||
if err := c.BindJSON(&newDatabase); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if newDatabase.ID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"})
|
||||
return
|
||||
}
|
||||
|
||||
status := repositories.CreateDatabase(newDatabase)
|
||||
if status == repositories.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusCreated, newDatabase)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
42
api/handlers/server_info.go
Normal file
42
api/handlers/server_info.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func GetServerInfo(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_self": "",
|
||||
"id": config.Config.DatabaseAccount,
|
||||
"_rid": fmt.Sprintf("%s.%s", config.Config.DatabaseAccount, config.Config.DatabaseDomain),
|
||||
"media": "//media/",
|
||||
"addresses": "//addresses/",
|
||||
"_dbs": "//dbs/",
|
||||
"writableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"readableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"enableMultipleWriteLocations": false,
|
||||
"userReplicationPolicy": map[string]interface{}{
|
||||
"asyncReplication": false,
|
||||
"minReplicaSetSize": 1,
|
||||
"maxReplicasetSize": 4,
|
||||
},
|
||||
"userConsistencyPolicy": map[string]interface{}{"defaultConsistencyLevel": "Session"},
|
||||
"systemReplicationPolicy": map[string]interface{}{"minReplicaSetSize": 1, "maxReplicasetSize": 4},
|
||||
"readPolicy": map[string]interface{}{"primaryReadCoefficient": 1, "secondaryReadCoefficient": 1},
|
||||
"queryEngineConfiguration": "{\"allowNewKeywords\":true,\"maxJoinsPerSqlQuery\":10,\"maxQueryRequestTimeoutFraction\":0.9,\"maxSqlQueryInputLength\":524288,\"maxUdfRefPerSqlQuery\":10,\"queryMaxInMemorySortDocumentCount\":-1000,\"spatialMaxGeometryPointCount\":256,\"sqlAllowNonFiniteNumbers\":false,\"sqlDisableOptimizationFlags\":0,\"enableSpatialIndexing\":true,\"maxInExpressionItemsCount\":2147483647,\"maxLogicalAndPerSqlQuery\":2147483647,\"maxLogicalOrPerSqlQuery\":2147483647,\"maxSpatialQueryCells\":2147483647,\"sqlAllowAggregateFunctions\":true,\"sqlAllowGroupByClause\":true,\"sqlAllowLike\":true,\"sqlAllowSubQuery\":true,\"sqlAllowScalarSubQuery\":true,\"sqlAllowTop\":true}",
|
||||
})
|
||||
}
|
||||
18
api/router.go
Normal file
18
api/router.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/handlers"
|
||||
)
|
||||
|
||||
func CreateRouter() *gin.Engine {
|
||||
router := gin.Default()
|
||||
|
||||
router.GET("/dbs/:id", handlers.GetDatabase)
|
||||
router.DELETE("/dbs/:id", handlers.DeleteDatabase)
|
||||
router.GET("/dbs", handlers.GetAllDatabases)
|
||||
router.POST("/dbs", handlers.CreateDatabase)
|
||||
router.GET("/", handlers.GetServerInfo)
|
||||
|
||||
return router
|
||||
}
|
||||
Reference in New Issue
Block a user