Skip to content

Commit

Permalink
Merge pull request #16 from official-stallion/15-connection-uri
Browse files Browse the repository at this point in the history
15 connection uri
  • Loading branch information
amirhnajafiz authored Oct 10, 2022
2 parents 2f13913 + fc79e79 commit 06d4443
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
</p>

<p align="center">
<img src="https://img.shields.io/badge/Golang-1.18-66ADD8?style=for-the-badge&logo=go" alt="go version" />
<img src="https://img.shields.io/badge/Version-1.1.2-red?style=for-the-badge&logo=github" alt="version" /><br />
<img src="https://img.shields.io/badge/Golang-1.19-66ADD8?style=for-the-badge&logo=go" alt="go version" />
<img src="https://img.shields.io/badge/Version-1.1.3-red?style=for-the-badge&logo=github" alt="version" /><br />
<img src="https://img.shields.io/badge/MacOS-black?style=for-the-badge&logo=apple" alt="version" />
<img src="https://img.shields.io/badge/Linux-white?style=for-the-badge&logo=linux" alt="version" />
<img src="https://img.shields.io/badge/Windows-blue?style=for-the-badge&logo=windows" alt="version" />
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.address)
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions example/client/multi-subscribe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion example/client/simple-publish/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
38 changes: 38 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package stallion

import (
"fmt"
"strings"
)

// url
// each url contains of the following parts:
// - host
// - port
type url struct {
address string
}

// 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.
if len(strings.Split(protocolSplit[1], ":")) < 2 {
return nil, fmt.Errorf("server ip or port is not given")
}

return &url{
address: protocolSplit[1],
}, nil
}

0 comments on commit 06d4443

Please sign in to comment.