Code cleanup; Implement persistant storage; Use maps for storage

This commit is contained in:
Pijus Kamandulis
2024-02-25 22:13:04 +02:00
parent 1c5e5ce85d
commit 48660b5f63
39 changed files with 1420 additions and 1408 deletions

View File

@@ -14,12 +14,13 @@ var Config = ServerConfig{}
func ParseFlags() {
host := flag.String("Host", "localhost", "Hostname")
port := flag.Int("Port", 8081, "Listen port")
explorerPath := flag.String("ExplorerDir", "/home/pk/pro/cosmos-explorer/dist", "Path to cosmos-explorer files")
tlsCertificatePath := flag.String("Cert", "../example.crt", "Hostname")
tlsCertificateKey := flag.String("CertKey", "../example.key", "Hostname")
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
tlsCertificatePath := flag.String("Cert", "", "Hostname")
tlsCertificateKey := flag.String("CertKey", "", "Hostname")
initialDataPath := flag.String("InitialData", "", "Path to JSON containing initial state")
accountKey := flag.String("AccountKey", DefaultAccountKey, "Account key for authentication")
disableAuthentication := flag.Bool("DisableAuth", false, "Disable authentication")
persistDataPath := flag.String("Persist", "", "Saves data to given path on application exit")
flag.Parse()
@@ -28,7 +29,8 @@ func ParseFlags() {
Config.ExplorerPath = *explorerPath
Config.TLS_CertificatePath = *tlsCertificatePath
Config.TLS_CertificateKey = *tlsCertificateKey
Config.DataFilePath = *initialDataPath
Config.InitialDataFilePath = *initialDataPath
Config.PersistDataFilePath = *persistDataPath
Config.DisableAuth = *disableAuthentication
Config.DatabaseAccount = Config.Host

View File

@@ -11,6 +11,7 @@ type ServerConfig struct {
Host string
TLS_CertificatePath string
TLS_CertificateKey string
DataFilePath string
InitialDataFilePath string
PersistDataFilePath string
DisableAuth bool
}

View File

@@ -65,14 +65,14 @@ func CreateDatabase(c *gin.Context) {
return
}
status := repositories.CreateDatabase(newDatabase)
createdDatabase, status := repositories.CreateDatabase(newDatabase)
if status == repositorymodels.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
return
}
if status == repositorymodels.StatusOk {
c.IndentedJSON(http.StatusCreated, newDatabase)
c.IndentedJSON(http.StatusCreated, createdDatabase)
return
}

View File

@@ -78,14 +78,14 @@ func ReplaceDocument(c *gin.Context) {
return
}
status = repositories.CreateDocument(databaseId, collectionId, requestBody)
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
if status == repositorymodels.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
return
}
if status == repositorymodels.StatusOk {
c.IndentedJSON(http.StatusCreated, requestBody)
c.IndentedJSON(http.StatusCreated, createdDocument)
return
}
@@ -114,7 +114,6 @@ func DocumentsPost(c *gin.Context) {
queryParameters = parametersToMap(paramsArray)
}
// TODO: Handle these {"query":"select c.id, c._self, c._rid, c._ts, [c[\"pk\"]] as _partitionKeyValue from c"}
docs, status := repositories.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
if status != repositorymodels.StatusOk {
// TODO: Currently we return everything if the query fails
@@ -131,14 +130,14 @@ func DocumentsPost(c *gin.Context) {
return
}
status := repositories.CreateDocument(databaseId, collectionId, requestBody)
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
if status == repositorymodels.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
return
}
if status == repositorymodels.StatusOk {
c.IndentedJSON(http.StatusCreated, requestBody)
c.IndentedJSON(http.StatusCreated, createdDocument)
return
}

View File

@@ -24,6 +24,9 @@ func RegisterExplorerHandlers(router *gin.Engine) {
ctx.Next()
}
})
explorer.Static("/", config.Config.ExplorerPath)
if config.Config.ExplorerPath != "" {
explorer.Static("/", config.Config.ExplorerPath)
}
}
}

View File

@@ -25,6 +25,7 @@ func Test_Collections(t *testing.T) {
)
assert.Nil(t, err)
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
databaseClient, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err)

View File

@@ -9,6 +9,7 @@ import (
func runTestServer() *httptest.Server {
config.Config.AccountKey = config.DefaultAccountKey
config.Config.ExplorerPath = "/tmp/nothing"
return httptest.NewServer(api.CreateRouter())
}

View File

@@ -50,6 +50,7 @@ func testCosmosQuery(t *testing.T,
}
func Test_Documents(t *testing.T) {
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
ID: testCollectionName,
PartitionKey: struct {
@@ -77,7 +78,7 @@ func Test_Documents(t *testing.T) {
t.Run("Should query document", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
"SELECT c.id, c[\"pk\"] FROM c",
"SELECT c.id, c[\"pk\"] FROM c ORDER BY c.id",
nil,
[]interface{}{
map[string]interface{}{"id": "12345", "pk": "123"},
@@ -88,7 +89,7 @@ func Test_Documents(t *testing.T) {
t.Run("Should query VALUE array", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
"SELECT VALUE [c.id, c[\"pk\"]] FROM c",
"SELECT VALUE [c.id, c[\"pk\"]] FROM c ORDER BY c.id",
nil,
[]interface{}{
[]interface{}{"12345", "123"},
@@ -99,7 +100,7 @@ func Test_Documents(t *testing.T) {
t.Run("Should query VALUE object", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
"SELECT VALUE { id: c.id, _pk: c.pk } FROM c",
"SELECT VALUE { id: c.id, _pk: c.pk } FROM c ORDER BY c.id",
nil,
[]interface{}{
map[string]interface{}{"id": "12345", "_pk": "123"},
@@ -112,7 +113,8 @@ func Test_Documents(t *testing.T) {
testCosmosQuery(t, collectionClient,
`select c.id
FROM c
WHERE c.isCool=true`,
WHERE c.isCool=true
ORDER BY c.id`,
nil,
[]interface{}{
map[string]interface{}{"id": "67890"},
@@ -124,7 +126,8 @@ func Test_Documents(t *testing.T) {
testCosmosQuery(t, collectionClient,
`select c.id
FROM c
WHERE c.id=@param_id`,
WHERE c.id=@param_id
ORDER BY c.id`,
[]azcosmos.QueryParameter{
{Name: "@param_id", Value: "67890"},
},