mirror of
https://github.com/pikami/cosmium.git
synced 2025-06-08 08:30:24 +01:00
Compare commits
No commits in common. "be7a6159316be04f13988d2f6037a5e1681cb3da" and "84c33e3c8e91754cbd4ef1f8d3eacd286e6bafa9" have entirely different histories.
be7a615931
...
84c33e3c8e
30
.github/workflows/compile-shared-libraries.yml
vendored
30
.github/workflows/compile-shared-libraries.yml
vendored
@ -1,30 +0,0 @@
|
|||||||
name: Cross-Compile Shared Libraries
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Cross-Compile with xgo
|
|
||||||
uses: crazy-max/ghaction-xgo@v3.1.0
|
|
||||||
with:
|
|
||||||
xgo_version: latest
|
|
||||||
go_version: 1.22.0
|
|
||||||
dest: dist
|
|
||||||
pkg: sharedlibrary
|
|
||||||
prefix: cosmium
|
|
||||||
targets: linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
|
|
||||||
v: true
|
|
||||||
buildmode: c-shared
|
|
||||||
|
|
||||||
- name: Upload artifact
|
|
||||||
uses: actions/upload-artifact@v3
|
|
||||||
with:
|
|
||||||
name: shared-libraries
|
|
||||||
path: dist/*
|
|
7
Makefile
7
Makefile
@ -8,8 +8,6 @@ SERVER_LOCATION=./cmd/server
|
|||||||
|
|
||||||
SHARED_LIB_LOCATION=./sharedlibrary
|
SHARED_LIB_LOCATION=./sharedlibrary
|
||||||
SHARED_LIB_OPT=-buildmode=c-shared
|
SHARED_LIB_OPT=-buildmode=c-shared
|
||||||
XGO_TARGETS=linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
|
|
||||||
GOVERSION=1.22.0
|
|
||||||
|
|
||||||
DIST_DIR=dist
|
DIST_DIR=dist
|
||||||
|
|
||||||
@ -45,11 +43,6 @@ build-sharedlib-linux-amd64:
|
|||||||
@echo "Building shared library for Linux x64..."
|
@echo "Building shared library for Linux x64..."
|
||||||
@GOOS=linux GOARCH=amd64 $(GOBUILD) $(SHARED_LIB_OPT) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64.so $(SHARED_LIB_LOCATION)
|
@GOOS=linux GOARCH=amd64 $(GOBUILD) $(SHARED_LIB_OPT) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64.so $(SHARED_LIB_LOCATION)
|
||||||
|
|
||||||
xgo-compile-sharedlib:
|
|
||||||
@echo "Building shared libraries using xgo..."
|
|
||||||
@mkdir -p $(DIST_DIR)
|
|
||||||
@xgo -targets=$(XGO_TARGETS) -go $(GOVERSION) -buildmode=c-shared -dest=$(DIST_DIR) -out=$(BINARY_NAME) -pkg=$(SHARED_LIB_LOCATION) .
|
|
||||||
|
|
||||||
generate-parser-nosql:
|
generate-parser-nosql:
|
||||||
pigeon -o ./parsers/nosql/nosql.go ./parsers/nosql/nosql.peg
|
pigeon -o ./parsers/nosql/nosql.go ./parsers/nosql/nosql.peg
|
||||||
|
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
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,8 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/internal/logger"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -15,7 +13,9 @@ const (
|
|||||||
ExplorerBaseUrlLocation = "/_explorer"
|
ExplorerBaseUrlLocation = "/_explorer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseFlags() ServerConfig {
|
var Config = ServerConfig{}
|
||||||
|
|
||||||
|
func ParseFlags() {
|
||||||
host := flag.String("Host", "localhost", "Hostname")
|
host := flag.String("Host", "localhost", "Hostname")
|
||||||
port := flag.Int("Port", 8081, "Listen port")
|
port := flag.Int("Port", 8081, "Listen port")
|
||||||
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
|
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
|
||||||
@ -31,30 +31,22 @@ func ParseFlags() ServerConfig {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
setFlagsFromEnvironment()
|
setFlagsFromEnvironment()
|
||||||
|
|
||||||
config := ServerConfig{}
|
Config.Host = *host
|
||||||
config.Host = *host
|
Config.Port = *port
|
||||||
config.Port = *port
|
Config.ExplorerPath = *explorerPath
|
||||||
config.ExplorerPath = *explorerPath
|
Config.TLS_CertificatePath = *tlsCertificatePath
|
||||||
config.TLS_CertificatePath = *tlsCertificatePath
|
Config.TLS_CertificateKey = *tlsCertificateKey
|
||||||
config.TLS_CertificateKey = *tlsCertificateKey
|
Config.InitialDataFilePath = *initialDataPath
|
||||||
config.InitialDataFilePath = *initialDataPath
|
Config.PersistDataFilePath = *persistDataPath
|
||||||
config.PersistDataFilePath = *persistDataPath
|
Config.DisableAuth = *disableAuthentication
|
||||||
config.DisableAuth = *disableAuthentication
|
Config.DisableTls = *disableTls
|
||||||
config.DisableTls = *disableTls
|
Config.Debug = *debug
|
||||||
config.Debug = *debug
|
|
||||||
config.AccountKey = *accountKey
|
|
||||||
|
|
||||||
config.PopulateCalculatedFields()
|
Config.DatabaseAccount = Config.Host
|
||||||
|
Config.DatabaseDomain = Config.Host
|
||||||
return config
|
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port)
|
||||||
}
|
Config.AccountKey = *accountKey
|
||||||
|
Config.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
|
||||||
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) {
|
func setFlagsFromEnvironment() (err error) {
|
||||||
|
@ -5,15 +5,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetAllCollections(c *gin.Context) {
|
func GetAllCollections(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
|
|
||||||
collections, status := h.repository.GetAllCollections(databaseId)
|
collections, status := repositories.GetAllCollections(databaseId)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
database, _ := h.repository.GetDatabase(databaseId)
|
database, _ := repositories.GetDatabase(databaseId)
|
||||||
|
|
||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(collections)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(collections)))
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{
|
c.IndentedJSON(http.StatusOK, gin.H{
|
||||||
@ -27,11 +28,11 @@ func (h *Handlers) GetAllCollections(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) GetCollection(c *gin.Context) {
|
func GetCollection(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
id := c.Param("collId")
|
id := c.Param("collId")
|
||||||
|
|
||||||
collection, status := h.repository.GetCollection(databaseId, id)
|
collection, status := repositories.GetCollection(databaseId, id)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.IndentedJSON(http.StatusOK, collection)
|
c.IndentedJSON(http.StatusOK, collection)
|
||||||
return
|
return
|
||||||
@ -45,11 +46,11 @@ func (h *Handlers) GetCollection(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) DeleteCollection(c *gin.Context) {
|
func DeleteCollection(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
id := c.Param("collId")
|
id := c.Param("collId")
|
||||||
|
|
||||||
status := h.repository.DeleteCollection(databaseId, id)
|
status := repositories.DeleteCollection(databaseId, id)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Status(http.StatusNoContent)
|
c.Status(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
@ -63,7 +64,7 @@ func (h *Handlers) DeleteCollection(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) CreateCollection(c *gin.Context) {
|
func CreateCollection(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
var newCollection repositorymodels.Collection
|
var newCollection repositorymodels.Collection
|
||||||
|
|
||||||
@ -77,7 +78,7 @@ func (h *Handlers) CreateCollection(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
createdCollection, status := h.repository.CreateCollection(databaseId, newCollection)
|
createdCollection, status := repositories.CreateCollection(databaseId, newCollection)
|
||||||
if status == repositorymodels.Conflict {
|
if status == repositorymodels.Conflict {
|
||||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||||
return
|
return
|
||||||
|
@ -4,10 +4,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) CosmiumExport(c *gin.Context) {
|
func CosmiumExport(c *gin.Context) {
|
||||||
repositoryState, err := h.repository.GetState()
|
repositoryState, err := repositories.GetState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
@ -5,11 +5,12 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetAllDatabases(c *gin.Context) {
|
func GetAllDatabases(c *gin.Context) {
|
||||||
databases, status := h.repository.GetAllDatabases()
|
databases, status := repositories.GetAllDatabases()
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(databases)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(databases)))
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{
|
c.IndentedJSON(http.StatusOK, gin.H{
|
||||||
@ -23,10 +24,10 @@ func (h *Handlers) GetAllDatabases(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) GetDatabase(c *gin.Context) {
|
func GetDatabase(c *gin.Context) {
|
||||||
id := c.Param("databaseId")
|
id := c.Param("databaseId")
|
||||||
|
|
||||||
database, status := h.repository.GetDatabase(id)
|
database, status := repositories.GetDatabase(id)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.IndentedJSON(http.StatusOK, database)
|
c.IndentedJSON(http.StatusOK, database)
|
||||||
return
|
return
|
||||||
@ -40,10 +41,10 @@ func (h *Handlers) GetDatabase(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) DeleteDatabase(c *gin.Context) {
|
func DeleteDatabase(c *gin.Context) {
|
||||||
id := c.Param("databaseId")
|
id := c.Param("databaseId")
|
||||||
|
|
||||||
status := h.repository.DeleteDatabase(id)
|
status := repositories.DeleteDatabase(id)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Status(http.StatusNoContent)
|
c.Status(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
@ -57,7 +58,7 @@ func (h *Handlers) DeleteDatabase(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) CreateDatabase(c *gin.Context) {
|
func CreateDatabase(c *gin.Context) {
|
||||||
var newDatabase repositorymodels.Database
|
var newDatabase repositorymodels.Database
|
||||||
|
|
||||||
if err := c.BindJSON(&newDatabase); err != nil {
|
if err := c.BindJSON(&newDatabase); err != nil {
|
||||||
@ -70,7 +71,7 @@ func (h *Handlers) CreateDatabase(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
createdDatabase, status := h.repository.CreateDatabase(newDatabase)
|
createdDatabase, status := repositories.CreateDatabase(newDatabase)
|
||||||
if status == repositorymodels.Conflict {
|
if status == repositorymodels.Conflict {
|
||||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||||
return
|
return
|
||||||
|
@ -10,16 +10,17 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pikami/cosmium/internal/constants"
|
"github.com/pikami/cosmium/internal/constants"
|
||||||
"github.com/pikami/cosmium/internal/logger"
|
"github.com/pikami/cosmium/internal/logger"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetAllDocuments(c *gin.Context) {
|
func GetAllDocuments(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
|
|
||||||
documents, status := h.repository.GetAllDocuments(databaseId, collectionId)
|
documents, status := repositories.GetAllDocuments(databaseId, collectionId)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||||
|
|
||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(documents)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(documents)))
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{
|
c.IndentedJSON(http.StatusOK, gin.H{
|
||||||
@ -33,12 +34,12 @@ func (h *Handlers) GetAllDocuments(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) GetDocument(c *gin.Context) {
|
func GetDocument(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
documentId := c.Param("docId")
|
documentId := c.Param("docId")
|
||||||
|
|
||||||
document, status := h.repository.GetDocument(databaseId, collectionId, documentId)
|
document, status := repositories.GetDocument(databaseId, collectionId, documentId)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.IndentedJSON(http.StatusOK, document)
|
c.IndentedJSON(http.StatusOK, document)
|
||||||
return
|
return
|
||||||
@ -52,12 +53,12 @@ func (h *Handlers) GetDocument(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) DeleteDocument(c *gin.Context) {
|
func DeleteDocument(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
documentId := c.Param("docId")
|
documentId := c.Param("docId")
|
||||||
|
|
||||||
status := h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
status := repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Status(http.StatusNoContent)
|
c.Status(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
@ -72,7 +73,7 @@ func (h *Handlers) DeleteDocument(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Maybe move "replace" logic to repository
|
// TODO: Maybe move "replace" logic to repository
|
||||||
func (h *Handlers) ReplaceDocument(c *gin.Context) {
|
func ReplaceDocument(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
documentId := c.Param("docId")
|
documentId := c.Param("docId")
|
||||||
@ -83,13 +84,13 @@ func (h *Handlers) ReplaceDocument(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
status := h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
status := repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||||
if status == repositorymodels.StatusNotFound {
|
if status == repositorymodels.StatusNotFound {
|
||||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
createdDocument, status := h.repository.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"})
|
||||||
return
|
return
|
||||||
@ -103,12 +104,12 @@ func (h *Handlers) ReplaceDocument(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) PatchDocument(c *gin.Context) {
|
func PatchDocument(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
documentId := c.Param("docId")
|
documentId := c.Param("docId")
|
||||||
|
|
||||||
document, status := h.repository.GetDocument(databaseId, collectionId, documentId)
|
document, status := repositories.GetDocument(databaseId, collectionId, documentId)
|
||||||
if status == repositorymodels.StatusNotFound {
|
if status == repositorymodels.StatusNotFound {
|
||||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||||
return
|
return
|
||||||
@ -159,13 +160,13 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
status = h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
status = repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||||
if status == repositorymodels.StatusNotFound {
|
if status == repositorymodels.StatusNotFound {
|
||||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, modifiedDocument)
|
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, modifiedDocument)
|
||||||
if status == repositorymodels.Conflict {
|
if status == repositorymodels.Conflict {
|
||||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||||
return
|
return
|
||||||
@ -179,7 +180,7 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) DocumentsPost(c *gin.Context) {
|
func DocumentsPost(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
|
|
||||||
@ -201,14 +202,14 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
|
|||||||
queryParameters = parametersToMap(paramsArray)
|
queryParameters = parametersToMap(paramsArray)
|
||||||
}
|
}
|
||||||
|
|
||||||
docs, status := h.repository.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
|
docs, status := repositories.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
|
||||||
if status != repositorymodels.StatusOk {
|
if status != repositorymodels.StatusOk {
|
||||||
// TODO: Currently we return everything if the query fails
|
// TODO: Currently we return everything if the query fails
|
||||||
h.GetAllDocuments(c)
|
GetAllDocuments(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(docs)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(docs)))
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{
|
c.IndentedJSON(http.StatusOK, gin.H{
|
||||||
"_rid": collection.ResourceID,
|
"_rid": collection.ResourceID,
|
||||||
@ -225,10 +226,10 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
|
|||||||
|
|
||||||
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert"))
|
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert"))
|
||||||
if isUpsert {
|
if isUpsert {
|
||||||
h.repository.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
|
repositories.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
createdDocument, status := h.repository.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"})
|
||||||
return
|
return
|
||||||
|
@ -4,14 +4,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/api/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) RegisterExplorerHandlers(router *gin.Engine) {
|
func RegisterExplorerHandlers(router *gin.Engine) {
|
||||||
explorer := router.Group(h.config.ExplorerBaseUrlLocation)
|
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" {
|
||||||
endpoint := fmt.Sprintf("https://%s:%d", h.config.Host, h.config.Port)
|
endpoint := fmt.Sprintf("https://%s:%d", config.Config.Host, config.Config.Port)
|
||||||
ctx.JSON(200, gin.H{
|
ctx.JSON(200, gin.H{
|
||||||
"BACKEND_ENDPOINT": endpoint,
|
"BACKEND_ENDPOINT": endpoint,
|
||||||
"MONGO_BACKEND_ENDPOINT": endpoint,
|
"MONGO_BACKEND_ENDPOINT": endpoint,
|
||||||
@ -24,8 +25,8 @@ func (h *Handlers) RegisterExplorerHandlers(router *gin.Engine) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if h.config.ExplorerPath != "" {
|
if config.Config.ExplorerPath != "" {
|
||||||
explorer.Static("/", h.config.ExplorerPath)
|
explorer.Static("/", config.Config.ExplorerPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
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"
|
"github.com/pikami/cosmium/internal/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Authentication(config config.ServerConfig) gin.HandlerFunc {
|
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.DisableAuth ||
|
if config.Config.DisableAuth ||
|
||||||
strings.HasPrefix(requestUrl, config.ExplorerBaseUrlLocation) ||
|
strings.HasPrefix(requestUrl, config.Config.ExplorerBaseUrlLocation) ||
|
||||||
strings.HasPrefix(requestUrl, "/cosmium") {
|
strings.HasPrefix(requestUrl, "/cosmium") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -25,7 +25,7 @@ func Authentication(config config.ServerConfig) gin.HandlerFunc {
|
|||||||
authHeader := c.Request.Header.Get("authorization")
|
authHeader := c.Request.Header.Get("authorization")
|
||||||
date := c.Request.Header.Get("x-ms-date")
|
date := c.Request.Header.Get("x-ms-date")
|
||||||
expectedSignature := authentication.GenerateSignature(
|
expectedSignature := authentication.GenerateSignature(
|
||||||
c.Request.Method, resourceType, resourceId, date, config.AccountKey)
|
c.Request.Method, resourceType, resourceId, date, config.Config.AccountKey)
|
||||||
|
|
||||||
decoded, _ := url.QueryUnescape(authHeader)
|
decoded, _ := url.QueryUnescape(authHeader)
|
||||||
params, _ := url.ParseQuery(decoded)
|
params, _ := url.ParseQuery(decoded)
|
||||||
|
@ -7,10 +7,10 @@ import (
|
|||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StripTrailingSlashes(r *gin.Engine, config config.ServerConfig) 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] == '/' && !strings.Contains(path, config.ExplorerBaseUrlLocation) {
|
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()
|
||||||
|
@ -5,10 +5,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
func GetPartitionKeyRanges(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
partitionKeyRanges, status := h.repository.GetPartitionKeyRanges(databaseId, collectionId)
|
partitionKeyRanges, status := repositories.GetPartitionKeyRanges(databaseId, collectionId)
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Header("etag", "\"420\"")
|
c.Header("etag", "\"420\"")
|
||||||
c.Header("lsn", "420")
|
c.Header("lsn", "420")
|
||||||
@ -26,7 +27,7 @@ func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
|||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(partitionKeyRanges)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(partitionKeyRanges)))
|
||||||
|
|
||||||
collectionRid := collectionId
|
collectionRid := collectionId
|
||||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||||
if collection.ResourceID != "" {
|
if collection.ResourceID != "" {
|
||||||
collectionRid = collection.ResourceID
|
collectionRid = collection.ResourceID
|
||||||
}
|
}
|
||||||
|
@ -5,26 +5,27 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/api/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetServerInfo(c *gin.Context) {
|
func GetServerInfo(c *gin.Context) {
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{
|
c.IndentedJSON(http.StatusOK, gin.H{
|
||||||
"_self": "",
|
"_self": "",
|
||||||
"id": h.config.DatabaseAccount,
|
"id": config.Config.DatabaseAccount,
|
||||||
"_rid": fmt.Sprintf("%s.%s", h.config.DatabaseAccount, h.config.DatabaseDomain),
|
"_rid": fmt.Sprintf("%s.%s", config.Config.DatabaseAccount, config.Config.DatabaseDomain),
|
||||||
"media": "//media/",
|
"media": "//media/",
|
||||||
"addresses": "//addresses/",
|
"addresses": "//addresses/",
|
||||||
"_dbs": "//dbs/",
|
"_dbs": "//dbs/",
|
||||||
"writableLocations": []map[string]interface{}{
|
"writableLocations": []map[string]interface{}{
|
||||||
{
|
{
|
||||||
"name": "South Central US",
|
"name": "South Central US",
|
||||||
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
|
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"readableLocations": []map[string]interface{}{
|
"readableLocations": []map[string]interface{}{
|
||||||
{
|
{
|
||||||
"name": "South Central US",
|
"name": "South Central US",
|
||||||
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
|
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"enableMultipleWriteLocations": false,
|
"enableMultipleWriteLocations": false,
|
||||||
|
@ -5,14 +5,15 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetAllStoredProcedures(c *gin.Context) {
|
func GetAllStoredProcedures(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
|
|
||||||
sps, status := h.repository.GetAllStoredProcedures(databaseId, collectionId)
|
sps, status := repositories.GetAllStoredProcedures(databaseId, collectionId)
|
||||||
|
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(sps)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(sps)))
|
||||||
|
@ -5,14 +5,15 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetAllTriggers(c *gin.Context) {
|
func GetAllTriggers(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
|
|
||||||
triggers, status := h.repository.GetAllTriggers(databaseId, collectionId)
|
triggers, status := repositories.GetAllTriggers(databaseId, collectionId)
|
||||||
|
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(triggers)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(triggers)))
|
||||||
|
@ -5,14 +5,15 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetAllUserDefinedFunctions(c *gin.Context) {
|
func GetAllUserDefinedFunctions(c *gin.Context) {
|
||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
collectionId := c.Param("collId")
|
||||||
|
|
||||||
udfs, status := h.repository.GetAllUserDefinedFunctions(databaseId, collectionId)
|
udfs, status := repositories.GetAllUserDefinedFunctions(databaseId, collectionId)
|
||||||
|
|
||||||
if status == repositorymodels.StatusOk {
|
if status == repositorymodels.StatusOk {
|
||||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(udfs)))
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(udfs)))
|
||||||
|
@ -6,75 +6,78 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pikami/cosmium/api/config"
|
||||||
"github.com/pikami/cosmium/api/handlers"
|
"github.com/pikami/cosmium/api/handlers"
|
||||||
"github.com/pikami/cosmium/api/handlers/middleware"
|
"github.com/pikami/cosmium/api/handlers/middleware"
|
||||||
"github.com/pikami/cosmium/internal/logger"
|
"github.com/pikami/cosmium/internal/logger"
|
||||||
"github.com/pikami/cosmium/internal/repositories"
|
|
||||||
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
|
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
type Server struct {
|
||||||
routeHandlers := handlers.NewHandlers(repository, s.config)
|
StopServer chan interface{}
|
||||||
|
}
|
||||||
if !s.config.Debug {
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func CreateRouter() *gin.Engine {
|
||||||
router := gin.Default(func(e *gin.Engine) {
|
router := gin.Default(func(e *gin.Engine) {
|
||||||
e.RedirectTrailingSlash = false
|
e.RedirectTrailingSlash = false
|
||||||
})
|
})
|
||||||
|
|
||||||
if s.config.Debug {
|
if config.Config.Debug {
|
||||||
router.Use(middleware.RequestLogger())
|
router.Use(middleware.RequestLogger())
|
||||||
}
|
}
|
||||||
|
|
||||||
router.Use(middleware.StripTrailingSlashes(router, s.config))
|
router.Use(middleware.StripTrailingSlashes(router))
|
||||||
router.Use(middleware.Authentication(s.config))
|
router.Use(middleware.Authentication())
|
||||||
|
|
||||||
router.GET("/dbs/:databaseId/colls/:collId/pkranges", routeHandlers.GetPartitionKeyRanges)
|
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges)
|
||||||
|
|
||||||
router.POST("/dbs/:databaseId/colls/:collId/docs", routeHandlers.DocumentsPost)
|
router.POST("/dbs/:databaseId/colls/:collId/docs", handlers.DocumentsPost)
|
||||||
router.GET("/dbs/:databaseId/colls/:collId/docs", routeHandlers.GetAllDocuments)
|
router.GET("/dbs/:databaseId/colls/:collId/docs", handlers.GetAllDocuments)
|
||||||
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.GetDocument)
|
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.GetDocument)
|
||||||
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.ReplaceDocument)
|
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.ReplaceDocument)
|
||||||
router.PATCH("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.PatchDocument)
|
router.PATCH("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.PatchDocument)
|
||||||
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.DeleteDocument)
|
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.DeleteDocument)
|
||||||
|
|
||||||
router.POST("/dbs/:databaseId/colls", routeHandlers.CreateCollection)
|
router.POST("/dbs/:databaseId/colls", handlers.CreateCollection)
|
||||||
router.GET("/dbs/:databaseId/colls", routeHandlers.GetAllCollections)
|
router.GET("/dbs/:databaseId/colls", handlers.GetAllCollections)
|
||||||
router.GET("/dbs/:databaseId/colls/:collId", routeHandlers.GetCollection)
|
router.GET("/dbs/:databaseId/colls/:collId", handlers.GetCollection)
|
||||||
router.DELETE("/dbs/:databaseId/colls/:collId", routeHandlers.DeleteCollection)
|
router.DELETE("/dbs/:databaseId/colls/:collId", handlers.DeleteCollection)
|
||||||
|
|
||||||
router.POST("/dbs", routeHandlers.CreateDatabase)
|
router.POST("/dbs", handlers.CreateDatabase)
|
||||||
router.GET("/dbs", routeHandlers.GetAllDatabases)
|
router.GET("/dbs", handlers.GetAllDatabases)
|
||||||
router.GET("/dbs/:databaseId", routeHandlers.GetDatabase)
|
router.GET("/dbs/:databaseId", handlers.GetDatabase)
|
||||||
router.DELETE("/dbs/:databaseId", routeHandlers.DeleteDatabase)
|
router.DELETE("/dbs/:databaseId", handlers.DeleteDatabase)
|
||||||
|
|
||||||
router.GET("/dbs/:databaseId/colls/:collId/udfs", routeHandlers.GetAllUserDefinedFunctions)
|
router.GET("/dbs/:databaseId/colls/:collId/udfs", handlers.GetAllUserDefinedFunctions)
|
||||||
router.GET("/dbs/:databaseId/colls/:collId/sprocs", routeHandlers.GetAllStoredProcedures)
|
router.GET("/dbs/:databaseId/colls/:collId/sprocs", handlers.GetAllStoredProcedures)
|
||||||
router.GET("/dbs/:databaseId/colls/:collId/triggers", routeHandlers.GetAllTriggers)
|
router.GET("/dbs/:databaseId/colls/:collId/triggers", handlers.GetAllTriggers)
|
||||||
|
|
||||||
router.GET("/offers", handlers.GetOffers)
|
router.GET("/offers", handlers.GetOffers)
|
||||||
router.GET("/", routeHandlers.GetServerInfo)
|
router.GET("/", handlers.GetServerInfo)
|
||||||
|
|
||||||
router.GET("/cosmium/export", routeHandlers.CosmiumExport)
|
router.GET("/cosmium/export", handlers.CosmiumExport)
|
||||||
|
|
||||||
routeHandlers.RegisterExplorerHandlers(router)
|
handlers.RegisterExplorerHandlers(router)
|
||||||
|
|
||||||
s.router = router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiServer) Start() {
|
func StartAPI() *Server {
|
||||||
listenAddress := fmt.Sprintf(":%d", s.config.Port)
|
if !config.Config.Debug {
|
||||||
s.isActive = true
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
router := CreateRouter()
|
||||||
|
listenAddress := fmt.Sprintf(":%d", config.Config.Port)
|
||||||
|
stopChan := make(chan interface{})
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: listenAddress,
|
Addr: listenAddress,
|
||||||
Handler: s.router.Handler(),
|
Handler: router.Handler(),
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-s.stopServer
|
<-stopChan
|
||||||
logger.Info("Shutting down server...")
|
logger.Info("Shutting down server...")
|
||||||
err := server.Shutdown(context.TODO())
|
err := server.Shutdown(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -83,22 +86,24 @@ func (s *ApiServer) Start() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if s.config.DisableTls {
|
if config.Config.DisableTls {
|
||||||
logger.Infof("Listening and serving HTTP on %s\n", server.Addr)
|
logger.Infof("Listening and serving HTTP on %s\n", server.Addr)
|
||||||
err := server.ListenAndServe()
|
err := server.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to start HTTP server:", err)
|
logger.Error("Failed to start HTTP server:", err)
|
||||||
}
|
}
|
||||||
s.isActive = false
|
return
|
||||||
} else if s.config.TLS_CertificatePath != "" && s.config.TLS_CertificateKey != "" {
|
}
|
||||||
|
|
||||||
|
if config.Config.TLS_CertificatePath != "" && config.Config.TLS_CertificateKey != "" {
|
||||||
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
||||||
err := server.ListenAndServeTLS(
|
err := server.ListenAndServeTLS(
|
||||||
s.config.TLS_CertificatePath,
|
config.Config.TLS_CertificatePath,
|
||||||
s.config.TLS_CertificateKey)
|
config.Config.TLS_CertificateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to start HTTPS server:", err)
|
logger.Error("Failed to start HTTPS server:", err)
|
||||||
}
|
}
|
||||||
s.isActive = false
|
return
|
||||||
} else {
|
} else {
|
||||||
tlsConfig := tlsprovider.GetDefaultTlsConfig()
|
tlsConfig := tlsprovider.GetDefaultTlsConfig()
|
||||||
server.TLSConfig = tlsConfig
|
server.TLSConfig = tlsConfig
|
||||||
@ -108,7 +113,9 @@ func (s *ApiServer) Start() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to start HTTPS server:", err)
|
logger.Error("Failed to start HTTPS server:", err)
|
||||||
}
|
}
|
||||||
s.isActive = false
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
return &Server{StopServer: stopChan}
|
||||||
}
|
}
|
||||||
|
@ -11,15 +11,16 @@ import (
|
|||||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Authentication(t *testing.T) {
|
func Test_Authentication(t *testing.T) {
|
||||||
ts := runTestServer()
|
ts := runTestServer()
|
||||||
defer ts.Server.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
t.Run("Should get 200 when correct account key is used", func(t *testing.T) {
|
t.Run("Should get 200 when correct account key is used", func(t *testing.T) {
|
||||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
repositories.DeleteDatabase(testDatabaseName)
|
||||||
client, err := azcosmos.NewClientFromConnectionString(
|
client, err := azcosmos.NewClientFromConnectionString(
|
||||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||||
&azcosmos.ClientOptions{},
|
&azcosmos.ClientOptions{},
|
||||||
@ -34,8 +35,26 @@ func Test_Authentication(t *testing.T) {
|
|||||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
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) {
|
t.Run("Should get 401 when wrong account key is used", func(t *testing.T) {
|
||||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
repositories.DeleteDatabase(testDatabaseName)
|
||||||
client, err := azcosmos.NewClientFromConnectionString(
|
client, err := azcosmos.NewClientFromConnectionString(
|
||||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
||||||
&azcosmos.ClientOptions{},
|
&azcosmos.ClientOptions{},
|
||||||
@ -66,29 +85,3 @@ func Test_Authentication(t *testing.T) {
|
|||||||
assert.Contains(t, string(responseBody), "BACKEND_ENDPOINT")
|
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,21 +10,22 @@ import (
|
|||||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Collections(t *testing.T) {
|
func Test_Collections(t *testing.T) {
|
||||||
ts := runTestServer()
|
ts := runTestServer()
|
||||||
defer ts.Server.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
client, err := azcosmos.NewClientFromConnectionString(
|
client, err := azcosmos.NewClientFromConnectionString(
|
||||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||||
&azcosmos.ClientOptions{},
|
&azcosmos.ClientOptions{},
|
||||||
)
|
)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||||
databaseClient, err := client.NewDatabase(testDatabaseName)
|
databaseClient, err := client.NewDatabase(testDatabaseName)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ func Test_Collections(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return conflict when collection exists", func(t *testing.T) {
|
t.Run("Should return conflict when collection exists", func(t *testing.T) {
|
||||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||||
ID: testCollectionName,
|
ID: testCollectionName,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -59,7 +60,7 @@ func Test_Collections(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("Collection Read", func(t *testing.T) {
|
t.Run("Collection Read", func(t *testing.T) {
|
||||||
t.Run("Should read collection", func(t *testing.T) {
|
t.Run("Should read collection", func(t *testing.T) {
|
||||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||||
ID: testCollectionName,
|
ID: testCollectionName,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ func Test_Collections(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||||
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
|
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||||
|
|
||||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@ -92,7 +93,7 @@ func Test_Collections(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("Collection Delete", func(t *testing.T) {
|
t.Run("Collection Delete", func(t *testing.T) {
|
||||||
t.Run("Should delete collection", func(t *testing.T) {
|
t.Run("Should delete collection", func(t *testing.T) {
|
||||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||||
ID: testCollectionName,
|
ID: testCollectionName,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -105,7 +106,7 @@ func Test_Collections(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||||
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
|
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||||
|
|
||||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -5,37 +5,14 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/api"
|
"github.com/pikami/cosmium/api"
|
||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
"github.com/pikami/cosmium/internal/repositories"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestServer struct {
|
func runTestServer() *httptest.Server {
|
||||||
Server *httptest.Server
|
config.Config.AccountKey = config.DefaultAccountKey
|
||||||
Repository *repositories.DataRepository
|
config.Config.ExplorerPath = "/tmp/nothing"
|
||||||
URL string
|
config.Config.ExplorerBaseUrlLocation = config.ExplorerBaseUrlLocation
|
||||||
}
|
|
||||||
|
|
||||||
func runTestServerCustomConfig(config config.ServerConfig) *TestServer {
|
return httptest.NewServer(api.CreateRouter())
|
||||||
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 (
|
const (
|
||||||
|
@ -10,23 +10,24 @@ import (
|
|||||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Databases(t *testing.T) {
|
func Test_Databases(t *testing.T) {
|
||||||
ts := runTestServer()
|
ts := runTestServer()
|
||||||
defer ts.Server.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
client, err := azcosmos.NewClientFromConnectionString(
|
client, err := azcosmos.NewClientFromConnectionString(
|
||||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||||
&azcosmos.ClientOptions{},
|
&azcosmos.ClientOptions{},
|
||||||
)
|
)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
t.Run("Database Create", func(t *testing.T) {
|
t.Run("Database Create", func(t *testing.T) {
|
||||||
t.Run("Should create database", func(t *testing.T) {
|
t.Run("Should create database", func(t *testing.T) {
|
||||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
repositories.DeleteDatabase(testDatabaseName)
|
||||||
|
|
||||||
createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
|
createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
|
||||||
ID: testDatabaseName,
|
ID: testDatabaseName,
|
||||||
@ -37,7 +38,7 @@ func Test_Databases(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return conflict when database exists", func(t *testing.T) {
|
t.Run("Should return conflict when database exists", func(t *testing.T) {
|
||||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
repositories.CreateDatabase(repositorymodels.Database{
|
||||||
ID: testDatabaseName,
|
ID: testDatabaseName,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -57,7 +58,7 @@ func Test_Databases(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("Database Read", func(t *testing.T) {
|
t.Run("Database Read", func(t *testing.T) {
|
||||||
t.Run("Should read database", func(t *testing.T) {
|
t.Run("Should read database", func(t *testing.T) {
|
||||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
repositories.CreateDatabase(repositorymodels.Database{
|
||||||
ID: testDatabaseName,
|
ID: testDatabaseName,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ func Test_Databases(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
repositories.DeleteDatabase(testDatabaseName)
|
||||||
|
|
||||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@ -90,7 +91,7 @@ func Test_Databases(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("Database Delete", func(t *testing.T) {
|
t.Run("Database Delete", func(t *testing.T) {
|
||||||
t.Run("Should delete database", func(t *testing.T) {
|
t.Run("Should delete database", func(t *testing.T) {
|
||||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
repositories.CreateDatabase(repositorymodels.Database{
|
||||||
ID: testDatabaseName,
|
ID: testDatabaseName,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -103,7 +104,7 @@ func Test_Databases(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
repositories.DeleteDatabase(testDatabaseName)
|
||||||
|
|
||||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@ -14,6 +15,7 @@ import (
|
|||||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@ -53,11 +55,9 @@ func testCosmosQuery(t *testing.T,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClient) {
|
func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.ContainerClient) {
|
||||||
ts := runTestServer()
|
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||||
|
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||||
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
|
||||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
|
||||||
ID: testCollectionName,
|
ID: testCollectionName,
|
||||||
PartitionKey: struct {
|
PartitionKey: struct {
|
||||||
Paths []string "json:\"paths\""
|
Paths []string "json:\"paths\""
|
||||||
@ -67,11 +67,13 @@ func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClien
|
|||||||
Paths: []string{"/pk"},
|
Paths: []string{"/pk"},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
ts.Repository.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": "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}})
|
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
|
||||||
|
|
||||||
|
ts := runTestServer()
|
||||||
|
|
||||||
client, err := azcosmos.NewClientFromConnectionString(
|
client, err := azcosmos.NewClientFromConnectionString(
|
||||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||||
&azcosmos.ClientOptions{},
|
&azcosmos.ClientOptions{},
|
||||||
)
|
)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@ -84,7 +86,7 @@ func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClien
|
|||||||
|
|
||||||
func Test_Documents(t *testing.T) {
|
func Test_Documents(t *testing.T) {
|
||||||
ts, collectionClient := documents_InitializeDb(t)
|
ts, collectionClient := documents_InitializeDb(t)
|
||||||
defer ts.Server.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
t.Run("Should query document", func(t *testing.T) {
|
t.Run("Should query document", func(t *testing.T) {
|
||||||
testCosmosQuery(t, collectionClient,
|
testCosmosQuery(t, collectionClient,
|
||||||
@ -216,7 +218,7 @@ func Test_Documents(t *testing.T) {
|
|||||||
|
|
||||||
func Test_Documents_Patch(t *testing.T) {
|
func Test_Documents_Patch(t *testing.T) {
|
||||||
ts, collectionClient := documents_InitializeDb(t)
|
ts, collectionClient := documents_InitializeDb(t)
|
||||||
defer ts.Server.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
t.Run("Should PATCH document", func(t *testing.T) {
|
t.Run("Should PATCH document", func(t *testing.T) {
|
||||||
context := context.TODO()
|
context := context.TODO()
|
||||||
|
@ -15,14 +15,14 @@ import (
|
|||||||
// Request document with trailing slash like python cosmosdb client does.
|
// Request document with trailing slash like python cosmosdb client does.
|
||||||
func Test_Documents_Read_Trailing_Slash(t *testing.T) {
|
func Test_Documents_Read_Trailing_Slash(t *testing.T) {
|
||||||
ts, _ := documents_InitializeDb(t)
|
ts, _ := documents_InitializeDb(t)
|
||||||
defer ts.Server.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
|
t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
|
||||||
resourceIdTemplate := "dbs/%s/colls/%s/docs/%s"
|
resourceIdTemplate := "dbs/%s/colls/%s/docs/%s"
|
||||||
path := fmt.Sprintf(resourceIdTemplate, testDatabaseName, testCollectionName, "12345")
|
path := fmt.Sprintf(resourceIdTemplate, testDatabaseName, testCollectionName, "12345")
|
||||||
testUrl := ts.URL + "/" + path + "/"
|
testUrl := ts.URL + "/" + path + "/"
|
||||||
date := time.Now().Format(time.RFC1123)
|
date := time.Now().Format(time.RFC1123)
|
||||||
signature := authentication.GenerateSignature("GET", "docs", path, date, config.DefaultAccountKey)
|
signature := authentication.GenerateSignature("GET", "docs", path, date, config.Config.AccountKey)
|
||||||
httpClient := &http.Client{}
|
httpClient := &http.Client{}
|
||||||
req, _ := http.NewRequest("GET", testUrl, nil)
|
req, _ := http.NewRequest("GET", testUrl, nil)
|
||||||
req.Header.Add("x-ms-date", date)
|
req.Header.Add("x-ms-date", date)
|
||||||
|
@ -11,20 +11,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configuration := config.ParseFlags()
|
config.ParseFlags()
|
||||||
|
|
||||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
repositories.InitializeRepository()
|
||||||
InitialDataFilePath: configuration.InitialDataFilePath,
|
|
||||||
PersistDataFilePath: configuration.PersistDataFilePath,
|
|
||||||
})
|
|
||||||
|
|
||||||
server := api.NewApiServer(repository, configuration)
|
server := api.StartAPI()
|
||||||
server.Start()
|
|
||||||
|
|
||||||
waitForExit(server, repository, configuration)
|
waitForExit(server)
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitForExit(server *api.ApiServer, repository *repositories.DataRepository, config config.ServerConfig) {
|
func waitForExit(server *api.Server) {
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
@ -32,9 +28,9 @@ func waitForExit(server *api.ApiServer, repository *repositories.DataRepository,
|
|||||||
<-sigs
|
<-sigs
|
||||||
|
|
||||||
// Stop the server
|
// Stop the server
|
||||||
server.Stop()
|
server.StopServer <- true
|
||||||
|
|
||||||
if config.PersistDataFilePath != "" {
|
if config.Config.PersistDataFilePath != "" {
|
||||||
repository.SaveStateFS(config.PersistDataFilePath)
|
repositories.SaveStateFS(config.Config.PersistDataFilePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,22 +3,22 @@ package logger
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
|
||||||
|
|
||||||
var EnableDebugOutput = false
|
"github.com/pikami/cosmium/api/config"
|
||||||
|
)
|
||||||
|
|
||||||
var DebugLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
|
var DebugLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
var InfoLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
var InfoLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||||
var ErrorLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
|
var ErrorLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
func Debug(v ...any) {
|
func Debug(v ...any) {
|
||||||
if EnableDebugOutput {
|
if config.Config.Debug {
|
||||||
DebugLogger.Println(v...)
|
DebugLogger.Println(v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Debugf(format string, v ...any) {
|
func Debugf(format string, v ...any) {
|
||||||
if EnableDebugOutput {
|
if config.Config.Debug {
|
||||||
DebugLogger.Printf(format, v...)
|
DebugLogger.Printf(format, v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,60 +11,60 @@ import (
|
|||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *DataRepository) GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||||
return make([]repositorymodels.Collection, 0), repositorymodels.StatusNotFound
|
return make([]repositorymodels.Collection, 0), repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return maps.Values(r.storeState.Collections[databaseId]), repositorymodels.StatusOk
|
return maps.Values(storeState.Collections[databaseId]), repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) GetCollection(databaseId string, collectionId string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
func GetCollection(databaseId string, collectionId string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.storeState.Collections[databaseId][collectionId], repositorymodels.StatusOk
|
return storeState.Collections[databaseId][collectionId], repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) DeleteCollection(databaseId string, collectionId string) repositorymodels.RepositoryStatus {
|
func DeleteCollection(databaseId string, collectionId string) repositorymodels.RepositoryStatus {
|
||||||
r.storeState.Lock()
|
storeState.Lock()
|
||||||
defer r.storeState.Unlock()
|
defer storeState.Unlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||||
return repositorymodels.StatusNotFound
|
return repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||||
return repositorymodels.StatusNotFound
|
return repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(r.storeState.Collections[databaseId], collectionId)
|
delete(storeState.Collections[databaseId], collectionId)
|
||||||
|
|
||||||
return repositorymodels.StatusOk
|
return repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.Lock()
|
storeState.Lock()
|
||||||
defer r.storeState.Unlock()
|
defer storeState.Unlock()
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
var database repositorymodels.Database
|
var database repositorymodels.Database
|
||||||
if database, ok = r.storeState.Databases[databaseId]; !ok {
|
if database, ok = storeState.Databases[databaseId]; !ok {
|
||||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok = r.storeState.Collections[databaseId][newCollection.ID]; ok {
|
if _, ok = storeState.Collections[databaseId][newCollection.ID]; ok {
|
||||||
return repositorymodels.Collection{}, repositorymodels.Conflict
|
return repositorymodels.Collection{}, repositorymodels.Conflict
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,8 +75,8 @@ func (r *DataRepository) CreateCollection(databaseId string, newCollection repos
|
|||||||
newCollection.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
newCollection.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
newCollection.Self = fmt.Sprintf("dbs/%s/colls/%s/", database.ResourceID, newCollection.ResourceID)
|
newCollection.Self = fmt.Sprintf("dbs/%s/colls/%s/", database.ResourceID, newCollection.ResourceID)
|
||||||
|
|
||||||
r.storeState.Collections[databaseId][newCollection.ID] = newCollection
|
storeState.Collections[databaseId][newCollection.ID] = newCollection
|
||||||
r.storeState.Documents[databaseId][newCollection.ID] = make(map[string]repositorymodels.Document)
|
storeState.Documents[databaseId][newCollection.ID] = make(map[string]repositorymodels.Document)
|
||||||
|
|
||||||
return newCollection, repositorymodels.StatusOk
|
return newCollection, repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
@ -10,42 +10,42 @@ import (
|
|||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *DataRepository) GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
func GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
return maps.Values(r.storeState.Databases), repositorymodels.StatusOk
|
return maps.Values(storeState.Databases), repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
func GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
if database, ok := r.storeState.Databases[id]; ok {
|
if database, ok := storeState.Databases[id]; ok {
|
||||||
return database, repositorymodels.StatusOk
|
return database, repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
return repositorymodels.Database{}, repositorymodels.StatusNotFound
|
return repositorymodels.Database{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
||||||
r.storeState.Lock()
|
storeState.Lock()
|
||||||
defer r.storeState.Unlock()
|
defer storeState.Unlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[id]; !ok {
|
if _, ok := storeState.Databases[id]; !ok {
|
||||||
return repositorymodels.StatusNotFound
|
return repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(r.storeState.Databases, id)
|
delete(storeState.Databases, id)
|
||||||
|
|
||||||
return repositorymodels.StatusOk
|
return repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
func CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.Lock()
|
storeState.Lock()
|
||||||
defer r.storeState.Unlock()
|
defer storeState.Unlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[newDatabase.ID]; ok {
|
if _, ok := storeState.Databases[newDatabase.ID]; ok {
|
||||||
return repositorymodels.Database{}, repositorymodels.Conflict
|
return repositorymodels.Database{}, repositorymodels.Conflict
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,9 +54,9 @@ func (r *DataRepository) CreateDatabase(newDatabase repositorymodels.Database) (
|
|||||||
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
|
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
|
||||||
|
|
||||||
r.storeState.Databases[newDatabase.ID] = newDatabase
|
storeState.Databases[newDatabase.ID] = newDatabase
|
||||||
r.storeState.Collections[newDatabase.ID] = make(map[string]repositorymodels.Collection)
|
storeState.Collections[newDatabase.ID] = make(map[string]repositorymodels.Collection)
|
||||||
r.storeState.Documents[newDatabase.ID] = make(map[string]map[string]repositorymodels.Document)
|
storeState.Documents[newDatabase.ID] = make(map[string]map[string]repositorymodels.Document)
|
||||||
|
|
||||||
return newDatabase, repositorymodels.StatusOk
|
return newDatabase, repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
@ -14,64 +14,64 @@ import (
|
|||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *DataRepository) GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
func GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||||
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
|
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||||
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
|
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return maps.Values(r.storeState.Documents[databaseId][collectionId]), repositorymodels.StatusOk
|
return maps.Values(storeState.Documents[databaseId][collectionId]), repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
func GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.storeState.Documents[databaseId][collectionId][documentId], repositorymodels.StatusOk
|
return storeState.Documents[databaseId][collectionId][documentId], repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
|
func DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
|
||||||
r.storeState.Lock()
|
storeState.Lock()
|
||||||
defer r.storeState.Unlock()
|
defer storeState.Unlock()
|
||||||
|
|
||||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||||
return repositorymodels.StatusNotFound
|
return repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||||
return repositorymodels.StatusNotFound
|
return repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||||
return repositorymodels.StatusNotFound
|
return repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(r.storeState.Documents[databaseId][collectionId], documentId)
|
delete(storeState.Documents[databaseId][collectionId], documentId)
|
||||||
|
|
||||||
return repositorymodels.StatusOk
|
return repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.Lock()
|
storeState.Lock()
|
||||||
defer r.storeState.Unlock()
|
defer storeState.Unlock()
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
var documentId string
|
var documentId string
|
||||||
@ -82,15 +82,15 @@ func (r *DataRepository) CreateDocument(databaseId string, collectionId string,
|
|||||||
document["id"] = documentId
|
document["id"] = documentId
|
||||||
}
|
}
|
||||||
|
|
||||||
if database, ok = r.storeState.Databases[databaseId]; !ok {
|
if database, ok = storeState.Databases[databaseId]; !ok {
|
||||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
|
if collection, ok = storeState.Collections[databaseId][collectionId]; !ok {
|
||||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; ok {
|
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; ok {
|
||||||
return repositorymodels.Document{}, repositorymodels.Conflict
|
return repositorymodels.Document{}, repositorymodels.Conflict
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,19 +99,19 @@ func (r *DataRepository) CreateDocument(databaseId string, collectionId string,
|
|||||||
document["_etag"] = fmt.Sprintf("\"%s\"", uuid.New())
|
document["_etag"] = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
document["_self"] = fmt.Sprintf("dbs/%s/colls/%s/docs/%s/", database.ResourceID, collection.ResourceID, document["_rid"])
|
document["_self"] = fmt.Sprintf("dbs/%s/colls/%s/docs/%s/", database.ResourceID, collection.ResourceID, document["_rid"])
|
||||||
|
|
||||||
r.storeState.Documents[databaseId][collectionId][documentId] = document
|
storeState.Documents[databaseId][collectionId][documentId] = document
|
||||||
|
|
||||||
return document, repositorymodels.StatusOk
|
return document, repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) ExecuteQueryDocuments(databaseId string, collectionId string, query string, queryParameters map[string]interface{}) ([]memoryexecutor.RowType, repositorymodels.RepositoryStatus) {
|
func ExecuteQueryDocuments(databaseId string, collectionId string, query string, queryParameters map[string]interface{}) ([]memoryexecutor.RowType, repositorymodels.RepositoryStatus) {
|
||||||
parsedQuery, err := nosql.Parse("", []byte(query))
|
parsedQuery, err := nosql.Parse("", []byte(query))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to parse query: %s\nerr: %v", query, err)
|
log.Printf("Failed to parse query: %s\nerr: %v", query, err)
|
||||||
return nil, repositorymodels.BadRequest
|
return nil, repositorymodels.BadRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
collectionDocuments, status := r.GetAllDocuments(databaseId, collectionId)
|
collectionDocuments, status := GetAllDocuments(databaseId, collectionId)
|
||||||
if status != repositorymodels.StatusOk {
|
if status != repositorymodels.StatusOk {
|
||||||
return nil, status
|
return nil, status
|
||||||
}
|
}
|
||||||
|
@ -9,19 +9,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// I have no idea what this is tbh
|
// I have no idea what this is tbh
|
||||||
func (r *DataRepository) GetPartitionKeyRanges(databaseId string, collectionId string) ([]repositorymodels.PartitionKeyRange, repositorymodels.RepositoryStatus) {
|
func GetPartitionKeyRanges(databaseId string, collectionId string) ([]repositorymodels.PartitionKeyRange, repositorymodels.RepositoryStatus) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
databaseRid := databaseId
|
databaseRid := databaseId
|
||||||
collectionRid := collectionId
|
collectionRid := collectionId
|
||||||
var timestamp int64 = 0
|
var timestamp int64 = 0
|
||||||
|
|
||||||
if database, ok := r.storeState.Databases[databaseId]; !ok {
|
if database, ok := storeState.Databases[databaseId]; !ok {
|
||||||
databaseRid = database.ResourceID
|
databaseRid = database.ResourceID
|
||||||
}
|
}
|
||||||
|
|
||||||
if collection, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
if collection, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||||
collectionRid = collection.ResourceID
|
collectionRid = collection.ResourceID
|
||||||
timestamp = collection.TimeStamp
|
timestamp = collection.TimeStamp
|
||||||
}
|
}
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
package repositories
|
|
||||||
|
|
||||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
|
||||||
|
|
||||||
type DataRepository struct {
|
|
||||||
storedProcedures []repositorymodels.StoredProcedure
|
|
||||||
triggers []repositorymodels.Trigger
|
|
||||||
userDefinedFunctions []repositorymodels.UserDefinedFunction
|
|
||||||
storeState repositorymodels.State
|
|
||||||
|
|
||||||
initialDataFilePath string
|
|
||||||
persistDataFilePath string
|
|
||||||
}
|
|
||||||
|
|
||||||
type RepositoryOptions struct {
|
|
||||||
InitialDataFilePath string
|
|
||||||
PersistDataFilePath string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDataRepository(options RepositoryOptions) *DataRepository {
|
|
||||||
repository := &DataRepository{
|
|
||||||
storedProcedures: []repositorymodels.StoredProcedure{},
|
|
||||||
triggers: []repositorymodels.Trigger{},
|
|
||||||
userDefinedFunctions: []repositorymodels.UserDefinedFunction{},
|
|
||||||
storeState: repositorymodels.State{
|
|
||||||
Databases: make(map[string]repositorymodels.Database),
|
|
||||||
Collections: make(map[string]map[string]repositorymodels.Collection),
|
|
||||||
Documents: make(map[string]map[string]map[string]repositorymodels.Document),
|
|
||||||
},
|
|
||||||
initialDataFilePath: options.InitialDataFilePath,
|
|
||||||
persistDataFilePath: options.PersistDataFilePath,
|
|
||||||
}
|
|
||||||
|
|
||||||
repository.InitializeRepository()
|
|
||||||
|
|
||||||
return repository
|
|
||||||
}
|
|
@ -6,18 +6,28 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/pikami/cosmium/api/config"
|
||||||
"github.com/pikami/cosmium/internal/logger"
|
"github.com/pikami/cosmium/internal/logger"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *DataRepository) InitializeRepository() {
|
var storedProcedures = []repositorymodels.StoredProcedure{}
|
||||||
if r.initialDataFilePath != "" {
|
var triggers = []repositorymodels.Trigger{}
|
||||||
r.LoadStateFS(r.initialDataFilePath)
|
var userDefinedFunctions = []repositorymodels.UserDefinedFunction{}
|
||||||
|
var storeState = repositorymodels.State{
|
||||||
|
Databases: make(map[string]repositorymodels.Database),
|
||||||
|
Collections: make(map[string]map[string]repositorymodels.Collection),
|
||||||
|
Documents: make(map[string]map[string]map[string]repositorymodels.Document),
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitializeRepository() {
|
||||||
|
if config.Config.InitialDataFilePath != "" {
|
||||||
|
LoadStateFS(config.Config.InitialDataFilePath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.persistDataFilePath != "" {
|
if config.Config.PersistDataFilePath != "" {
|
||||||
stat, err := os.Stat(r.persistDataFilePath)
|
stat, err := os.Stat(config.Config.PersistDataFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -27,12 +37,12 @@ func (r *DataRepository) InitializeRepository() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.LoadStateFS(r.persistDataFilePath)
|
LoadStateFS(config.Config.PersistDataFilePath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) LoadStateFS(filePath string) {
|
func LoadStateFS(filePath string) {
|
||||||
data, err := os.ReadFile(filePath)
|
data, err := os.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error reading state JSON file: %v", err)
|
log.Fatalf("Error reading state JSON file: %v", err)
|
||||||
@ -50,16 +60,16 @@ func (r *DataRepository) LoadStateFS(filePath string) {
|
|||||||
logger.Infof("Collections: %d\n", getLength(state.Collections))
|
logger.Infof("Collections: %d\n", getLength(state.Collections))
|
||||||
logger.Infof("Documents: %d\n", getLength(state.Documents))
|
logger.Infof("Documents: %d\n", getLength(state.Documents))
|
||||||
|
|
||||||
r.storeState = state
|
storeState = state
|
||||||
|
|
||||||
r.ensureStoreStateNoNullReferences()
|
ensureStoreStateNoNullReferences()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) SaveStateFS(filePath string) {
|
func SaveStateFS(filePath string) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
data, err := json.MarshalIndent(r.storeState, "", "\t")
|
data, err := json.MarshalIndent(storeState, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Failed to save state: %v\n", err)
|
logger.Errorf("Failed to save state: %v\n", err)
|
||||||
return
|
return
|
||||||
@ -68,16 +78,16 @@ func (r *DataRepository) SaveStateFS(filePath string) {
|
|||||||
os.WriteFile(filePath, data, os.ModePerm)
|
os.WriteFile(filePath, data, os.ModePerm)
|
||||||
|
|
||||||
logger.Info("Saved state:")
|
logger.Info("Saved state:")
|
||||||
logger.Infof("Databases: %d\n", getLength(r.storeState.Databases))
|
logger.Infof("Databases: %d\n", getLength(storeState.Databases))
|
||||||
logger.Infof("Collections: %d\n", getLength(r.storeState.Collections))
|
logger.Infof("Collections: %d\n", getLength(storeState.Collections))
|
||||||
logger.Infof("Documents: %d\n", getLength(r.storeState.Documents))
|
logger.Infof("Documents: %d\n", getLength(storeState.Documents))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) GetState() (string, error) {
|
func GetState() (string, error) {
|
||||||
r.storeState.RLock()
|
storeState.RLock()
|
||||||
defer r.storeState.RUnlock()
|
defer storeState.RUnlock()
|
||||||
|
|
||||||
data, err := json.MarshalIndent(r.storeState, "", "\t")
|
data, err := json.MarshalIndent(storeState, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Failed to serialize state: %v\n", err)
|
logger.Errorf("Failed to serialize state: %v\n", err)
|
||||||
return "", err
|
return "", err
|
||||||
@ -111,36 +121,36 @@ func getLength(v interface{}) int {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) ensureStoreStateNoNullReferences() {
|
func ensureStoreStateNoNullReferences() {
|
||||||
if r.storeState.Databases == nil {
|
if storeState.Databases == nil {
|
||||||
r.storeState.Databases = make(map[string]repositorymodels.Database)
|
storeState.Databases = make(map[string]repositorymodels.Database)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.storeState.Collections == nil {
|
if storeState.Collections == nil {
|
||||||
r.storeState.Collections = make(map[string]map[string]repositorymodels.Collection)
|
storeState.Collections = make(map[string]map[string]repositorymodels.Collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.storeState.Documents == nil {
|
if storeState.Documents == nil {
|
||||||
r.storeState.Documents = make(map[string]map[string]map[string]repositorymodels.Document)
|
storeState.Documents = make(map[string]map[string]map[string]repositorymodels.Document)
|
||||||
}
|
}
|
||||||
|
|
||||||
for database := range r.storeState.Databases {
|
for database := range storeState.Databases {
|
||||||
if r.storeState.Collections[database] == nil {
|
if storeState.Collections[database] == nil {
|
||||||
r.storeState.Collections[database] = make(map[string]repositorymodels.Collection)
|
storeState.Collections[database] = make(map[string]repositorymodels.Collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.storeState.Documents[database] == nil {
|
if storeState.Documents[database] == nil {
|
||||||
r.storeState.Documents[database] = make(map[string]map[string]repositorymodels.Document)
|
storeState.Documents[database] = make(map[string]map[string]repositorymodels.Document)
|
||||||
}
|
}
|
||||||
|
|
||||||
for collection := range r.storeState.Collections[database] {
|
for collection := range storeState.Collections[database] {
|
||||||
if r.storeState.Documents[database][collection] == nil {
|
if storeState.Documents[database][collection] == nil {
|
||||||
r.storeState.Documents[database][collection] = make(map[string]repositorymodels.Document)
|
storeState.Documents[database][collection] = make(map[string]repositorymodels.Document)
|
||||||
}
|
}
|
||||||
|
|
||||||
for document := range r.storeState.Documents[database][collection] {
|
for document := range storeState.Documents[database][collection] {
|
||||||
if r.storeState.Documents[database][collection][document] == nil {
|
if storeState.Documents[database][collection][document] == nil {
|
||||||
delete(r.storeState.Documents[database][collection], document)
|
delete(storeState.Documents[database][collection], document)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,6 @@ package repositories
|
|||||||
|
|
||||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
|
|
||||||
func (r *DataRepository) GetAllStoredProcedures(databaseId string, collectionId string) ([]repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
|
func GetAllStoredProcedures(databaseId string, collectionId string) ([]repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
|
||||||
return r.storedProcedures, repositorymodels.StatusOk
|
return storedProcedures, repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,6 @@ package repositories
|
|||||||
|
|
||||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
|
|
||||||
func (r *DataRepository) GetAllTriggers(databaseId string, collectionId string) ([]repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
|
func GetAllTriggers(databaseId string, collectionId string) ([]repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
|
||||||
return r.triggers, repositorymodels.StatusOk
|
return triggers, repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,6 @@ package repositories
|
|||||||
|
|
||||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
|
|
||||||
func (r *DataRepository) GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
|
func GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
|
||||||
return r.userDefinedFunctions, repositorymodels.StatusOk
|
return userDefinedFunctions, repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
|
@ -9,80 +9,44 @@ import (
|
|||||||
"github.com/pikami/cosmium/internal/repositories"
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerInstance struct {
|
var currentServer *api.Server
|
||||||
server *api.ApiServer
|
|
||||||
repository *repositories.DataRepository
|
|
||||||
}
|
|
||||||
|
|
||||||
var serverInstances map[string]*ServerInstance
|
|
||||||
|
|
||||||
const (
|
|
||||||
ResponseSuccess = 0
|
|
||||||
ResponseUnknown = 1
|
|
||||||
ResponseServerInstanceAlreadyExists = 2
|
|
||||||
ResponseFailedToParseConfiguration = 3
|
|
||||||
ResponseServerInstanceNotFound = 4
|
|
||||||
)
|
|
||||||
|
|
||||||
//export CreateServerInstance
|
|
||||||
func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
|
|
||||||
configStr := C.GoString(configurationJSON)
|
|
||||||
serverNameStr := C.GoString(serverName)
|
|
||||||
|
|
||||||
if serverInstances == nil {
|
|
||||||
serverInstances = make(map[string]*ServerInstance)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := serverInstances[serverNameStr]; ok {
|
|
||||||
return ResponseServerInstanceAlreadyExists
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//export Configure
|
||||||
|
func Configure(configurationJSON *C.char) bool {
|
||||||
var configuration config.ServerConfig
|
var configuration config.ServerConfig
|
||||||
err := json.Unmarshal([]byte(configStr), &configuration)
|
err := json.Unmarshal([]byte(C.GoString(configurationJSON)), &configuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ResponseFailedToParseConfiguration
|
return false
|
||||||
}
|
}
|
||||||
|
config.Config = configuration
|
||||||
configuration.PopulateCalculatedFields()
|
return true
|
||||||
|
|
||||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
|
||||||
InitialDataFilePath: configuration.InitialDataFilePath,
|
|
||||||
PersistDataFilePath: configuration.PersistDataFilePath,
|
|
||||||
})
|
|
||||||
|
|
||||||
server := api.NewApiServer(repository, configuration)
|
|
||||||
server.Start()
|
|
||||||
|
|
||||||
serverInstances[serverNameStr] = &ServerInstance{
|
|
||||||
server: server,
|
|
||||||
repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export StopServerInstance
|
//export InitializeRepository
|
||||||
func StopServerInstance(serverName *C.char) int {
|
func InitializeRepository() {
|
||||||
if serverInstance, ok := serverInstances[C.GoString(serverName)]; ok {
|
repositories.InitializeRepository()
|
||||||
serverInstance.server.Stop()
|
|
||||||
delete(serverInstances, C.GoString(serverName))
|
|
||||||
return ResponseSuccess
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseServerInstanceNotFound
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export GetServerInstanceState
|
//export StartAPI
|
||||||
func GetServerInstanceState(serverName *C.char) *C.char {
|
func StartAPI() {
|
||||||
if serverInstance, ok := serverInstances[C.GoString(serverName)]; ok {
|
currentServer = api.StartAPI()
|
||||||
stateJSON, err := serverInstance.repository.GetState()
|
}
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return C.CString(stateJSON)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
//export StopAPI
|
||||||
|
func StopAPI() {
|
||||||
|
if currentServer == nil {
|
||||||
|
currentServer.StopServer <- true
|
||||||
|
currentServer = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//export GetState
|
||||||
|
func GetState() *C.char {
|
||||||
|
stateJSON, err := repositories.GetState()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return C.CString(stateJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user