basic server/client http communication setup
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
version = "0.1.5"
|
||||
rootPath = ""
|
||||
currentbranch = "master"
|
||||
|
||||
localbranches = ["master"]
|
||||
remotebranches = ["master", "test", "test2"]
|
||||
|
||||
[[remote]]
|
||||
name = "test2"
|
||||
host = "localhost"
|
||||
port = 9999
|
||||
port = 80
|
||||
default = false
|
||||
|
||||
[[remote]]
|
||||
@@ -35,12 +34,13 @@ remotebranches = ["master", "test", "test2"]
|
||||
port = 9995
|
||||
default = false
|
||||
|
||||
[locked]
|
||||
files = ["test1\\client8.exe", "client1.exe"]
|
||||
exts = [".png"]
|
||||
|
||||
[ignore]
|
||||
files = ["client1.exe", "client2.exe", "client3.exe", "client4.exe", "client5.exe", "client6.exe", "client7.exe", "test1\\client8.exe", "clientcmd\\init.go"]
|
||||
exts = [".exe", ".tl"]
|
||||
|
||||
[nocompress]
|
||||
|
||||
[locked]
|
||||
files = ["test1\\client8.exe", "client1.exe"]
|
||||
exts = [".png"]
|
||||
|
@@ -327,11 +327,11 @@ func infoCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
|
||||
return fmt.Errorf("Please supply a server name to test a remote")
|
||||
}
|
||||
server := remoteInfoCmd.OtherArgs()[0]
|
||||
conn, err := clientcmd.ConnectToServer(server, "master", conf)
|
||||
err := clientcmd.PingServer(server, "master", conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = clientcmd.GetServerInfo(conn)
|
||||
err = clientcmd.GetServerInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -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")
|
||||
}
|
||||
|
@@ -1,34 +1,23 @@
|
||||
package clientcmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
// GetServerInfo queries the supplied server name for information about the server
|
||||
func GetServerInfo(conn *net.Conn) error {
|
||||
for { // sending request for details to server
|
||||
bytesSent, err := fmt.Fprintf(*conn, "Details\n")
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message to server! ", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
if bytesSent > 0 {
|
||||
fmt.Println("Message Sent")
|
||||
}
|
||||
message, err := bufio.NewReader(*conn).ReadString('\n')
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading from connection: %s", err)
|
||||
}
|
||||
fmt.Printf("Message from server: %s", message)
|
||||
switch message {
|
||||
case "Details\n":
|
||||
fmt.Println("Server details: ", message)
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unrecognized message: %s", message)
|
||||
}
|
||||
// GetServerInfo queries the supplied server name for information about the current repo from the server
|
||||
func GetServerInfo() error {
|
||||
serverURL := "http://localhost:80/info/"
|
||||
client := resty.New()
|
||||
resp, err := client.R().
|
||||
SetPathParams(map[string]string{
|
||||
"repoName": "testRepo",
|
||||
}).
|
||||
Get(serverURL + "{repoName}")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error connecting to server at: %s: error was: %s", serverURL, err)
|
||||
}
|
||||
fmt.Println(resp)
|
||||
return nil
|
||||
}
|
||||
|
@@ -1,43 +1,19 @@
|
||||
package clientcmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
clientconfig "github.com/deranjer/gvc/client/clientconfig"
|
||||
)
|
||||
|
||||
// RefreshContent gets all the new file locks and updated pulls from the server (like git fetch)
|
||||
func RefreshContent(conf *clientconfig.Gvcconfig) error {
|
||||
remotes := conf.Remotes
|
||||
for _, remote := range remotes {
|
||||
if remote.Default {
|
||||
conn, err := ConnectToServer(remote.Name, conf.CurrentBranch, conf)
|
||||
err := PingServer(remote.Name, conf.CurrentBranch, conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for { // Server should send 'Connected\n' to client after connect, then client can ask for server details
|
||||
message, err := bufio.NewReader(*conn).ReadString('\n')
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading from connection: %s", err)
|
||||
}
|
||||
fmt.Printf("Message from server: %s", message)
|
||||
switch message {
|
||||
case "Connected\n":
|
||||
fmt.Println("Server has acknowledged connection, asking for server details")
|
||||
bytesSent, err := fmt.Fprintf(*conn, "Details\n")
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message to server! ", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
if bytesSent > 0 {
|
||||
fmt.Println("Message Sent")
|
||||
}
|
||||
default:
|
||||
fmt.Println("Details: ", message)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
//TODO: Now refresh content
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user