fxgcppubsub

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 9 Imported by: 0

README

Fx GCP Pub/Sub Module

ci go report codecov Deps PkgGoDev

Fx module for GCP Pub/Sub.

Overview

This module provides to your Fx application a pubsub.Client, that you can inject anywhere to publish or subscribe on a Pub/Sub instance.

Installation

First install the module:

go get github.com/ankorstore/yokai-contrib/fxgcppubsub

Then activate it in your application bootstrapper:

// internal/bootstrap.go
package internal

import (
	"github.com/ankorstore/yokai/fxcore"
	"github.com/ankorstore/yokai-contrib/fxgcppubsub"
)

var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
	// load fxgcppubsub module
	fxgcppubsub.FxGcpPubSubModule,
	// ...
)

Configuration

Configuration reference:

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: true
modules:
  gcppubsub:
    project:
      id: ${GCP_PROJECT_ID}  # GCP project id
    healthcheck:
      topics:                # list of topics to check for the topics probe
        - topic1
        - topic2
      subscriptions:         # list of subscriptions to check for the subscriptions probe
        - subscription1
        - subscription2

Health Check

This module provides ready to use health check probes, to be used by the fxhealthcheck module:

To activate those probes, you just need to register them:

// internal/services.go
package internal

import (
	"github.com/ankorstore/yokai/fxhealthcheck"
	"github.com/ankorstore/yokai-contrib/fxgcppubsub/healthcheck"
	"go.uber.org/fx"
)

func ProvideServices() fx.Option {
	return fx.Options(
		// register the GcpPubSubTopicsProbe
		fxhealthcheck.AsCheckerProbe(healthcheck.NewGcpPubSubTopicsProbe),
		// register the GcpPubSubSubscriptionsProbe
		fxhealthcheck.AsCheckerProbe(healthcheck.NewGcpPubSubSubscriptionsProbe),
		// ...
	)
}

If your application is interested only in publishing, activate the GcpPubSubTopicsProbe only.

If it is interested only in subscribing, activate the GcpPubSubSubscriptionsProbe only.

Testing

In test mode, this client is configured to work with a ptest.Server, avoiding the need to run any Pub/Sub instance, for better tests portability.

// internal/example/example_test.go
package example_test

import (
	"context"
	"testing"

	"cloud.google.com/go/pubsub"
	"github.com/foo/bar/internal"
	"github.com/stretchr/testify/assert"
	"go.uber.org/fx"
)

func TestExample(t *testing.T) {
	var client *pubsub.Client

	internal.RunTest(t, fx.Populate(&client))

	ctx := context.Background()
	
	// prepare test topic on test server
	topic, err := client.CreateTopic(ctx, "test-topic")
	assert.NoError(t, err)

	// public on test topic
	topic.Publish(ctx, &pubsub.Message{Data: []byte("test message")})
	
	// ...
}

Documentation

Index

Constants

View Source
const ModuleName = "gcppubsub"

ModuleName is the module name.

Variables

View Source
var FxGcpPubSubModule = fx.Module(
	ModuleName,
	fx.Provide(
		NewFxGcpPubSubClient,
	),
)

FxGcpPubSubModule is the Fx pubsub module.

Functions

func NewFxGcpPubSubClient

func NewFxGcpPubSubClient(p FxGcpPubSubClientParam) (*pubsub.Client, error)

NewFxGcpPubSubClient returns a pubsub.Client.

Types

type FxGcpPubSubClientParam

type FxGcpPubSubClientParam struct {
	fx.In
	LifeCycle fx.Lifecycle
	Config    *config.Config
}

FxGcpPubSubClientParam allows injection of the required dependencies in NewFxGcpPubSubClient.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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