Skip to content

Commit

Permalink
feat(fxgcppubsub): Provided module
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Mar 5, 2024
1 parent cd570b9 commit 5ab3048
Show file tree
Hide file tree
Showing 21 changed files with 716 additions and 253 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: coverage

on:
push:
branches:
- main
workflow_call:
inputs:
go_version:
required: false
type: string
default: "1.20.0"

permissions:
contents: write
pull-requests: write

jobs:
coverage:
runs-on: ubuntu-latest
strategy:
matrix:
module:
- "fxgcppubsub"
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ inputs.go_version }}
- name: Install dependencies for module ${{ matrix.module }}
working-directory: ${{ matrix.module }}
run: go mod download
- name: Run tests for module ${{ matrix.module }}
working-directory: ${{ matrix.module }}
run: go test -v -race -failfast -coverpkg=./... -covermode=atomic -coverprofile=coverage.tmp.out ./...
- name: Exclude testdata
working-directory: ${{ matrix.module}}
run: cat coverage.tmp.out | grep -v "testdata" > coverage.out
- name: Clean temp coverage
working-directory: ${{ matrix.module }}
run: rm coverage.tmp.out
- name: Codecov for module ${{ matrix.module }}
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: false
flags: ${{ matrix.module }}
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
31 changes: 31 additions & 0 deletions .github/workflows/fxgcppubsub-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "fxgcppubsub-ci"

on:
push:
branches:
- "feat**"
- "fix**"
- "hotfix**"
- "chore**"
paths:
- "fxgcppubsub/**.go"
- "fxgcppubsub/go.mod"
- "fxgcppubsub/go.sum"
pull_request:
types:
- opened
- synchronize
- reopened
branches:
- main
paths:
- "fxgcppubsub/**.go"
- "fxgcppubsub/go.mod"
- "fxgcppubsub/go.sum"

jobs:
ci:
uses: ./.github/workflows/common-ci.yml
secrets: inherit
with:
module: "fxgcppubsub"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
## Modules

| Module | Description |
|----------|-----------------------------------------------------------|
| fxpubsub | subscribing to [Pub/Sub](https://cloud.google.com/pubsub) |
| Module | Description |
|-------------|--------------------------------------------------------------|
| fxgcppubsub | Module for [Google Pub/Sub](https://cloud.google.com/pubsub) |

## Contributing

Expand Down
145 changes: 145 additions & 0 deletions fxgcppubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Fx Google Pub/Sub Module

[![ci](https://github.com/ankorstore/yokai/actions/workflows/fxgcppubsub-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/fxgcppubsub-ci.yml)
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/fxgcppubsub)](https://goreportcard.com/report/github.com/ankorstore/yokai/fxgcppubsub)
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR&flag=fxgcppubsub)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxgcppubsub)
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxgcppubsub)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxgcppubsub)](https://pkg.go.dev/github.com/ankorstore/yokai/fxgcppubsub)

> [Fx](https://uber-go.github.io/fx/) module for [Google Pub/Sub](https://cloud.google.com/pubsub).
<!-- TOC -->
* [Overview](#overview)
* [Installation](#installation)
* [Configuration](#configuration)
* [Health Check](#health-check)
* [Testing](#testing)
<!-- TOC -->

## Overview

This module provides to your Fx application a [pubsub.Client](https://pkg.go.dev/cloud.google.com/go/pubsub#Client),
that you can `inject` anywhere to `publish` or `subscribe` on a `Pub/Sub` instance.

## Installation

First install the module:

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

Then activate it in your application bootstrapper:

```go
// 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:

```yaml
# ./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](https://ankorstore.github.io/yokai/modules/fxhealthcheck/) module:
- [GcpPubSubTopicsProbe](healthcheck/topic.go): to check existence of the topics in `modules.gcppubsub.healthcheck.topics`
- [GcpPubSubSubscriptionsProbe](healthcheck/subscription.go): to check existence of topics in `modules.gcppubsub.healthcheck.subscriptions`

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

```go
// 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](module.go), avoiding the need to run any `Pub/Sub`
instance, for better tests portability.

```go
// 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")})
// ...
}
```
7 changes: 1 addition & 6 deletions fxpubsub/go.mod → fxgcppubsub/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module github.com/ankorstore/yokai-contrib/fxpubsub
module github.com/ankorstore/yokai-contrib/fxgcppubsub

go 1.20.0

require (
cloud.google.com/go/pubsub v1.36.1
github.com/ankorstore/yokai/config v1.2.0
github.com/ankorstore/yokai/fxconfig v1.0.0
github.com/ankorstore/yokai/fxlog v1.0.0
github.com/ankorstore/yokai/healthcheck v1.0.0
github.com/stretchr/testify v1.8.4
go.uber.org/fx v1.20.1
Expand All @@ -19,7 +18,6 @@ require (
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
github.com/ankorstore/yokai/log v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand All @@ -33,13 +31,10 @@ require (
github.com/googleapis/gax-go/v2 v2.12.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
18 changes: 0 additions & 18 deletions fxpubsub/go.sum → fxgcppubsub/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ github.com/ankorstore/yokai/config v1.2.0 h1:OqgrPGGIWEJoXEKGsveSBwLPGMnI3ZZBvIf
github.com/ankorstore/yokai/config v1.2.0/go.mod h1:yDANaMWIOfAUkAMClG22Q4bzQk91NLwWK3WbL5IFnbg=
github.com/ankorstore/yokai/fxconfig v1.0.0 h1:zaYOLfpurqFJuS/IHeAXPOzrCNuttsFjJWNOK39opR4=
github.com/ankorstore/yokai/fxconfig v1.0.0/go.mod h1:p+x6Jp8aLv1+uE1qO42KF+yahBK+VJdPP1/YReBjJ7M=
github.com/ankorstore/yokai/fxlog v1.0.0 h1:ujq/XxgCK0uwKCNSt86XEYR2vqYbXZX2/lA/pQHZX4A=
github.com/ankorstore/yokai/fxlog v1.0.0/go.mod h1:juQnBYNddDVOa7Ukhw8axLYWyibDDMJAwG7MDpluKnk=
github.com/ankorstore/yokai/healthcheck v1.0.0 h1:uX6RrchsvbxCV70dh5d6RX5LEuGIf+Pt+14waV0CzY0=
github.com/ankorstore/yokai/healthcheck v1.0.0/go.mod h1:Frz73NuG8ruLDz04vQxzf0bWhKK1Ru2Ktod+3ltaIxs=
github.com/ankorstore/yokai/log v1.0.0 h1:9NsM0J+1O028WuNDW7vr0yeUdWDX1JKYTkuz7hiYCSs=
github.com/ankorstore/yokai/log v1.0.0/go.mod h1:lyBRVA8VkrmlNjaR2jVTH9XjV06ioolWTuDVN6wF0vk=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa h1:jQCWAUqqlij9Pgj2i/PB79y4KOPYVyFYdROxgaCwdTQ=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand All @@ -46,7 +41,6 @@ github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
Expand Down Expand Up @@ -91,25 +85,16 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
Expand Down Expand Up @@ -203,9 +188,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
Loading

0 comments on commit 5ab3048

Please sign in to comment.