Skip to content

Commit

Permalink
Merge pull request #72 from kelchy/main
Browse files Browse the repository at this point in the history
Release rmq/consumer 0.1.1
  • Loading branch information
kelchy committed Jul 18, 2023
2 parents 65a7e98 + 6ec388a commit 6f88578
Show file tree
Hide file tree
Showing 57 changed files with 3,531 additions and 972 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL := /bin/bash

.PHONY:

DIR = log redis
DIR = log redis http

test-%:
$(MAKE) GOPATH=$${PWD} test -C $* SUB=${SUB}
Expand Down
12 changes: 11 additions & 1 deletion http/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ SHELL := /bin/bash

.PHONY:

test:
# this will install binary in ${GOPATH}
$(GOPATH)/bin/golint:
go install golang.org/x/lint/golint@v0.0.0-20201208152925-83fdc39ff7b5

lint: $(GOPATH)/bin/golint
${GOPATH}/bin/golint -set_exit_status ./...

test: lint
$(MAKE) test -C client
$(MAKE) test -C server

ifndef ${SUB}
@echo "SUB not defined defaulting to client"
$(MAKE) test -C client
Expand Down
81 changes: 81 additions & 0 deletions http/server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package server

import (
"github.com/kelchy/go-lib/log"
"net/http"
"net/http/httptest"
"testing"
)

func TestNew(t *testing.T) {
// Test case with empty origins and headers
router, err := New(nil, nil)
if err != nil {
t.Fatalf("Error creating router: %v", err)
}
if router == nil {
t.Fatal("Router is nil")
}

// Test case with invalid log configuration
_, err = log.New("invalid config")
if err == nil {
t.Fatal("Expected an error creating logger")
}

// happy flow
origins := []string{"http://localhost"}
headers := []string{"Accept", "Content-Type"}

router, err = New(origins, headers)
if err != nil {
t.Fatalf("Error creating router: %v", err)
}

if router == nil {
t.Fatal("Router is nil")
}

// Test that the router has the expected fields
if router.log == (log.Log{}) {
t.Fatal("Log is nil/invalid")
}
if router.logRequest != true {
t.Fatal("LogRequest should be true by default")
}
if len(router.logSkipPath) != 1 || router.logSkipPath[0] != "/" {
t.Fatal("LogSkipPath should have the default value")
}
if router.Engine == nil {
t.Fatal("Engine is nil")
}

// Test that the router middleware has been set correctly
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
resp := httptest.NewRecorder()
router.Engine.ServeHTTP(resp, req)
if resp.Code != http.StatusNotFound {
t.Fatal("catchall middleware should return 404")
}

// Test case with empty origins and default headers
router, err = New(nil, []string{})
if err != nil {
t.Fatalf("Error creating router: %v", err)
}
if router == nil {
t.Fatal("Router is nil")
}

// Test case with default origins and empty headers
router, err = New([]string{}, nil)
if err != nil {
t.Fatalf("Error creating router: %v", err)
}
if router == nil {
t.Fatal("Router is nil")
}
}
13 changes: 7 additions & 6 deletions redis/example/redis_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package main
import (
"context"
"fmt"
"time"

"github.com/kelchy/go-lib/redis"
"os"
"time"
)

func main() {

uri := "<redis uri here>"
uri := os.Getenv("REDIS_URI")

// path to cert files
clientCertPath := "<file-path>"
clientKeyPath := `<file-path>`
// clientCertPath := "<file-path>"
// clientKeyPath := `<file-path>`

// use redis.New if TLS connection is not required
// skipVerifyCondition should only be true when running locally
redisclient, e := redis.NewSecure(uri, clientCertPath, clientKeyPath, false)
// redisclient, e := redis.NewSecure(uri, clientCertPath, clientKeyPath, false)
redisclient, e := redis.New(uri)
if e != nil {
fmt.Println(e)
return
Expand Down
17 changes: 10 additions & 7 deletions rmq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ This is a wrapper to provide functions for easily and safely interacting with Ra

Functionlity Supported:

| Functionality | Supported? |
| ----------- | ----------- |
| Publisher ||
| Consumer ||
| Dead Letter Queue ||
| Immediate & Mandatory Options ||
| Functionality | Supported? |
| ------------------------------- | ---------- |
| Publisher ||
| Consumer ||
| Dead Letter Queue ||
| Auto Reconnect upon disconnects ||
| Immediate & Mandatory Options ||
| Message retry count ||

## Quickstart
Refer to the respective publisher and consumer packages for how to use the library

Refer to the respective publisher and consumer packages for how to use the library
21 changes: 21 additions & 0 deletions rmq/consumer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Lane Wagner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions rmq/consumer/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SHELL := /bin/bash

.PHONY:

# this will install binary in ${GOPATH}
$(GOPATH)/bin/golint:
go install golang.org/x/lint/golint@v0.0.0-20201208152925-83fdc39ff7b5
Expand Down
53 changes: 4 additions & 49 deletions rmq/consumer/README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,9 @@
# RMQ Consumer

This library is a wrapper around RMQ functions to make interaction with RMQ simpler and safer
This library is a wrapper around RMQ functions to make interaction with RMQ simpler and safer. Functionality is tested with hosted CloudAMQP, which offers automated failover using connection with a single URI.

## Quickstart

Refer to the below code snippet for how to set up a consumer called `test-consumer` consuming events from a queue `test-queue` bound to an exchange `test-exchange` on a binding key `test-routing-key`.
```
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/kelchy/go-lib/rmq/consumer"
)
func main() {
eventProcessor := &EventProcessor{}
err := consumer.New(
consumer.DefaultConnectionConfig([]string{os.Getenv("RMQ_URI")}),
// Queue names should be the same in QueueConfig and QueueBindConfig
consumer.DefaultQueueConfig("test-queue-logging"),
consumer.DefaultQueueBindConfig("test-exchange", "test-queue-logging", "test-routing-key"),
consumer.DefaultConfig("test-consumer"),
consumer.DefaultMessageRetryConfig(),
eventProcessor,
consumer.DefaultLogger())
if err != nil {
fmt.Println("failed to create consumer: ", err)
}
Adapted from [go-rabbitmq](https://github.com/wagslane/go-rabbitmq)

// Leave the consumer running for 30 seconds before exiting, only for example purposes
time.Sleep(30 * time.Second)
}
// Declare what to do when messages come in on the queue here
type EventProcessor struct{}
func (*EventProcessor) ProcessEvent(ctx context.Context, message consumer.IMessage) error {
fmt.Printf("Recieved message: ID: %s, Message: %s\n", message.GetID(), string(message.Body()))
// Fill in what to do with the consumed messages here
return nil
}
func (*EventProcessor) ProcessDeadMessage(ctx context.Context, message consumer.IMessage, err error) error {
fmt.Printf("Recieved dead message: ID: %s, Message: %s, Error: %v", message.GetID(), string(message.Body()), err)
// Fill in what to do with the dead messages here
return nil
}
## Quickstart

```
Refer to `example/consumer.example.go` to see how to setup your own consumer

0 comments on commit 6f88578

Please sign in to comment.