Skip to content

Commit

Permalink
Updated with gofmt formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyV1337 committed May 14, 2020
1 parent 3077e3b commit b37a82b
Show file tree
Hide file tree
Showing 15 changed files with 2,659 additions and 2,662 deletions.
940 changes: 470 additions & 470 deletions src/handlers/consensus_handler.go

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions src/handlers/general_handler.go
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()
}
2 changes: 1 addition & 1 deletion src/handlers/general_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ func Test_GetConnections(t *testing.T) {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
}
110 changes: 55 additions & 55 deletions src/handlers/node_controller_handler.go
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})
}
98 changes: 49 additions & 49 deletions src/handlers/node_controller_handler_test.go
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())
}
}
Loading

0 comments on commit b37a82b

Please sign in to comment.