basic server/client http communication setup
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
package clientcmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
clientconfig "github.com/deranjer/gvc/client/clientconfig"
|
||||
messages "github.com/deranjer/gvc/messages"
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
// ConfigPath is the global path to the config that is injected from the main client package.
|
||||
@@ -64,8 +62,8 @@ func checkFileTypes(input string, inputType string, ignores clientconfig.FileTyp
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConnectToServer sends a quick hello to the server to make sure it is live and we can connect to it.
|
||||
func ConnectToServer(serverName string, branchName string, conf *clientconfig.Gvcconfig) (conn *net.Conn, err error) {
|
||||
// PingServer sends a quick hello to the server to make sure it is live and we can connect to it. //TODO Remove this is just extra shit
|
||||
func PingServer(serverName string, branchName string, conf *clientconfig.Gvcconfig) error {
|
||||
if branchName == "" { // If no branch listed select master TODO: in future the 'default' branch will be their current branch
|
||||
branchName = "master"
|
||||
}
|
||||
@@ -73,33 +71,19 @@ func ConnectToServer(serverName string, branchName string, conf *clientconfig.Gv
|
||||
if serverName == remote.Name {
|
||||
fmt.Printf("Server found in config, connecting to: %s, host: %s, port: %d \n", remote.Name, remote.Host, remote.Port)
|
||||
port := ":" + strconv.Itoa(remote.Port)
|
||||
connectionString := remote.Host + port
|
||||
conn, err := net.Dial("tcp", connectionString)
|
||||
defer conn.Close()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error connecting to %s", connectionString)
|
||||
}
|
||||
connectionString := "http://" + remote.Host + port + "/hello" //Create our hello request
|
||||
fmt.Println("Attempting connection on: ", connectionString)
|
||||
decoder := json.NewDecoder(conn)
|
||||
var newMessage messages.Command
|
||||
for { // Doing our 'handshake'
|
||||
decoder.Decode(&newMessage)
|
||||
client := resty.New()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading from connection: %s", err)
|
||||
}
|
||||
fmt.Println("Message from server", newMessage)
|
||||
switch newMessage.CmdID {
|
||||
case messages.CONNECTED:
|
||||
fmt.Println("Server recognized client")
|
||||
return &conn, nil
|
||||
default:
|
||||
fmt.Println("message: ", newMessage.CmdID)
|
||||
return nil, fmt.Errorf("Unexpected message from server")
|
||||
//return nil, fmt.Errorf("unexpected response from server: ", newMessage)
|
||||
}
|
||||
resp, err := client.R().EnableTrace().Get(connectionString)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error connecting to server at: %s: error was: %s", connectionString, err)
|
||||
}
|
||||
if resp.StatusCode() == 200 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("response from server was not 200, was instead %d", resp.StatusCode())
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to find server name in config")
|
||||
return fmt.Errorf("unable to find server name in config")
|
||||
}
|
||||
|
Reference in New Issue
Block a user