Implement Genesis endpoints
@@ -3,12 +3,17 @@ package restapi
import (
"crypto/tls"
"net/http"
+ "time"
+
+ "fmt"
errors "github.com/go-openapi/errors"
runtime "github.com/go-openapi/runtime"
middleware "github.com/go-openapi/runtime/middleware"
+ "github.com/creativesoftwarefdn/weaviate/genesis/models"
"github.com/creativesoftwarefdn/weaviate/genesis/restapi/operations"
+ libstate "github.com/creativesoftwarefdn/weaviate/genesis/state"
log "github.com/sirupsen/logrus"
)
@@ -19,7 +24,14 @@ func configureFlags(api *operations.WeaviateGenesisAPI) {
// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
}
+var state libstate.State
+
func configureAPI(api *operations.WeaviateGenesisAPI) http.Handler {
+ log.SetLevel(log.DebugLevel)
+
+ state = libstate.NewInMemoryState()
+ log.Info("Created in memory state")
+
// configure the api here
api.ServeError = errors.ServeError
@@ -34,13 +46,66 @@ func configureAPI(api *operations.WeaviateGenesisAPI) http.Handler {
api.JSONProducer = runtime.JSONProducer()
api.GenesisPeersLeaveHandler = operations.GenesisPeersLeaveHandlerFunc(func(params operations.GenesisPeersLeaveParams) middleware.Responder {
- return middleware.NotImplemented("operation .GenesisPeersLeave has not yet been implemented")
+ err := (state).RemovePeer(string(params.PeerID))
+
+ if err == nil {
+ return operations.NewGenesisPeersLeaveNoContent()
+ } else {
+ return operations.NewGenesisPeersLeaveNotFound()
+ }
})
+
api.GenesisPeersPingHandler = operations.GenesisPeersPingHandlerFunc(func(params operations.GenesisPeersPingParams) middleware.Responder {
- return middleware.NotImplemented("operation .GenesisPeersPing has not yet been implemented")
+ update := libstate.PeerInfo{
+ LastContactAt: time.Now(),
+ }
+
+ err := (state).UpdatePeer(string(params.PeerID), update)
+
+ if err == nil {
+ return operations.NewGenesisPeersPingOK()
+ } else {
+ return operations.NewGenesisPeersPingNotFound()
+ }
})
+
api.GenesisPeersRegisterHandler = operations.GenesisPeersRegisterHandlerFunc(func(params operations.GenesisPeersRegisterParams) middleware.Responder {
- return middleware.NotImplemented("operation .GenesisPeersRegister has not yet been implemented")
+ //TODO: perform ping action on weaviate
+ var err error = nil
+
+ if err == nil {
+ err = (state).RegisterPeer(params.Body.PeerName, params.Body.PeerHost)
+ if err != nil {
+ return operations.NewGenesisPeersRegisterForbidden()
+ }
+ peers, err := (state).ListPeers()
+ return operations.NewGenesisPeersRegisterOK()
+ } else {
+ return operations.NewGenesisPeersRegisterBadRequest()
+ }
+ })
+
+ api.GenesisPeersListHandler = operations.GenesisPeersListHandlerFunc(func(params operations.GenesisPeersListParams) middleware.Responder {
+ peers := make([]*models.Peer, 0)
+
+ listed_peers, err := (state).ListPeers()
+ if err != nil {
+ log.Infof("Failed to list peers, because %+v", err)
+ return operations.NewGenesisPeersListInternalServerError()
+ } else {
+ for _, peer := range listed_peers {
+ p := models.Peer{
+ PeerHost: peer.Host(),
+ PeerName: peer.Name(),
+ }
+
+ peers = append(peers, &p)
+ }
+
+ reply := operations.NewGenesisPeersListOK()
+ reply.SetPayload(peers)
+ return reply
+ }
})
api.ServerShutdown = func() {}
