mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-23 20:27:42 +00:00
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
|
package cmdutil
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
|
||
|
"github.com/DataDog/datadog-go/statsd"
|
||
|
"github.com/adjust/rmq/v4"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
"github.com/jackc/pgx/v4/pgxpool"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
func NewLogrusLogger(debug bool) *logrus.Logger {
|
||
|
logger := logrus.New()
|
||
|
|
||
|
if debug || os.Getenv("ENV") == "" {
|
||
|
logger.SetLevel(logrus.DebugLevel)
|
||
|
} else {
|
||
|
logger.SetFormatter(&logrus.TextFormatter{
|
||
|
DisableColors: true,
|
||
|
FullTimestamp: true,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return logger
|
||
|
}
|
||
|
|
||
|
func NewStatsdClient(tags ...string) (*statsd.Client, error) {
|
||
|
if env := os.Getenv("ENV"); env != "" {
|
||
|
tags = append(tags, fmt.Sprintf("env:%s", env))
|
||
|
}
|
||
|
|
||
|
return statsd.New("127.0.0.1:8125", statsd.WithTags(tags))
|
||
|
}
|
||
|
|
||
|
func NewRedisClient(ctx context.Context) (*redis.Client, error) {
|
||
|
opt, err := redis.ParseURL(os.Getenv("REDISCLOUD_URL"))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
client := redis.NewClient(opt)
|
||
|
if err := client.Ping(ctx).Err(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return client, nil
|
||
|
}
|
||
|
|
||
|
func NewDatabasePool(ctx context.Context) (*pgxpool.Pool, error) {
|
||
|
url := fmt.Sprintf("%s?sslmode=require", os.Getenv("DATABASE_CONNECTION_POOL_URL"))
|
||
|
config, err := pgxpool.ParseConfig(url)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Setting the build statement cache to nil helps this work with pgbouncer
|
||
|
config.ConnConfig.BuildStatementCache = nil
|
||
|
config.ConnConfig.PreferSimpleProtocol = true
|
||
|
|
||
|
return pgxpool.ConnectConfig(ctx, config)
|
||
|
}
|
||
|
|
||
|
func NewQueueClient(logger *logrus.Logger, conn *redis.Client, identifier string) (rmq.Connection, error) {
|
||
|
errChan := make(chan error, 10)
|
||
|
go func() {
|
||
|
for err := range errChan {
|
||
|
logger.WithFields(logrus.Fields{
|
||
|
"err": err,
|
||
|
}).Error("error occured with queue")
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return rmq.OpenConnectionWithRedisClient(identifier, conn, errChan)
|
||
|
}
|