-
Notifications
You must be signed in to change notification settings - Fork 0
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 #8 from Moranilt/feature/clients
Feature/clients
- Loading branch information
Showing
14 changed files
with
1,484 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Clients | ||
Default clients for http_template |
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,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 | ||
} |
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,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 | ||
} |
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,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() | ||
} |
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,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 | ||
} |
Oops, something went wrong.