Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fxgcppubsub): Provided module #1

Merged
merged 6 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Go version](https://img.shields.io/badge/Go-≥1.20-blue)](https://go.dev/)
[![codecov](https://codecov.io/gh/ankorstore/yokai-contrib/graph/badge.svg?token=CxImMei31C)](https://codecov.io/gh/ankorstore/yokai-contrib)

> Contrib modules repository for the [Yokai](https://github.com/ankorstore/yokai) framework.

## Modules

| Module | Description |
|--------|-------------|

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

## Contributing

Expand Down
10 changes: 10 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
coverage:
status:
project:
default:
target: 80%
threshold: 1%
patch:
default:
target: 80%
threshold: 1%
65 changes: 65 additions & 0 deletions fxgcppubsub/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
run:
timeout: 5m
concurrency: 8

linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- dogsled
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- forbidigo
- forcetypeassert
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- gofmt
- goheader
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- grouper
- importas
- ineffassign
- interfacebloat
- logrlint
- maintidx
- makezero
- misspell
- nestif
- nilerr
- nilnil
- nlreturn
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- staticcheck
- tenv
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- whitespace
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")})

// ...
}
```
Loading
Loading