diff --git a/README.md b/README.md index 13365a2..97d4d04 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,18 @@ Once a subscription has declared itself as a publisher, it will enter a loop whe When sending a message representing an action (subscribe, publish etc) then a uint16 binary message is sent. -When sending any other data, the length of the data is to be sent first using a binary uint32 and then the actual data sent afterwards. \ No newline at end of file +When sending any other data, the length of the data is to be sent first using a binary uint32 and then the actual data sent afterwards. + +## Running the server + +There is a server that can be run using `docker-compose up message-server`. This will start a server running listening on port 3000. + +## Example clients +There is an example application that implements the subscriber and publishers in the `example` directory. + +Run `go build .` to build the file. + +When running the example there are the following flags: + +`publish` : settings this to true will allow messages to be sent every 500ms as well as consuming +`consume-from` : this allows you to specify what message to start from. If you don't set this or set it to be -1, you will start consuming from the next sent message. diff --git a/example/server/main.go b/cmd/server/main.go similarity index 100% rename from example/server/main.go rename to cmd/server/main.go diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..a556973 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,7 @@ +version: "3.7" +services: + message-server: + build: + context: . + dockerfile: dockerfile.server + ports: [ "3000:3000" ] \ No newline at end of file diff --git a/dockerfile.example-server b/dockerfile.server similarity index 92% rename from dockerfile.example-server rename to dockerfile.server index 8f91a5d..84291f3 100644 --- a/dockerfile.example-server +++ b/dockerfile.server @@ -3,7 +3,7 @@ FROM golang:latest as builder WORKDIR /app COPY go.mod go.sum ./ -COPY example/server/ ./ +COPY cmd/server/ ./ RUN go mod download COPY . . diff --git a/example/main.go b/example/main.go index d9d8e5c..74e7afb 100644 --- a/example/main.go +++ b/example/main.go @@ -11,15 +11,19 @@ import ( "github.com/willdot/messagebroker/internal/server" ) -var consumeOnly *bool +var publish *bool var consumeFrom *int +const ( + topic = "topic-a" +) + func main() { - consumeOnly = flag.Bool("consume-only", false, "just consumes (doesn't start server and doesn't publish)") + publish = flag.Bool("publish", false, "will also publish messages every 500ms until client is stopped") consumeFrom = flag.Int("consume-from", -1, "index of message to start consuming from. If not set it will consume from the most recent") flag.Parse() - if !*consumeOnly { + if *publish { go sendMessages() } @@ -38,7 +42,7 @@ func main() { startAt = *consumeFrom } - err = sub.SubscribeToTopics([]string{"topic a"}, startAtType, startAt) + err = sub.SubscribeToTopics([]string{topic}, startAtType, startAt) if err != nil { panic(err) } @@ -69,7 +73,7 @@ func sendMessages() { i := 0 for { i++ - msg := client.NewMessage("topic a", []byte(fmt.Sprintf("message %d", i))) + msg := client.NewMessage(topic, []byte(fmt.Sprintf("message %d", i))) err = publisher.PublishMessage(msg) if err != nil {