basic server/client http communication setup

This commit is contained in:
2020-06-06 21:02:58 -04:00
parent 23dd5090e3
commit dd3fd7c4ae
9 changed files with 65 additions and 93 deletions

View File

@@ -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"]

View File

@@ -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
}

View File

@@ -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")
}

View File

@@ -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
}

View File

@@ -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

2
go.mod
View File

@@ -6,6 +6,8 @@ require (
github.com/deranjer/clir v1.0.5
github.com/deranjer/store v0.0.0-20200526205429-464dd59c6031
github.com/firstrow/tcp_server v0.0.0-20190424084220-b7a05ff2879d
github.com/go-resty/resty/v2 v2.3.0
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/echo/v4 v4.1.16 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
)

7
go.sum
View File

@@ -8,12 +8,16 @@ github.com/deranjer/store v0.0.0-20200526205429-464dd59c6031/go.mod h1:wPOs9IJ77
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/firstrow/tcp_server v0.0.0-20190424084220-b7a05ff2879d h1:3/oQzvZhwA8Jb5ykb0KehJfsdHokCJdC96k7xy6SJcs=
github.com/firstrow/tcp_server v0.0.0-20190424084220-b7a05ff2879d/go.mod h1:hGkv6sO57ZC+XrSTyzdIGXX7+O6S3RJb9G8sPopEF/4=
github.com/go-resty/resty v1.12.0 h1:L1P5qymrXL5H/doXe2pKUr1wxovAI5ilm2LdVLbwThc=
github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So=
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/labstack/echo v1.4.4 h1:1bEiBNeGSUKxcPDGfZ/7IgdhJJZx8wV/pICJh4W2NJI=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/echo/v4 v4.1.16 h1:8swiwjE5Jkai3RPfZoahp8kjVCRNq+y7Q0hPji2Kz0o=
github.com/labstack/echo/v4 v4.1.16/go.mod h1:awO+5TzAjvL8XpibdsfXxPgHr+orhtXZJZIQCVjogKI=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
@@ -44,6 +48,8 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -51,6 +57,7 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

View File

@@ -1,7 +1,19 @@
package engine
import "github.com/labstack/echo"
import (
"net/http"
func (server *GVCServer) Info(context echo.Context) {
"github.com/labstack/echo"
)
// GetInfo return the relevant repo specific info to the client
func (Server *GVCServer) GetInfo(context echo.Context) error {
repo := context.Param("repoName")
return context.JSON(http.StatusAccepted, repo)
}
// Hello just verifies the server is running //TODO remove this, just extra shit we are sending
func (Server *GVCServer) Hello(context echo.Context) error {
helloMsg := "server alive"
return context.JSON(http.StatusOK, helloMsg)
}

View File

@@ -46,7 +46,9 @@ func main() {
log.Info("Logger starting...")
// Setup the web server
e := echo.New()
server.Echo = e
//Start the routes
//server.Echo.GET("/info")
e.GET("/hello", server.Hello)
e.GET("/info/:repoName", server.GetInfo)
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", server.Config.BindIP, server.Config.Port)))
}