Skip to content

Commit

Permalink
Merge pull request #8 from Moranilt/feature/clients
Browse files Browse the repository at this point in the history
Feature/clients
  • Loading branch information
Moranilt committed Feb 26, 2024
2 parents 6f5767d + abacdd8 commit ac182ba
Show file tree
Hide file tree
Showing 14 changed files with 1,484 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# HTTP utilities for http_template

- [Client](./client/README.md)
- [Clients](./clients/README.md)
- [Handler](./handler/README.md)
- [Logger](./logger/README.md)
- [Mock](./mock/README.md)
Expand Down
2 changes: 2 additions & 0 deletions clients/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Clients
Default clients for http_template
53 changes: 53 additions & 0 deletions clients/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package database

import (
"context"
"fmt"

"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)

type Credentials struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
Host string `mapstructure:"host"`
SSLMode string `mapstructure:"sslmode"`
}

func (d *Credentials) SourceString(production bool) string {
if !production {
return fmt.Sprintf(
"user=%s password=%s dbname=%s host=%s sslmode=disable",
d.Username, d.Password, d.DBName, d.Host,
)
}
return fmt.Sprintf(
"user=%s password=%s dbname=%s host=%s",
d.Username, d.Password, d.DBName, d.Host,
)
}

type Client struct {
*sqlx.DB
}

func (d *Client) Check(ctx context.Context) error {
return d.PingContext(ctx)
}

func New(ctx context.Context, driverName string, creds *Credentials, production bool) (*Client, error) {
connection, err := sqlx.Open(driverName, creds.SourceString(production))
if err != nil {
return nil, err
}

if err := connection.Ping(); err != nil {
return nil, err
}

return &Client{
connection,
}, nil
}
18 changes: 18 additions & 0 deletions clients/database/mock/database_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package database_mock

import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
)

func NewSQlMock(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock) {
t.Helper()
mockDB, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
if err != nil {
t.Error(err)
}
sqlxDB := sqlx.NewDb(mockDB, "sqlmock")
return sqlxDB, mock
}
28 changes: 28 additions & 0 deletions clients/rabbitmq/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rabbitmq

import (
"context"
"time"

amqp "github.com/rabbitmq/amqp091-go"
)

func Push(ctx context.Context, data []byte) error {
return rabbitMQClient.Push(ctx, data)
}

func UnsafePush(ctx context.Context, data []byte) error {
return rabbitMQClient.UnsafePush(ctx, data)
}

func ReadMsgs(ctx context.Context, maxAmount int, wait time.Duration, callback ReadMsgCallback) {
go rabbitMQClient.ReadMsgs(ctx, maxAmount, wait, callback)
}

func Consume() (<-chan amqp.Delivery, error) {
return rabbitMQClient.Consume()
}

func Close() error {
return rabbitMQClient.Close()
}
168 changes: 168 additions & 0 deletions clients/rabbitmq/delivery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package rabbitmq

import (
"time"

amqp "github.com/rabbitmq/amqp091-go"
)

type RabbitDelivery interface {
// Ack acknowledges processing of a Delivery.
Ack(multiple bool) error
// Nack negatively acknowledges a Delivery.
Nack(multiple bool, requeue bool) error
// Reject rejects a delivery.
Reject(requeue bool) error

// Body returns the message body.
Body() []byte

// Acknowledger provides acknowledgement information.
Acknowledger() amqp.Acknowledger
// Header returns the message header.
Header() amqp.Table
// ContentType returns the message content type.
ContentType() string
// ContentEncoding returns the message content encoding.
ContentEncoding() string
// DeliveryMode returns the delivery mode.
DeliveryMode() uint8
// Priority returns the message priority.
Priority() uint8
// CorelationId returns the correlation id.
CorelationId() string
// ReplyTo returns the reply to value.
ReplyTo() string
// Expiration returns the message expiration.
Expiration() string
// MessageId returns the message id.
MessageId() string
// Timestamp returns the message timestamp.
Timestamp() time.Time
// Type returns the message type.
Type() string
// UserId returns the creating user id.
UserId() string
// AppId returns the creating application id.
AppId() string
// ConsumerTag returns the consumer tag.
ConsumerTag() string
// MessageCount returns the number of messages pending acknowledgement.
MessageCount() uint32
// DeliveryTag returns the delivery tag.
DeliveryTag() uint64
// Redelivered returns true if this message is being redelivered.
Redelivered() bool
// Exchange returns the exchange this message was published to.
Exchange() string
// RoutingKey returns the routing key used when publishing this message.
RoutingKey() string
}

func NewDelivery(d amqp.Delivery) RabbitDelivery {
return &rabbitDelivery{d: d}
}

type rabbitDelivery struct {
d amqp.Delivery
}

func (r *rabbitDelivery) Priority() uint8 {
return r.d.Priority
}

func (r *rabbitDelivery) Header() amqp.Table {
return r.d.Headers
}

func (r *rabbitDelivery) DeliveryMode() uint8 {
return r.d.DeliveryMode
}

func (r *rabbitDelivery) CorelationId() string {
return r.d.CorrelationId
}

func (r *rabbitDelivery) ContentType() string {
return r.d.ContentType
}

func (r *rabbitDelivery) ContentEncoding() string {
return r.d.ContentEncoding
}

func (r *rabbitDelivery) Body() []byte {
return r.d.Body
}

func (r *rabbitDelivery) Acknowledger() amqp.Acknowledger {
return r.d.Acknowledger
}

func (r *rabbitDelivery) Ack(multiple bool) error {
return r.d.Ack(multiple)
}

func (r *rabbitDelivery) Nack(multiple, requeue bool) error {
return r.d.Nack(multiple, requeue)
}

func (r *rabbitDelivery) Reject(requeue bool) error {
return r.d.Reject(requeue)
}

func (r *rabbitDelivery) MessageId() string {
return r.d.MessageId
}

func (r *rabbitDelivery) AppId() string {
return r.d.AppId
}

func (r *rabbitDelivery) Timestamp() time.Time {
return r.d.Timestamp
}

func (r *rabbitDelivery) Type() string {
return r.d.Type
}

func (r *rabbitDelivery) UserId() string {
return r.d.UserId
}

func (r *rabbitDelivery) ConsumerTag() string {
return r.d.ConsumerTag
}

func (r *rabbitDelivery) DeliveryTag() uint64 {
return r.d.DeliveryTag
}

func (r *rabbitDelivery) Redelivered() bool {
return r.d.Redelivered
}

func (r *rabbitDelivery) Exchange() string {
return r.d.Exchange
}

func (r *rabbitDelivery) RoutingKey() string {
return r.d.RoutingKey
}

func (r *rabbitDelivery) CorrelationId() string {
return r.d.CorrelationId
}

func (r *rabbitDelivery) ReplyTo() string {
return r.d.ReplyTo
}

func (r *rabbitDelivery) Expiration() string {
return r.d.Expiration
}

func (r *rabbitDelivery) MessageCount() uint32 {
return r.d.MessageCount
}
Loading

0 comments on commit ac182ba

Please sign in to comment.