-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3077e3b
commit b37a82b
Showing
15 changed files
with
2,659 additions
and
2,662 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"sync" | ||
"encoding/json" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/SimplyVC/oasis_api_server/src/config" | ||
lgr "github.com/SimplyVC/oasis_api_server/src/logger" | ||
"github.com/SimplyVC/oasis_api_server/src/responses" | ||
"github.com/SimplyVC/oasis_api_server/src/config" | ||
lgr "github.com/SimplyVC/oasis_api_server/src/logger" | ||
"github.com/SimplyVC/oasis_api_server/src/responses" | ||
) | ||
|
||
// Pong responds with ping if entire API is online | ||
func Pong(w http.ResponseWriter, r *http.Request) { | ||
|
||
// Add header so that received knows they're receiving JSON | ||
w.Header().Add("Content-Type", "application/json") | ||
lgr.Info.Println("Received request for /api/pingapi") | ||
json.NewEncoder(w).Encode(responses.SuccessResponsed) | ||
// Add header so that received knows they're receiving JSON | ||
w.Header().Add("Content-Type", "application/json") | ||
lgr.Info.Println("Received request for /api/pingapi") | ||
json.NewEncoder(w).Encode(responses.SuccessResponsed) | ||
} | ||
|
||
// GetConnections retrieves the node names that are configured in the API | ||
func GetConnections(w http.ResponseWriter, r *http.Request) { | ||
|
||
// Add header so that received knows they're receiving JSON | ||
w.Header().Add("Content-Type", "application/json") | ||
lgr.Info.Println("Received request for /api/getconnectionslist") | ||
|
||
mutex := &sync.RWMutex{} | ||
mutex.Lock() | ||
|
||
// Create new empty Slice of strings where connections will be stored | ||
connectionsResponse := []string{} | ||
allSockets := config.GetNodes() | ||
|
||
lgr.Info.Println("Iterating through all socket connections.") | ||
for _, socket := range allSockets { | ||
lgr.Info.Printf("Node: %s has socket %s \n", | ||
socket["node_name"], socket["isocket_path"]) | ||
connectionsResponse = append(connectionsResponse, | ||
socket["node_name"]) | ||
} | ||
// Encode object and send it using predefind response | ||
json.NewEncoder(w).Encode(responses.ConnectionsResponse{ | ||
Results: connectionsResponse}) | ||
mutex.Unlock() | ||
} | ||
// Add header so that received knows they're receiving JSON | ||
w.Header().Add("Content-Type", "application/json") | ||
lgr.Info.Println("Received request for /api/getconnectionslist") | ||
|
||
mutex := &sync.RWMutex{} | ||
mutex.Lock() | ||
|
||
// Create new empty Slice of strings where connections will be stored | ||
connectionsResponse := []string{} | ||
allSockets := config.GetNodes() | ||
|
||
lgr.Info.Println("Iterating through all socket connections.") | ||
for _, socket := range allSockets { | ||
lgr.Info.Printf("Node: %s has socket %s \n", | ||
socket["node_name"], socket["isocket_path"]) | ||
connectionsResponse = append(connectionsResponse, | ||
socket["node_name"]) | ||
} | ||
// Encode object and send it using predefind response | ||
json.NewEncoder(w).Encode(responses.ConnectionsResponse{ | ||
Results: connectionsResponse}) | ||
mutex.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,78 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc" | ||
|
||
lgr "github.com/SimplyVC/oasis_api_server/src/logger" | ||
"github.com/SimplyVC/oasis_api_server/src/responses" | ||
"github.com/SimplyVC/oasis_api_server/src/rpc" | ||
control "github.com/oasislabs/oasis-core/go/control/api" | ||
lgr "github.com/SimplyVC/oasis_api_server/src/logger" | ||
"github.com/SimplyVC/oasis_api_server/src/responses" | ||
"github.com/SimplyVC/oasis_api_server/src/rpc" | ||
control "github.com/oasislabs/oasis-core/go/control/api" | ||
) | ||
|
||
// loadNodeControllerClient loads node controller client and returns it | ||
func loadNodeControllerClient(socket string) (*grpc.ClientConn, | ||
control.NodeController) { | ||
control.NodeController) { | ||
|
||
// Attempt to load connection with staking client | ||
connection, nodeControllerClient, err := rpc. | ||
NodeControllerClient(socket) | ||
if err != nil { | ||
lgr.Error.Println("Failed to establish connection to "+ | ||
"NodeController client : ", err) | ||
return nil, nil | ||
} | ||
return connection, nodeControllerClient | ||
// Attempt to load connection with staking client | ||
connection, nodeControllerClient, err := rpc. | ||
NodeControllerClient(socket) | ||
if err != nil { | ||
lgr.Error.Println("Failed to establish connection to "+ | ||
"NodeController client : ", err) | ||
return nil, nil | ||
} | ||
return connection, nodeControllerClient | ||
} | ||
|
||
// GetIsSynced checks whether node has finished syncing. | ||
func GetIsSynced(w http.ResponseWriter, r *http.Request) { | ||
|
||
// Add header so that received knows they're receiving JSON | ||
w.Header().Add("Content-Type", "application/json") | ||
// Add header so that received knows they're receiving JSON | ||
w.Header().Add("Content-Type", "application/json") | ||
|
||
// Retrieving name of node from query request | ||
nodeName := r.URL.Query().Get("name") | ||
confirmation, socket := checkNodeName(nodeName) | ||
if confirmation == false { | ||
// Retrieving name of node from query request | ||
nodeName := r.URL.Query().Get("name") | ||
confirmation, socket := checkNodeName(nodeName) | ||
if confirmation == false { | ||
|
||
// Stop code here no need to establish connection and reply | ||
json.NewEncoder(w).Encode(responses.ErrorResponse{ | ||
Error: "Node name requested doesn't exist"}) | ||
return | ||
} | ||
// Stop code here no need to establish connection and reply | ||
json.NewEncoder(w).Encode(responses.ErrorResponse{ | ||
Error: "Node name requested doesn't exist"}) | ||
return | ||
} | ||
|
||
// Attempt to load connection with staking client | ||
connection, nc := loadNodeControllerClient(socket) | ||
// Attempt to load connection with staking client | ||
connection, nc := loadNodeControllerClient(socket) | ||
|
||
// Close connection once code underneath executes | ||
defer connection.Close() | ||
// Close connection once code underneath executes | ||
defer connection.Close() | ||
|
||
// If null object was retrieved send response | ||
if nc == nil { | ||
// If null object was retrieved send response | ||
if nc == nil { | ||
|
||
// Stop code here faild to establish connection and reply | ||
json.NewEncoder(w).Encode(responses.ErrorResponse{ | ||
Error: "Failed to establish connection using socket: " + | ||
socket}) | ||
return | ||
} | ||
// Stop code here faild to establish connection and reply | ||
json.NewEncoder(w).Encode(responses.ErrorResponse{ | ||
Error: "Failed to establish connection using socket: " + | ||
socket}) | ||
return | ||
} | ||
|
||
// Retrieving synchronized state from node controller client | ||
synced, err := nc.IsSynced(context.Background()) | ||
if err != nil { | ||
json.NewEncoder(w).Encode(responses.ErrorResponse{ | ||
Error: "Failed to get IsSynced!"}) | ||
lgr.Error.Println("Request at /api/nodecontroller/synced/ "+ | ||
"failed to get IsSynced : ", err) | ||
return | ||
} | ||
// Retrieving synchronized state from node controller client | ||
synced, err := nc.IsSynced(context.Background()) | ||
if err != nil { | ||
json.NewEncoder(w).Encode(responses.ErrorResponse{ | ||
Error: "Failed to get IsSynced!"}) | ||
lgr.Error.Println("Request at /api/nodecontroller/synced/ "+ | ||
"failed to get IsSynced : ", err) | ||
return | ||
} | ||
|
||
// Responding with retrieved synchronizatio state above | ||
lgr.Info.Println("Request at /api/nodecontroller/synced/ sending with" + | ||
" IsSynced State!") | ||
json.NewEncoder(w).Encode(responses.IsSyncedResponse{Synced: synced}) | ||
} | ||
// Responding with retrieved synchronizatio state above | ||
lgr.Info.Println("Request at /api/nodecontroller/synced/ sending with" + | ||
" IsSynced State!") | ||
json.NewEncoder(w).Encode(responses.IsSyncedResponse{Synced: synced}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,59 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
hdl "github.com/SimplyVC/oasis_api_server/src/handlers" | ||
hdl "github.com/SimplyVC/oasis_api_server/src/handlers" | ||
) | ||
|
||
func Test_GetIsSynced_BadNode(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "/api/staking/synced", nil) | ||
q := req.URL.Query() | ||
q.Add("name", "Unicorn") | ||
req.URL.RawQuery = q.Encode() | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(hdl.GetIsSynced) | ||
handler.ServeHTTP(rr, req) | ||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", | ||
status, http.StatusOK) | ||
} | ||
|
||
expected := `{"error":"Node name requested doesn't exist"}` | ||
|
||
if strings.TrimSpace(rr.Body.String()) != strings.TrimSpace(expected) { | ||
t.Errorf("handler returned unexpected body: got %v want %v", | ||
rr.Body.String(), expected) | ||
} | ||
req, _ := http.NewRequest("GET", "/api/staking/synced", nil) | ||
q := req.URL.Query() | ||
q.Add("name", "Unicorn") | ||
req.URL.RawQuery = q.Encode() | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(hdl.GetIsSynced) | ||
handler.ServeHTTP(rr, req) | ||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", | ||
status, http.StatusOK) | ||
} | ||
|
||
expected := `{"error":"Node name requested doesn't exist"}` | ||
|
||
if strings.TrimSpace(rr.Body.String()) != strings.TrimSpace(expected) { | ||
t.Errorf("handler returned unexpected body: got %v want %v", | ||
rr.Body.String(), expected) | ||
} | ||
} | ||
|
||
func Test_GetIsSynced_Height3(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "/api/staking/synced", nil) | ||
q := req.URL.Query() | ||
q.Add("name", "Oasis_Local") | ||
q.Add("height", "3") | ||
|
||
req.URL.RawQuery = q.Encode() | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(hdl.GetIsSynced) | ||
handler.ServeHTTP(rr, req) | ||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", | ||
status, http.StatusOK) | ||
} | ||
|
||
expectedTrue := `{"result":true}` | ||
expectedFalse := `{"result":false}` | ||
|
||
if strings.TrimSpace(rr.Body.String()) != strings.TrimSpace( | ||
expectedTrue) && strings.TrimSpace(rr.Body.String()) != | ||
strings.TrimSpace(expectedFalse) { | ||
t.Errorf("handler returned unexpected body: got %v", | ||
rr.Body.String()) | ||
} | ||
} | ||
req, _ := http.NewRequest("GET", "/api/staking/synced", nil) | ||
q := req.URL.Query() | ||
q.Add("name", "Oasis_Local") | ||
q.Add("height", "3") | ||
|
||
req.URL.RawQuery = q.Encode() | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(hdl.GetIsSynced) | ||
handler.ServeHTTP(rr, req) | ||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", | ||
status, http.StatusOK) | ||
} | ||
|
||
expectedTrue := `{"result":true}` | ||
expectedFalse := `{"result":false}` | ||
|
||
if strings.TrimSpace(rr.Body.String()) != strings.TrimSpace( | ||
expectedTrue) && strings.TrimSpace(rr.Body.String()) != | ||
strings.TrimSpace(expectedFalse) { | ||
t.Errorf("handler returned unexpected body: got %v", | ||
rr.Body.String()) | ||
} | ||
} |
Oops, something went wrong.