cfutil

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2019 License: MIT Imports: 21 Imported by: 0

README

CFUTIL

This package provides a number of convenience functions for apps running in Cloudfoundry In case the app is started outside of Cloudfoundry an attempt is made to simulate the Cloudfoundry environment

Simulating Cloudfoundry Services

When running locally the app looks for the following environment variables

  • CF_LOCAL_POSTGRES
  • CF_LOCAL_SMTP
  • CF_LOCAL_RABBITMQ

Services are setup using the variable values as the URI. This allows you to use local Postgres, SMTP and RabbitMQ services just as you would in an actual Cloudfoundry deployment

License

MIT

Documentation

Overview

Package cfutil provides a number of convenience functions to apps being developed for use in a Cloudfoundry environment. For local development it can simulate the CF environment variables that your app typically expects. It can also emulate Cloudfoundry services based on environment variables.

Index

Constants

View Source
const KeyCorrelationID = "correlationid" // TODO: get rid of this magic

Variables

This section is empty.

Functions

func CreateURLFromServiceCatalog

func CreateURLFromServiceCatalog(catalog *consul.CatalogService) (string, error)

func Current

func Current() (appEnv *cfenv.App, err error)

func ForceHTTP

func ForceHTTP() bool

ForceHTTP() returns true if the environment variable `FORCE_HTTP` contains `true`. This is useful for example when you are running your app locally and don't have access to working TLS certificates.

func GetAppIndex

func GetAppIndex() int

GetAppIndex() returns the app index when running in Cloudfoundry.

func GetApplicationName

func GetApplicationName() (string, error)

GetApplicationName() returns the name of the app within Cloudfoundry

func GetHostname

func GetHostname() (string, error)

GetHostname() returns the (first) hostname designated to your app within Cloudfoundry

func Getenv

func Getenv(name string) string

Getenv(name) returns the environment variable value of ENV `name`

func IsFirstInstance

func IsFirstInstance() bool

IsFirstInstance() returns true if the instance calling it is the first running instance of the app within Cloudfoundry. This is useful if you want to for example trigger database migrations but only want to execute these on the first starting instance.

func IsLocal

func IsLocal() bool

func ListenString

func ListenString() string

ListenString() returns the listen string based on the `PORT` environment variable value

func MattermostDSN

func MattermostDSN(serviceName string) (*url.URL, error)

func Migrate

func Migrate(connectString, path string) ([]error, bool)

Migrate() starts a database migration for the given database specified in `connectString`. It looks in the `path` for the migration files.

func NewConnection

func NewConnection(driver, name string) (conn *sqlx.DB, connectString string, err error)

NewConnection opens a new database connection given the driver and CF service name. Currently only postgres is supported.

func RabbitMQAdminURI

func RabbitMQAdminURI(serviceName string) (string, error)

func SentryDSN

func SentryDSN(serviceName string) (string, error)

Types

type ConsulClient added in v0.2.0

type ConsulClient struct {
	consul.Client
	Namespace string
	Token     string
}

func NewConsulClient

func NewConsulClient(server, namespace, token string) (*ConsulClient, error)

NewConsulClient() returns a new consul client which you can use to access the Consul cluster HTTP API. It uses `CONSUL_MASTER` and `CONSUL_TOKEN` environment variables to set up the HTTP API connection.

func (*ConsulClient) ConsulDatacenter added in v0.2.0

func (client *ConsulClient) ConsulDatacenter() (string, error)

func (*ConsulClient) DiscoverServiceURL added in v0.2.0

func (client *ConsulClient) DiscoverServiceURL(serviceName, tags string) (string, error)

func (*ConsulClient) GetConsulKey added in v0.2.0

func (client *ConsulClient) GetConsulKey(mooncoreKey string) (string, error)

func (*ConsulClient) GetConsulKeyValue added in v0.2.0

func (client *ConsulClient) GetConsulKeyValue(mooncoreKey string) (string, error)

func (*ConsulClient) ServiceRegister added in v0.2.0

func (client *ConsulClient) ServiceRegister(name string, path string, tags ...string) error

Use ServiceRegister() to register your app in the Consul cluster Optionally you can provide a health endpoint on your URL and a number of tags to make your service more discoverable

func (*ConsulClient) Services added in v0.2.0

func (client *ConsulClient) Services() ([]string, error)

Services() returns the list of services available from the Consul cluster

type Consumer

type Consumer struct {
	// contains filtered or unexported fields
}

func NewConsumer

func NewConsumer(config ConsumerConfig) (*Consumer, error)

func (*Consumer) AnnounceQueue

func (c *Consumer) AnnounceQueue(queueName, bindingKey string) (<-chan amqp.Delivery, error)

AnnounceQueue sets the queue that will be listened to for this connection...

func (*Consumer) Connect

func (c *Consumer) Connect() error

Connect to RabbitMQ server

func (*Consumer) Handle

func (c *Consumer) Handle(
	d <-chan amqp.Delivery,
	fn ConsumerHandlerFunc,
	threads int,
	queue string,
	routingKey string)

Handle has all the logic to make sure your program keeps running d should be a delievey channel as created when you call AnnounceQueue fn should be a function that handles the processing of deliveries this should be the last thing called in main as code under it will become unreachable unless put int a goroutine. The q and rk params are redundent but allow you to have multiple queue listeners in main without them you would be tied into only using one queue per connection

