mirror of
https://github.com/pikami/cosmium.git
synced 2026-04-24 15:28:54 +01:00
Compare commits
3 Commits
a6b5d32ff7
...
v0.1.9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3584f9b5ce | ||
|
|
c7d01b4593 | ||
|
|
2834f3f641 |
@@ -9,11 +9,6 @@ builds:
|
|||||||
- arm64
|
- arm64
|
||||||
env:
|
env:
|
||||||
- CGO_ENABLED=0
|
- CGO_ENABLED=0
|
||||||
ignore:
|
|
||||||
- goos: linux
|
|
||||||
goarch: arm64
|
|
||||||
- goos: windows
|
|
||||||
goarch: arm64
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
prerelease: auto
|
prerelease: auto
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
|
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
|
||||||
EnvPrefix = "COSMIUM_"
|
EnvPrefix = "COSMIUM_"
|
||||||
|
ExplorerBaseUrlLocation = "/_explorer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Config = ServerConfig{}
|
var Config = ServerConfig{}
|
||||||
@@ -45,6 +46,7 @@ func ParseFlags() {
|
|||||||
Config.DatabaseDomain = Config.Host
|
Config.DatabaseDomain = Config.Host
|
||||||
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port)
|
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port)
|
||||||
Config.AccountKey = *accountKey
|
Config.AccountKey = *accountKey
|
||||||
|
Config.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFlagsFromEnvironment() (err error) {
|
func setFlagsFromEnvironment() (err error) {
|
||||||
|
|||||||
@@ -16,4 +16,5 @@ type ServerConfig struct {
|
|||||||
DisableAuth bool
|
DisableAuth bool
|
||||||
DisableTls bool
|
DisableTls bool
|
||||||
Debug bool
|
Debug bool
|
||||||
|
ExplorerBaseUrlLocation string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
jsonpatch "github.com/evanphx/json-patch/v5"
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -223,6 +224,11 @@ func DocumentsPost(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert"))
|
||||||
|
if isUpsert {
|
||||||
|
repositories.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
|
||||||
|
}
|
||||||
|
|
||||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
||||||
if status == repositorymodels.Conflict {
|
if status == repositorymodels.Conflict {
|
||||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RegisterExplorerHandlers(router *gin.Engine) {
|
func RegisterExplorerHandlers(router *gin.Engine) {
|
||||||
explorer := router.Group("/_explorer")
|
explorer := router.Group(config.Config.ExplorerBaseUrlLocation)
|
||||||
{
|
{
|
||||||
explorer.Use(func(ctx *gin.Context) {
|
explorer.Use(func(ctx *gin.Context) {
|
||||||
if ctx.Param("filepath") == "/config.json" {
|
if ctx.Param("filepath") == "/config.json" {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func Authentication() gin.HandlerFunc {
|
|||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
requestUrl := c.Request.URL.String()
|
requestUrl := c.Request.URL.String()
|
||||||
if config.Config.DisableAuth ||
|
if config.Config.DisableAuth ||
|
||||||
strings.HasPrefix(requestUrl, "/_explorer") ||
|
strings.HasPrefix(requestUrl, config.Config.ExplorerBaseUrlLocation) ||
|
||||||
strings.HasPrefix(requestUrl, "/cosmium") {
|
strings.HasPrefix(requestUrl, "/cosmium") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/api/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StripTrailingSlashes(r *gin.Engine) gin.HandlerFunc {
|
func StripTrailingSlashes(r *gin.Engine) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
if len(path) > 1 && path[len(path)-1] == '/' {
|
if len(path) > 1 && path[len(path)-1] == '/' && !strings.Contains(path, config.Config.ExplorerBaseUrlLocation) {
|
||||||
c.Request.URL.Path = path[:len(path)-1]
|
c.Request.URL.Path = path[:len(path)-1]
|
||||||
r.HandleContext(c)
|
r.HandleContext(c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
func runTestServer() *httptest.Server {
|
func runTestServer() *httptest.Server {
|
||||||
config.Config.AccountKey = config.DefaultAccountKey
|
config.Config.AccountKey = config.DefaultAccountKey
|
||||||
config.Config.ExplorerPath = "/tmp/nothing"
|
config.Config.ExplorerPath = "/tmp/nothing"
|
||||||
|
config.Config.ExplorerBaseUrlLocation = config.ExplorerBaseUrlLocation
|
||||||
|
|
||||||
return httptest.NewServer(api.CreateRouter())
|
return httptest.NewServer(api.CreateRouter())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -220,4 +220,92 @@ func Test_Documents_Patch(t *testing.T) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("CreateItem", func(t *testing.T) {
|
||||||
|
context := context.TODO()
|
||||||
|
|
||||||
|
item := map[string]interface{}{
|
||||||
|
"Id": "6789011",
|
||||||
|
"pk": "456",
|
||||||
|
"newField": "newValue2",
|
||||||
|
}
|
||||||
|
bytes, err := json.Marshal(item)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
r, err2 := collectionClient.CreateItem(
|
||||||
|
context,
|
||||||
|
azcosmos.PartitionKey{},
|
||||||
|
bytes,
|
||||||
|
&azcosmos.ItemOptions{
|
||||||
|
EnableContentResponseOnWrite: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert.NotNil(t, r)
|
||||||
|
assert.Nil(t, err2)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("CreateItem that already exists", func(t *testing.T) {
|
||||||
|
context := context.TODO()
|
||||||
|
|
||||||
|
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}}
|
||||||
|
bytes, err := json.Marshal(item)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
r, err := collectionClient.CreateItem(
|
||||||
|
context,
|
||||||
|
azcosmos.PartitionKey{},
|
||||||
|
bytes,
|
||||||
|
&azcosmos.ItemOptions{
|
||||||
|
EnableContentResponseOnWrite: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert.NotNil(t, r)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
|
var respErr *azcore.ResponseError
|
||||||
|
if errors.As(err, &respErr) {
|
||||||
|
assert.Equal(t, http.StatusConflict, respErr.StatusCode)
|
||||||
|
} else {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("UpsertItem new", func(t *testing.T) {
|
||||||
|
context := context.TODO()
|
||||||
|
|
||||||
|
item := map[string]interface{}{"id": "123456", "pk": "1234", "isCool": false, "arr": []int{1, 2, 3}}
|
||||||
|
bytes, err := json.Marshal(item)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
r, err2 := collectionClient.UpsertItem(
|
||||||
|
context,
|
||||||
|
azcosmos.PartitionKey{},
|
||||||
|
bytes,
|
||||||
|
&azcosmos.ItemOptions{
|
||||||
|
EnableContentResponseOnWrite: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert.NotNil(t, r)
|
||||||
|
assert.Nil(t, err2)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("UpsertItem that already exists", func(t *testing.T) {
|
||||||
|
context := context.TODO()
|
||||||
|
|
||||||
|
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3, 4}}
|
||||||
|
bytes, err := json.Marshal(item)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
r, err2 := collectionClient.UpsertItem(
|
||||||
|
context,
|
||||||
|
azcosmos.PartitionKey{},
|
||||||
|
bytes,
|
||||||
|
&azcosmos.ItemOptions{
|
||||||
|
EnableContentResponseOnWrite: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert.NotNil(t, r)
|
||||||
|
assert.Nil(t, err2)
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user