Expose repository functions to sharedlibs

This commit is contained in:
Pijus Kamandulis
2024-12-20 20:25:32 +02:00
parent 363f822e5a
commit bcf4b513b6
10 changed files with 484 additions and 41 deletions

View File

@@ -9,32 +9,12 @@ import (
"github.com/pikami/cosmium/internal/repositories"
)
type ServerInstance struct {
server *api.ApiServer
repository *repositories.DataRepository
}
var serverInstances map[string]*ServerInstance
const (
ResponseSuccess = 0
ResponseUnknown = 1
ResponseServerInstanceAlreadyExists = 2
ResponseFailedToParseConfiguration = 3
ResponseServerInstanceNotFound = 4
ResponseFailedToLoadState = 5
)
//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 {
if _, ok := getInstance(serverNameStr); ok {
return ResponseServerInstanceAlreadyExists
}
@@ -55,19 +35,21 @@ func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
server := api.NewApiServer(repository, configuration)
server.Start()
serverInstances[serverNameStr] = &ServerInstance{
addInstance(serverNameStr, &ServerInstance{
server: server,
repository: repository,
}
})
return ResponseSuccess
}
//export StopServerInstance
func StopServerInstance(serverName *C.char) int {
if serverInstance, ok := serverInstances[C.GoString(serverName)]; ok {
serverNameStr := C.GoString(serverName)
if serverInstance, ok := getInstance(serverNameStr); ok {
serverInstance.server.Stop()
delete(serverInstances, C.GoString(serverName))
removeInstance(serverNameStr)
return ResponseSuccess
}
@@ -76,7 +58,9 @@ func StopServerInstance(serverName *C.char) int {
//export GetServerInstanceState
func GetServerInstanceState(serverName *C.char) *C.char {
if serverInstance, ok := serverInstances[C.GoString(serverName)]; ok {
serverNameStr := C.GoString(serverName)
if serverInstance, ok := getInstance(serverNameStr); ok {
stateJSON, err := serverInstance.repository.GetState()
if err != nil {
return nil
@@ -92,7 +76,7 @@ func LoadServerInstanceState(serverName *C.char, stateJSON *C.char) int {
serverNameStr := C.GoString(serverName)
stateJSONStr := C.GoString(stateJSON)
if serverInstance, ok := serverInstances[serverNameStr]; ok {
if serverInstance, ok := getInstance(serverNameStr); ok {
err := serverInstance.repository.LoadStateJSON(stateJSONStr)
if err != nil {
return ResponseFailedToLoadState