func (*Consumer) ReConnect

func (c *Consumer) ReConnect(queueName, bindingKey string) (<-chan amqp.Delivery, error)

ReConnect is called in places where NotifyClose() channel is called wait 30 seconds before trying to reconnect. Any shorter amount of time will likely destroy the error log while waiting for servers to come back online. This requires two parameters which is just to satisfy the AccounceQueue call and allows greater flexability

func (*Consumer) Start

func (c *Consumer) Start() error

type ConsumerConfig

type ConsumerConfig struct {
	ServiceName  string
	Exchange     string
	ExchangeType string
	QueueName    string
	RoutingKey   string
	CTag         string
	HandlerFunc  ConsumerHandlerFunc
	// contains filtered or unexported fields
}

type ConsumerHandlerFunc

type ConsumerHandlerFunc func(deliveries <-chan amqp.Delivery) error

type DefaultLogger added in v0.2.0

type DefaultLogger struct {
}

func (DefaultLogger) Critical added in v0.2.0

func (l DefaultLogger) Critical(c context.Context, format string, args ...interface{})

func (DefaultLogger) Debug added in v0.2.0

func (l DefaultLogger) Debug(c context.Context, format string, args ...interface{})

func (DefaultLogger) Error added in v0.2.0

func (l DefaultLogger) Error(c context.Context, format string, args ...interface{})

func (DefaultLogger) Info added in v0.2.0

func (l DefaultLogger) Info(c context.Context, format string, args ...interface{})

func (DefaultLogger) Raw added in v0.2.0

func (l DefaultLogger) Raw(c context.Context, rawMessage string)

func (DefaultLogger) Warning added in v0.2.0

func (l DefaultLogger) Warning(c context.Context, format string, args ...interface{})

type HSDPLogger

type HSDPLogger struct {
	// contains filtered or unexported fields
}

func (HSDPLogger) Critical

func (f HSDPLogger) Critical(c context.Context, format string, args ...interface{})

func (HSDPLogger) Debug

func (f HSDPLogger) Debug(c context.Context, format string, args ...interface{})

func (HSDPLogger) Error

func (f HSDPLogger) Error(c context.Context, format string, args ...interface{})

func (*HSDPLogger) Format

func (f *HSDPLogger) Format(entry *logrus.Entry) ([]byte, error)

func (HSDPLogger) Info

func (f HSDPLogger) Info(c context.Context, format string, args ...interface{})

func (HSDPLogger) Raw

func (f HSDPLogger) Raw(c context.Context, rawString string)

func (HSDPLogger) Warning

func (f HSDPLogger) Warning(c context.Context, format string, args ...interface{})

type Logger

type Logger interface {
	Debug(c context.Context, format string, args ...interface{})
	Info(c context.Context, format string, args ...interface{})
	Warning(c context.Context, format string, args ...interface{})
	Error(c context.Context, format string, args ...interface{})
	Critical(c context.Context, format string, args ...interface{})
	Raw(c context.Context, rawMessage string)
}

func NewLogger

func NewLogger(config LoggerConfig) Logger

type LoggerConfig added in v0.2.0

type LoggerConfig struct {
	AppName       string
	AppVersion    string
	AppInstance   string
	AppComponent  string
	CorrelationID string
}

type PHService

type PHService struct {
	Type            string `json:"type"`
	ApplicationName string `json:"application_name"`
	PropositionName string `json:"proposition_name"`
	BaseURL         string `json:"base_url"`
	SharedKey       string `json:"shared_key"`
	SharedSecret    string `json:"shared_secret"`
	Client          string `json:"client"`
	Password        string `json:"password"`
}

func ConnectPHService

func ConnectPHService(phServiceType, serviceName string) (*PHService, error)

type Producer

type Producer struct {
	// contains filtered or unexported fields
}

func NewProducer

func NewProducer(config ProducerConfig) (*Producer, error)

func (*Producer) Close

func (p *Producer) Close()

func (*Producer) Publish

func (p *Producer) Publish(exchange, routingKey string, msg amqp.Publishing) error

type ProducerConfig

type ProducerConfig struct {
	ServiceName  string
	Exchange     string
	ExchangeType string
	// contains filtered or unexported fields
}

type SMTPService

type SMTPService struct {
	url.URL
	Authentication     string
	EnableStartTLSAuto string
	Username           string
	Password           string
}

func FindSMTPService

func FindSMTPService(serviceName string) (*SMTPService, error)

type Value

type Value struct {
	Message string `json:"message"`
}

type VaultClient

type VaultClient struct {
	vault.Client
	Endpoint           string
	RoleID             string
	SecretID           string
	ServiceSecretPath  string
	ServiceTransitPath string
	SpaceSecretPath    string
	OrgSecretPath      string
	Secret             *vault.Secret
}

func NewVaultClient

func NewVaultClient(serviceName string) (*VaultClient, error)

func (*VaultClient) Login

func (v *VaultClient) Login() (err error)

func (*VaultClient) ReadOrgString

func (v *VaultClient) ReadOrgString(path string) (string, error)

func (*VaultClient) ReadSpaceString

func (v *VaultClient) ReadSpaceString(path string) (string, error)

func (*VaultClient) ReadString

func (v *VaultClient) ReadString(prefix, path string) (string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL