From 943811c365322b0d572e31492744378a416cf8fd Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:20:11 +0330
Subject: [PATCH 1/8] init: url struct
---
internal/url.go | 1 +
1 file changed, 1 insertion(+)
create mode 100644 internal/url.go
diff --git a/internal/url.go b/internal/url.go
new file mode 100644
index 0000000..5bf0569
--- /dev/null
+++ b/internal/url.go
@@ -0,0 +1 @@
+package internal
From 41b8b95f1f5c72c5787b2bcd1bb5ea4e3d0920f2 Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:22:24 +0330
Subject: [PATCH 2/8] refactor: url method
---
internal/url.go | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/internal/url.go b/internal/url.go
index 5bf0569..749d438 100644
--- a/internal/url.go
+++ b/internal/url.go
@@ -1 +1,16 @@
package internal
+
+// url
+// each url contains of the following parts:
+// - host
+// - port
+type url struct {
+ host string
+ port string
+}
+
+// urlUnpack
+// manages to create url struct from url string.
+func urlUnpack(url string) *url {
+ return nil
+}
From 2e3af7651e58b28d54f8c3297eb5f40990610f8e Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:27:03 +0330
Subject: [PATCH 3/8] add: protocol validation for url
---
internal/url.go | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/internal/url.go b/internal/url.go
index 749d438..6663111 100644
--- a/internal/url.go
+++ b/internal/url.go
@@ -1,5 +1,10 @@
package internal
+import (
+ "fmt"
+ "strings"
+)
+
// url
// each url contains of the following parts:
// - host
@@ -11,6 +16,15 @@ type url struct {
// urlUnpack
// manages to create url struct from url string.
-func urlUnpack(url string) *url {
- return nil
+func urlUnpack(url string) (*url, error) {
+ protocolSplit := strings.Split(url, "://")
+ if len(protocolSplit) < 2 {
+ return nil, fmt.Errorf("invalid uri")
+ }
+
+ if protocolSplit[0] != "st" {
+ return nil, fmt.Errorf("not using stallion protocol (st://...)")
+ }
+
+ return nil, nil
}
From 684d80a4363389e3a979b3f9cf44b0583bf4f33e Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:31:18 +0330
Subject: [PATCH 4/8] add: host port exporting in url
---
internal/url.go | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/internal/url.go b/internal/url.go
index 6663111..6867642 100644
--- a/internal/url.go
+++ b/internal/url.go
@@ -16,8 +16,8 @@ type url struct {
// urlUnpack
// manages to create url struct from url string.
-func urlUnpack(url string) (*url, error) {
- protocolSplit := strings.Split(url, "://")
+func urlUnpack(inputUrl string) (*url, error) {
+ protocolSplit := strings.Split(inputUrl, "://")
if len(protocolSplit) < 2 {
return nil, fmt.Errorf("invalid uri")
}
@@ -26,5 +26,13 @@ func urlUnpack(url string) (*url, error) {
return nil, fmt.Errorf("not using stallion protocol (st://...)")
}
- return nil, nil
+ hostInformation := strings.Split(protocolSplit[1], ":")
+ if len(hostInformation) < 2 {
+ return nil, fmt.Errorf("server ip or port is not given")
+ }
+
+ return &url{
+ host: hostInformation[0],
+ port: hostInformation[1],
+ }, nil
}
From 0a2fc36e7a52c017b1bb689a049bf412d9779797 Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:32:11 +0330
Subject: [PATCH 5/8] add: host and port
---
internal/url.go | 3 +++
1 file changed, 3 insertions(+)
diff --git a/internal/url.go b/internal/url.go
index 6867642..e6b54d4 100644
--- a/internal/url.go
+++ b/internal/url.go
@@ -17,15 +17,18 @@ type url struct {
// urlUnpack
// manages to create url struct from url string.
func urlUnpack(inputUrl string) (*url, error) {
+ // check the input url protocol
protocolSplit := strings.Split(inputUrl, "://")
if len(protocolSplit) < 2 {
return nil, fmt.Errorf("invalid uri")
}
+ // protocol must be 'st'
if protocolSplit[0] != "st" {
return nil, fmt.Errorf("not using stallion protocol (st://...)")
}
+ // exporting the host:port pair.
hostInformation := strings.Split(protocolSplit[1], ":")
if len(hostInformation) < 2 {
return nil, fmt.Errorf("server ip or port is not given")
From 8699077c7a9fe117741d70a1814258587424e0ee Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:35:06 +0330
Subject: [PATCH 6/8] used: uri in client connection
---
client.go | 7 ++++++-
internal/url.go => url.go | 2 +-
2 files changed, 7 insertions(+), 2 deletions(-)
rename internal/url.go => url.go (97%)
diff --git a/client.go b/client.go
index 5722db2..946716a 100644
--- a/client.go
+++ b/client.go
@@ -22,7 +22,12 @@ type Client interface {
// NewClient creates a new client to connect to broker server.
func NewClient(uri string) (Client, error) {
- conn, err := net.Dial("tcp", uri)
+ url, err := urlUnpack(uri)
+ if err != nil {
+ return nil, fmt.Errorf("invalid uri: %w", err)
+ }
+
+ conn, err := net.Dial("tcp", url.host+":"+url.port)
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
diff --git a/internal/url.go b/url.go
similarity index 97%
rename from internal/url.go
rename to url.go
index e6b54d4..4cafaef 100644
--- a/internal/url.go
+++ b/url.go
@@ -1,4 +1,4 @@
-package internal
+package stallion
import (
"fmt"
From b863b7d9b2af5f5ef390e6769c74b30b0a95ef69 Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:36:33 +0330
Subject: [PATCH 7/8] update: url struct
---
client.go | 2 +-
url.go | 9 +++------
2 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/client.go b/client.go
index 946716a..b3af43d 100644
--- a/client.go
+++ b/client.go
@@ -27,7 +27,7 @@ func NewClient(uri string) (Client, error) {
return nil, fmt.Errorf("invalid uri: %w", err)
}
- conn, err := net.Dial("tcp", url.host+":"+url.port)
+ conn, err := net.Dial("tcp", url.address)
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
diff --git a/url.go b/url.go
index 4cafaef..a4341d9 100644
--- a/url.go
+++ b/url.go
@@ -10,8 +10,7 @@ import (
// - host
// - port
type url struct {
- host string
- port string
+ address string
}
// urlUnpack
@@ -29,13 +28,11 @@ func urlUnpack(inputUrl string) (*url, error) {
}
// exporting the host:port pair.
- hostInformation := strings.Split(protocolSplit[1], ":")
- if len(hostInformation) < 2 {
+ if len(strings.Split(protocolSplit[1], ":")) < 2 {
return nil, fmt.Errorf("server ip or port is not given")
}
return &url{
- host: hostInformation[0],
- port: hostInformation[1],
+ address: protocolSplit[1],
}, nil
}
From fc79e79df0fff2ee6cc6c5692310e38135495bec Mon Sep 17 00:00:00 2001
From: amirhnajafiz
Date: Mon, 10 Oct 2022 16:38:11 +0330
Subject: [PATCH 8/8] update: client examples
---
README.md | 6 +++---
example/client/multi-subscribe/main.go | 4 ++--
example/client/simple-publish/main.go | 2 +-
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 078331c..a31626f 100644
--- a/README.md
+++ b/README.md
@@ -3,8 +3,8 @@
-
-
+
+
@@ -51,7 +51,7 @@ import (
)
func main() {
- client, err := stallion.NewClient("localhost:9090")
+ client, err := stallion.NewClient("st://localhost:9090")
if err != nil {
panic(err)
}
diff --git a/example/client/multi-subscribe/main.go b/example/client/multi-subscribe/main.go
index 1c081e4..b3721de 100644
--- a/example/client/multi-subscribe/main.go
+++ b/example/client/multi-subscribe/main.go
@@ -8,14 +8,14 @@ import (
)
func main() {
- client, err := stallion.NewClient("localhost:9090")
+ client, err := stallion.NewClient("st://localhost:9090")
if err != nil {
panic(err)
}
for i := 0; i < 2; i++ {
go func(j int) {
- cli, er := stallion.NewClient("localhost:9090")
+ cli, er := stallion.NewClient("st://localhost:9090")
if er != nil {
log.Fatal(er)
}
diff --git a/example/client/simple-publish/main.go b/example/client/simple-publish/main.go
index 98295be..3960cfe 100644
--- a/example/client/simple-publish/main.go
+++ b/example/client/simple-publish/main.go
@@ -8,7 +8,7 @@ import (
)
func main() {
- client, err := stallion.NewClient("localhost:9090")
+ client, err := stallion.NewClient("st://localhost:9090")
if err != nil {
panic(err)
}