mirror of
https://github.com/pikami/cosmium.git
synced 2026-01-09 12:35:12 +00:00
Refactor to support multiple server instances in shared library
This commit is contained in:
35
api/api_server.go
Normal file
35
api/api_server.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
type ApiServer struct {
|
||||
stopServer chan interface{}
|
||||
isActive bool
|
||||
router *gin.Engine
|
||||
config config.ServerConfig
|
||||
}
|
||||
|
||||
func NewApiServer(dataRepository *repositories.DataRepository, config config.ServerConfig) *ApiServer {
|
||||
stopChan := make(chan interface{})
|
||||
|
||||
apiServer := &ApiServer{
|
||||
stopServer: stopChan,
|
||||
config: config,
|
||||
}
|
||||
|
||||
apiServer.CreateRouter(dataRepository)
|
||||
|
||||
return apiServer
|
||||
}
|
||||
|
||||
func (s *ApiServer) GetRouter() *gin.Engine {
|
||||
return s.router
|
||||
}
|
||||
|
||||
func (s *ApiServer) Stop() {
|
||||
s.stopServer <- true
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -13,9 +15,7 @@ const (
|
||||
ExplorerBaseUrlLocation = "/_explorer"
|
||||
)
|
||||
|
||||
var Config = ServerConfig{}
|
||||
|
||||
func ParseFlags() {
|
||||
func ParseFlags() ServerConfig {
|
||||
host := flag.String("Host", "localhost", "Hostname")
|
||||
port := flag.Int("Port", 8081, "Listen port")
|
||||
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
|
||||
@@ -31,22 +31,30 @@ func ParseFlags() {
|
||||
flag.Parse()
|
||||
setFlagsFromEnvironment()
|
||||
|
||||
Config.Host = *host
|
||||
Config.Port = *port
|
||||
Config.ExplorerPath = *explorerPath
|
||||
Config.TLS_CertificatePath = *tlsCertificatePath
|
||||
Config.TLS_CertificateKey = *tlsCertificateKey
|
||||
Config.InitialDataFilePath = *initialDataPath
|
||||
Config.PersistDataFilePath = *persistDataPath
|
||||
Config.DisableAuth = *disableAuthentication
|
||||
Config.DisableTls = *disableTls
|
||||
Config.Debug = *debug
|
||||
config := ServerConfig{}
|
||||
config.Host = *host
|
||||
config.Port = *port
|
||||
config.ExplorerPath = *explorerPath
|
||||
config.TLS_CertificatePath = *tlsCertificatePath
|
||||
config.TLS_CertificateKey = *tlsCertificateKey
|
||||
config.InitialDataFilePath = *initialDataPath
|
||||
config.PersistDataFilePath = *persistDataPath
|
||||
config.DisableAuth = *disableAuthentication
|
||||
config.DisableTls = *disableTls
|
||||
config.Debug = *debug
|
||||
config.AccountKey = *accountKey
|
||||
|
||||
Config.DatabaseAccount = Config.Host
|
||||
Config.DatabaseDomain = Config.Host
|
||||
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port)
|
||||
Config.AccountKey = *accountKey
|
||||
Config.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
|
||||
config.PopulateCalculatedFields()
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (c *ServerConfig) PopulateCalculatedFields() {
|
||||
c.DatabaseAccount = c.Host
|
||||
c.DatabaseDomain = c.Host
|
||||
c.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", c.Host, c.Port)
|
||||
c.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
|
||||
logger.EnableDebugOutput = c.Debug
|
||||
}
|
||||
|
||||
func setFlagsFromEnvironment() (err error) {
|
||||
|
||||
@@ -5,16 +5,15 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllCollections(c *gin.Context) {
|
||||
func (h *Handlers) GetAllCollections(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
|
||||
collections, status := repositories.GetAllCollections(databaseId)
|
||||
collections, status := h.repository.GetAllCollections(databaseId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
database, _ := repositories.GetDatabase(databaseId)
|
||||
database, _ := h.repository.GetDatabase(databaseId)
|
||||
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(collections)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
@@ -28,11 +27,11 @@ func GetAllCollections(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func GetCollection(c *gin.Context) {
|
||||
func (h *Handlers) GetCollection(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
id := c.Param("collId")
|
||||
|
||||
collection, status := repositories.GetCollection(databaseId, id)
|
||||
collection, status := h.repository.GetCollection(databaseId, id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, collection)
|
||||
return
|
||||
@@ -46,11 +45,11 @@ func GetCollection(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DeleteCollection(c *gin.Context) {
|
||||
func (h *Handlers) DeleteCollection(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
id := c.Param("collId")
|
||||
|
||||
status := repositories.DeleteCollection(databaseId, id)
|
||||
status := h.repository.DeleteCollection(databaseId, id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
@@ -64,7 +63,7 @@ func DeleteCollection(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func CreateCollection(c *gin.Context) {
|
||||
func (h *Handlers) CreateCollection(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
var newCollection repositorymodels.Collection
|
||||
|
||||
@@ -78,7 +77,7 @@ func CreateCollection(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
createdCollection, status := repositories.CreateCollection(databaseId, newCollection)
|
||||
createdCollection, status := h.repository.CreateCollection(databaseId, newCollection)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
|
||||
@@ -4,11 +4,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func CosmiumExport(c *gin.Context) {
|
||||
repositoryState, err := repositories.GetState()
|
||||
func (h *Handlers) CosmiumExport(c *gin.Context) {
|
||||
repositoryState, err := h.repository.GetState()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -5,12 +5,11 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllDatabases(c *gin.Context) {
|
||||
databases, status := repositories.GetAllDatabases()
|
||||
func (h *Handlers) GetAllDatabases(c *gin.Context) {
|
||||
databases, status := h.repository.GetAllDatabases()
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(databases)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
@@ -24,10 +23,10 @@ func GetAllDatabases(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func GetDatabase(c *gin.Context) {
|
||||
func (h *Handlers) GetDatabase(c *gin.Context) {
|
||||
id := c.Param("databaseId")
|
||||
|
||||
database, status := repositories.GetDatabase(id)
|
||||
database, status := h.repository.GetDatabase(id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, database)
|
||||
return
|
||||
@@ -41,10 +40,10 @@ func GetDatabase(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DeleteDatabase(c *gin.Context) {
|
||||
func (h *Handlers) DeleteDatabase(c *gin.Context) {
|
||||
id := c.Param("databaseId")
|
||||
|
||||
status := repositories.DeleteDatabase(id)
|
||||
status := h.repository.DeleteDatabase(id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
@@ -58,7 +57,7 @@ func DeleteDatabase(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func CreateDatabase(c *gin.Context) {
|
||||
func (h *Handlers) CreateDatabase(c *gin.Context) {
|
||||
var newDatabase repositorymodels.Database
|
||||
|
||||
if err := c.BindJSON(&newDatabase); err != nil {
|
||||
@@ -71,7 +70,7 @@ func CreateDatabase(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
createdDatabase, status := repositories.CreateDatabase(newDatabase)
|
||||
createdDatabase, status := h.repository.CreateDatabase(newDatabase)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
|
||||
@@ -10,17 +10,16 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/constants"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllDocuments(c *gin.Context) {
|
||||
func (h *Handlers) GetAllDocuments(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
documents, status := repositories.GetAllDocuments(databaseId, collectionId)
|
||||
documents, status := h.repository.GetAllDocuments(databaseId, collectionId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
||||
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(documents)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
@@ -34,12 +33,12 @@ func GetAllDocuments(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func GetDocument(c *gin.Context) {
|
||||
func (h *Handlers) GetDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
document, status := repositories.GetDocument(databaseId, collectionId, documentId)
|
||||
document, status := h.repository.GetDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, document)
|
||||
return
|
||||
@@ -53,12 +52,12 @@ func GetDocument(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DeleteDocument(c *gin.Context) {
|
||||
func (h *Handlers) DeleteDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
status := repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||
status := h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
@@ -73,7 +72,7 @@ func DeleteDocument(c *gin.Context) {
|
||||
}
|
||||
|
||||
// TODO: Maybe move "replace" logic to repository
|
||||
func ReplaceDocument(c *gin.Context) {
|
||||
func (h *Handlers) ReplaceDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
@@ -84,13 +83,13 @@ func ReplaceDocument(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
status := repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||
status := h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, requestBody)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
@@ -104,12 +103,12 @@ func ReplaceDocument(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func PatchDocument(c *gin.Context) {
|
||||
func (h *Handlers) PatchDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
document, status := repositories.GetDocument(databaseId, collectionId, documentId)
|
||||
document, status := h.repository.GetDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
@@ -160,13 +159,13 @@ func PatchDocument(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
status = repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||
status = h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, modifiedDocument)
|
||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, modifiedDocument)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
@@ -180,7 +179,7 @@ func PatchDocument(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DocumentsPost(c *gin.Context) {
|
||||
func (h *Handlers) DocumentsPost(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
@@ -202,14 +201,14 @@ func DocumentsPost(c *gin.Context) {
|
||||
queryParameters = parametersToMap(paramsArray)
|
||||
}
|
||||
|
||||
docs, status := repositories.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
|
||||
docs, status := h.repository.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
|
||||
if status != repositorymodels.StatusOk {
|
||||
// TODO: Currently we return everything if the query fails
|
||||
GetAllDocuments(c)
|
||||
h.GetAllDocuments(c)
|
||||
return
|
||||
}
|
||||
|
||||
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(docs)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": collection.ResourceID,
|
||||
@@ -226,10 +225,10 @@ func DocumentsPost(c *gin.Context) {
|
||||
|
||||
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert"))
|
||||
if isUpsert {
|
||||
repositories.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
|
||||
h.repository.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
|
||||
}
|
||||
|
||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, requestBody)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
|
||||
@@ -4,15 +4,14 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func RegisterExplorerHandlers(router *gin.Engine) {
|
||||
explorer := router.Group(config.Config.ExplorerBaseUrlLocation)
|
||||
func (h *Handlers) RegisterExplorerHandlers(router *gin.Engine) {
|
||||
explorer := router.Group(h.config.ExplorerBaseUrlLocation)
|
||||
{
|
||||
explorer.Use(func(ctx *gin.Context) {
|
||||
if ctx.Param("filepath") == "/config.json" {
|
||||
endpoint := fmt.Sprintf("https://%s:%d", config.Config.Host, config.Config.Port)
|
||||
endpoint := fmt.Sprintf("https://%s:%d", h.config.Host, h.config.Port)
|
||||
ctx.JSON(200, gin.H{
|
||||
"BACKEND_ENDPOINT": endpoint,
|
||||
"MONGO_BACKEND_ENDPOINT": endpoint,
|
||||
@@ -25,8 +24,8 @@ func RegisterExplorerHandlers(router *gin.Engine) {
|
||||
}
|
||||
})
|
||||
|
||||
if config.Config.ExplorerPath != "" {
|
||||
explorer.Static("/", config.Config.ExplorerPath)
|
||||
if h.config.ExplorerPath != "" {
|
||||
explorer.Static("/", h.config.ExplorerPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
api/handlers/handlers.go
Normal file
18
api/handlers/handlers.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
repository *repositories.DataRepository
|
||||
config config.ServerConfig
|
||||
}
|
||||
|
||||
func NewHandlers(dataRepository *repositories.DataRepository, config config.ServerConfig) *Handlers {
|
||||
return &Handlers{
|
||||
repository: dataRepository,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
func Authentication() gin.HandlerFunc {
|
||||
func Authentication(config config.ServerConfig) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
requestUrl := c.Request.URL.String()
|
||||
if config.Config.DisableAuth ||
|
||||
strings.HasPrefix(requestUrl, config.Config.ExplorerBaseUrlLocation) ||
|
||||
if config.DisableAuth ||
|
||||
strings.HasPrefix(requestUrl, config.ExplorerBaseUrlLocation) ||
|
||||
strings.HasPrefix(requestUrl, "/cosmium") {
|
||||
return
|
||||
}
|
||||
@@ -25,7 +25,7 @@ func Authentication() gin.HandlerFunc {
|
||||
authHeader := c.Request.Header.Get("authorization")
|
||||
date := c.Request.Header.Get("x-ms-date")
|
||||
expectedSignature := authentication.GenerateSignature(
|
||||
c.Request.Method, resourceType, resourceId, date, config.Config.AccountKey)
|
||||
c.Request.Method, resourceType, resourceId, date, config.AccountKey)
|
||||
|
||||
decoded, _ := url.QueryUnescape(authHeader)
|
||||
params, _ := url.ParseQuery(decoded)
|
||||
|
||||
@@ -7,10 +7,10 @@ import (
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func StripTrailingSlashes(r *gin.Engine) gin.HandlerFunc {
|
||||
func StripTrailingSlashes(r *gin.Engine, config config.ServerConfig) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
if len(path) > 1 && path[len(path)-1] == '/' && !strings.Contains(path, config.Config.ExplorerBaseUrlLocation) {
|
||||
if len(path) > 1 && path[len(path)-1] == '/' && !strings.Contains(path, config.ExplorerBaseUrlLocation) {
|
||||
c.Request.URL.Path = path[:len(path)-1]
|
||||
r.HandleContext(c)
|
||||
c.Abort()
|
||||
|
||||
@@ -5,11 +5,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetPartitionKeyRanges(c *gin.Context) {
|
||||
func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
@@ -18,7 +17,7 @@ func GetPartitionKeyRanges(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
partitionKeyRanges, status := repositories.GetPartitionKeyRanges(databaseId, collectionId)
|
||||
partitionKeyRanges, status := h.repository.GetPartitionKeyRanges(databaseId, collectionId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("etag", "\"420\"")
|
||||
c.Header("lsn", "420")
|
||||
@@ -27,7 +26,7 @@ func GetPartitionKeyRanges(c *gin.Context) {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(partitionKeyRanges)))
|
||||
|
||||
collectionRid := collectionId
|
||||
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
||||
if collection.ResourceID != "" {
|
||||
collectionRid = collection.ResourceID
|
||||
}
|
||||
|
||||
@@ -5,27 +5,26 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func GetServerInfo(c *gin.Context) {
|
||||
func (h *Handlers) 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),
|
||||
"id": h.config.DatabaseAccount,
|
||||
"_rid": fmt.Sprintf("%s.%s", h.config.DatabaseAccount, h.config.DatabaseDomain),
|
||||
"media": "//media/",
|
||||
"addresses": "//addresses/",
|
||||
"_dbs": "//dbs/",
|
||||
"writableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"readableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"enableMultipleWriteLocations": false,
|
||||
|
||||
@@ -5,15 +5,14 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllStoredProcedures(c *gin.Context) {
|
||||
func (h *Handlers) GetAllStoredProcedures(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
sps, status := repositories.GetAllStoredProcedures(databaseId, collectionId)
|
||||
sps, status := h.repository.GetAllStoredProcedures(databaseId, collectionId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(sps)))
|
||||
|
||||
@@ -5,15 +5,14 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllTriggers(c *gin.Context) {
|
||||
func (h *Handlers) GetAllTriggers(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
triggers, status := repositories.GetAllTriggers(databaseId, collectionId)
|
||||
triggers, status := h.repository.GetAllTriggers(databaseId, collectionId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(triggers)))
|
||||
|
||||
@@ -5,15 +5,14 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllUserDefinedFunctions(c *gin.Context) {
|
||||
func (h *Handlers) GetAllUserDefinedFunctions(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
udfs, status := repositories.GetAllUserDefinedFunctions(databaseId, collectionId)
|
||||
udfs, status := h.repository.GetAllUserDefinedFunctions(databaseId, collectionId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(udfs)))
|
||||
|
||||
@@ -6,78 +6,75 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/api/handlers"
|
||||
"github.com/pikami/cosmium/api/handlers/middleware"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
StopServer chan interface{}
|
||||
}
|
||||
func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
||||
routeHandlers := handlers.NewHandlers(repository, s.config)
|
||||
|
||||
func CreateRouter() *gin.Engine {
|
||||
router := gin.Default(func(e *gin.Engine) {
|
||||
e.RedirectTrailingSlash = false
|
||||
})
|
||||
|
||||
if config.Config.Debug {
|
||||
if s.config.Debug {
|
||||
router.Use(middleware.RequestLogger())
|
||||
}
|
||||
|
||||
router.Use(middleware.StripTrailingSlashes(router))
|
||||
router.Use(middleware.Authentication())
|
||||
router.Use(middleware.StripTrailingSlashes(router, s.config))
|
||||
router.Use(middleware.Authentication(s.config))
|
||||
|
||||
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/pkranges", routeHandlers.GetPartitionKeyRanges)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls/:collId/docs", handlers.DocumentsPost)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs", handlers.GetAllDocuments)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.GetDocument)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.ReplaceDocument)
|
||||
router.PATCH("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.PatchDocument)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.DeleteDocument)
|
||||
router.POST("/dbs/:databaseId/colls/:collId/docs", routeHandlers.DocumentsPost)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs", routeHandlers.GetAllDocuments)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.GetDocument)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.ReplaceDocument)
|
||||
router.PATCH("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.PatchDocument)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.DeleteDocument)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls", handlers.CreateCollection)
|
||||
router.GET("/dbs/:databaseId/colls", handlers.GetAllCollections)
|
||||
router.GET("/dbs/:databaseId/colls/:collId", handlers.GetCollection)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId", handlers.DeleteCollection)
|
||||
router.POST("/dbs/:databaseId/colls", routeHandlers.CreateCollection)
|
||||
router.GET("/dbs/:databaseId/colls", routeHandlers.GetAllCollections)
|
||||
router.GET("/dbs/:databaseId/colls/:collId", routeHandlers.GetCollection)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId", routeHandlers.DeleteCollection)
|
||||
|
||||
router.POST("/dbs", handlers.CreateDatabase)
|
||||
router.GET("/dbs", handlers.GetAllDatabases)
|
||||
router.GET("/dbs/:databaseId", handlers.GetDatabase)
|
||||
router.DELETE("/dbs/:databaseId", handlers.DeleteDatabase)
|
||||
router.POST("/dbs", routeHandlers.CreateDatabase)
|
||||
router.GET("/dbs", routeHandlers.GetAllDatabases)
|
||||
router.GET("/dbs/:databaseId", routeHandlers.GetDatabase)
|
||||
router.DELETE("/dbs/:databaseId", routeHandlers.DeleteDatabase)
|
||||
|
||||
router.GET("/dbs/:databaseId/colls/:collId/udfs", handlers.GetAllUserDefinedFunctions)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/sprocs", handlers.GetAllStoredProcedures)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/triggers", handlers.GetAllTriggers)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/udfs", routeHandlers.GetAllUserDefinedFunctions)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/sprocs", routeHandlers.GetAllStoredProcedures)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/triggers", routeHandlers.GetAllTriggers)
|
||||
|
||||
router.GET("/offers", handlers.GetOffers)
|
||||
router.GET("/", handlers.GetServerInfo)
|
||||
router.GET("/", routeHandlers.GetServerInfo)
|
||||
|
||||
router.GET("/cosmium/export", handlers.CosmiumExport)
|
||||
router.GET("/cosmium/export", routeHandlers.CosmiumExport)
|
||||
|
||||
handlers.RegisterExplorerHandlers(router)
|
||||
routeHandlers.RegisterExplorerHandlers(router)
|
||||
|
||||
return router
|
||||
s.router = router
|
||||
}
|
||||
|
||||
func StartAPI() *Server {
|
||||
if !config.Config.Debug {
|
||||
func (s *ApiServer) Start() {
|
||||
if !s.config.Debug {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
router := CreateRouter()
|
||||
listenAddress := fmt.Sprintf(":%d", config.Config.Port)
|
||||
stopChan := make(chan interface{})
|
||||
listenAddress := fmt.Sprintf(":%d", s.config.Port)
|
||||
s.isActive = true
|
||||
|
||||
server := &http.Server{
|
||||
Addr: listenAddress,
|
||||
Handler: router.Handler(),
|
||||
Handler: s.router.Handler(),
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-stopChan
|
||||
<-s.stopServer
|
||||
logger.Info("Shutting down server...")
|
||||
err := server.Shutdown(context.TODO())
|
||||
if err != nil {
|
||||
@@ -86,24 +83,22 @@ func StartAPI() *Server {
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if config.Config.DisableTls {
|
||||
if s.config.DisableTls {
|
||||
logger.Infof("Listening and serving HTTP on %s\n", server.Addr)
|
||||
err := server.ListenAndServe()
|
||||
if err != nil {
|
||||
logger.Error("Failed to start HTTP server:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if config.Config.TLS_CertificatePath != "" && config.Config.TLS_CertificateKey != "" {
|
||||
s.isActive = false
|
||||
} else if s.config.TLS_CertificatePath != "" && s.config.TLS_CertificateKey != "" {
|
||||
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
||||
err := server.ListenAndServeTLS(
|
||||
config.Config.TLS_CertificatePath,
|
||||
config.Config.TLS_CertificateKey)
|
||||
s.config.TLS_CertificatePath,
|
||||
s.config.TLS_CertificateKey)
|
||||
if err != nil {
|
||||
logger.Error("Failed to start HTTPS server:", err)
|
||||
}
|
||||
return
|
||||
s.isActive = false
|
||||
} else {
|
||||
tlsConfig := tlsprovider.GetDefaultTlsConfig()
|
||||
server.TLSConfig = tlsConfig
|
||||
@@ -113,9 +108,7 @@ func StartAPI() *Server {
|
||||
if err != nil {
|
||||
logger.Error("Failed to start HTTPS server:", err)
|
||||
}
|
||||
return
|
||||
s.isActive = false
|
||||
}
|
||||
}()
|
||||
|
||||
return &Server{StopServer: stopChan}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,15 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Authentication(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should get 200 when correct account key is used", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
@@ -35,26 +34,8 @@ func Test_Authentication(t *testing.T) {
|
||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
})
|
||||
|
||||
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
|
||||
config.Config.DisableAuth = true
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
createResponse, err := client.CreateDatabase(
|
||||
context.TODO(),
|
||||
azcosmos.DatabaseProperties{ID: testDatabaseName},
|
||||
&azcosmos.CreateDatabaseOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
config.Config.DisableAuth = false
|
||||
})
|
||||
|
||||
t.Run("Should get 401 when wrong account key is used", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
||||
&azcosmos.ClientOptions{},
|
||||
@@ -85,3 +66,29 @@ func Test_Authentication(t *testing.T) {
|
||||
assert.Contains(t, string(responseBody), "BACKEND_ENDPOINT")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Authentication_Disabled(t *testing.T) {
|
||||
ts := runTestServerCustomConfig(config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
DisableAuth: true,
|
||||
})
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
createResponse, err := client.CreateDatabase(
|
||||
context.TODO(),
|
||||
azcosmos.DatabaseProperties{ID: testDatabaseName},
|
||||
&azcosmos.CreateDatabaseOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,22 +10,21 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Collections(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
databaseClient, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -40,7 +39,7 @@ func Test_Collections(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return conflict when collection exists", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
@@ -60,7 +59,7 @@ func Test_Collections(t *testing.T) {
|
||||
|
||||
t.Run("Collection Read", func(t *testing.T) {
|
||||
t.Run("Should read collection", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
@@ -74,7 +73,7 @@ func Test_Collections(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
@@ -93,7 +92,7 @@ func Test_Collections(t *testing.T) {
|
||||
|
||||
t.Run("Collection Delete", func(t *testing.T) {
|
||||
t.Run("Should delete collection", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
@@ -106,7 +105,7 @@ func Test_Collections(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -5,14 +5,37 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func runTestServer() *httptest.Server {
|
||||
config.Config.AccountKey = config.DefaultAccountKey
|
||||
config.Config.ExplorerPath = "/tmp/nothing"
|
||||
config.Config.ExplorerBaseUrlLocation = config.ExplorerBaseUrlLocation
|
||||
type TestServer struct {
|
||||
Server *httptest.Server
|
||||
Repository *repositories.DataRepository
|
||||
URL string
|
||||
}
|
||||
|
||||
return httptest.NewServer(api.CreateRouter())
|
||||
func runTestServerCustomConfig(config config.ServerConfig) *TestServer {
|
||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{})
|
||||
|
||||
api := api.NewApiServer(repository, config)
|
||||
|
||||
server := httptest.NewServer(api.GetRouter())
|
||||
|
||||
return &TestServer{
|
||||
Server: server,
|
||||
Repository: repository,
|
||||
URL: server.URL,
|
||||
}
|
||||
}
|
||||
|
||||
func runTestServer() *TestServer {
|
||||
config := config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
}
|
||||
|
||||
return runTestServerCustomConfig(config)
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -10,24 +10,23 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Databases(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Database Create", func(t *testing.T) {
|
||||
t.Run("Should create database", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
|
||||
createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
|
||||
ID: testDatabaseName,
|
||||
@@ -38,7 +37,7 @@ func Test_Databases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return conflict when database exists", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
@@ -58,7 +57,7 @@ func Test_Databases(t *testing.T) {
|
||||
|
||||
t.Run("Database Read", func(t *testing.T) {
|
||||
t.Run("Should read database", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
@@ -72,7 +71,7 @@ func Test_Databases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
@@ -91,7 +90,7 @@ func Test_Databases(t *testing.T) {
|
||||
|
||||
t.Run("Database Delete", func(t *testing.T) {
|
||||
t.Run("Should delete database", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
@@ -104,7 +103,7 @@ func Test_Databases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -15,7 +14,6 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -55,9 +53,11 @@ func testCosmosQuery(t *testing.T,
|
||||
}
|
||||
}
|
||||
|
||||
func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.ContainerClient) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClient) {
|
||||
ts := runTestServer()
|
||||
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
PartitionKey: struct {
|
||||
Paths []string "json:\"paths\""
|
||||
@@ -67,13 +67,11 @@ func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.Container
|
||||
Paths: []string{"/pk"},
|
||||
},
|
||||
})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
|
||||
|
||||
ts := runTestServer()
|
||||
ts.Repository.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
|
||||
ts.Repository.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
@@ -86,7 +84,7 @@ func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.Container
|
||||
|
||||
func Test_Documents(t *testing.T) {
|
||||
ts, collectionClient := documents_InitializeDb(t)
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should query document", func(t *testing.T) {
|
||||
testCosmosQuery(t, collectionClient,
|
||||
@@ -218,7 +216,7 @@ func Test_Documents(t *testing.T) {
|
||||
|
||||
func Test_Documents_Patch(t *testing.T) {
|
||||
ts, collectionClient := documents_InitializeDb(t)
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should PATCH document", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
@@ -15,14 +15,14 @@ import (
|
||||
// Request document with trailing slash like python cosmosdb client does.
|
||||
func Test_Documents_Read_Trailing_Slash(t *testing.T) {
|
||||
ts, _ := documents_InitializeDb(t)
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
|
||||
resourceIdTemplate := "dbs/%s/colls/%s/docs/%s"
|
||||
path := fmt.Sprintf(resourceIdTemplate, testDatabaseName, testCollectionName, "12345")
|
||||
testUrl := ts.URL + "/" + path + "/"
|
||||
date := time.Now().Format(time.RFC1123)
|
||||
signature := authentication.GenerateSignature("GET", "docs", path, date, config.Config.AccountKey)
|
||||
signature := authentication.GenerateSignature("GET", "docs", path, date, config.DefaultAccountKey)
|
||||
httpClient := &http.Client{}
|
||||
req, _ := http.NewRequest("GET", testUrl, nil)
|
||||
req.Header.Add("x-ms-date", date)
|
||||
|
||||
Reference in New Issue
Block a user