-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from kelchy/main
Release rmq/consumer 0.1.1
- Loading branch information
Showing
57 changed files
with
3,531 additions
and
972 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.