pubsubmit

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: MIT Imports: 5 Imported by: 2

README

pubsubmit

Create Go microservices that publish data to a Pub/Sub topic.

Very easy to use, create a working service by adding just one line of code to your main method:

pubsubmit.Run()

Examples

Try working examples of pubsubmit here

Getting started

Create a new Go project

mkdir pubsubmit-example
cd pubsubmit-example    
go mod init pubsubmit/example

Get pubsubmit

go get github.com/jonnyorman/pubsubmit

Add a main.go file with the following

package main

import "github.com/jonnyorman/pubsubmit"

func main() {
	pubsubmit.Run()
}

Add a pubsubmit-config.json file with the following

{
    "projectID": "your-firebase-project",
    "collectionName": "entities",
    "operation": "create"
}

Tidy and run with access to a Firebase project or emulator

    go mod tidy
    go run .

Submit a POST to the service with a body. The body data will get published as a message to a topic called entities-create-submit.

You can also create a struct with the data you want to publish in the message. Create a struct and use it:

package main

import "github.com/jonnyorman/pubsubmit"

type EntityModel struct {
	Prop1 string
	Prop2 int
}

func main() {
	pubsubmit.RunTyped[EntityModel]()
}

Environment configuration

The configuration can also be provided by the environment with the following keys:

  • projectID - PROJECT_ID
  • collectionName - COLLECTION_NAME
  • operation - OPERATION

A combination of the pubsubmit-config.json file and environment variables can be used. For example, the project ID could be provided as the PROJECT_ID environment variable, while the collection name and operation are provided with the following configuration file:

{
    "collectionName": "entities",
    "operation": "create"
}

If a configuration value is provided in both pubsubmit-config.json and the environment, then the configuration file with take priority. For example, if the PROJECT_ID envronment varialbe has value "env-project-id" and the following pubsubmit-config.json file is provided:

{
    "projectID": "config-project-id"
}

then the project ID will be "config-project-id".

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildApplication

func BuildApplication[T any]() *fireworks.Application

func Run

func Run()

func RunTyped

func RunTyped[T any]()

Types

type Configuration

type Configuration struct {
	fireworks.Configuration
	Operation string
}

func GenerateConfiguration

func GenerateConfiguration(fileName string) *Configuration

func NewConfiguration

func NewConfiguration(
	fireworksConfiguration fireworks.Configuration,
	operation string) *Configuration

type DataPublisher

type DataPublisher[T any] interface {
	Publish(data T)
}

type GinRequestBodyReader

type GinRequestBodyReader[T any] struct {
	// contains filtered or unexported fields
}

func NewGinRequestBodyReader

func NewGinRequestBodyReader[T any](
	reader fireworks.Reader,
	bodyDeserialiser fireworks.DataDeserialiser[T],
) *GinRequestBodyReader[T]

func (GinRequestBodyReader[T]) Read

func (this GinRequestBodyReader[T]) Read(ginContext *gin.Context) T

type HttpRequestHandler

type HttpRequestHandler[T any] struct {
	// contains filtered or unexported fields
}

func NewHttpRequestHandler

func NewHttpRequestHandler[T any](
	dataReader fireworks.DataReader[T],
	dataPublisher DataPublisher[T]) *HttpRequestHandler[T]

func (HttpRequestHandler[T]) Handle

func (this HttpRequestHandler[T]) Handle(ginContext *gin.Context)

type PubSubDataPublisher

type PubSubDataPublisher[T any] struct {
	// contains filtered or unexported fields
}

func NewPubSubDataPublisher

func NewPubSubDataPublisher[T any](configuration *Configuration) *PubSubDataPublisher[T]

func (PubSubDataPublisher[T]) Publish

func (this PubSubDataPublisher[T]) Publish(data T)

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

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