Fx GCP Pub/Sub Module
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")})
// ...
}