pubsub

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2020 License: MIT Imports: 5 Imported by: 0

README

pubsub

Google Cloud's PubSub request middleware for Fiber. This middleware handle the validation of PubSub request and its payload decoding.

Install

go get -u github.com/fiberweb/pubsub

Usage

package main

import (
  "encoding/json"
  "fmt"
  
  "github.com/gofiber/fiber"
  "github.com/fiberweb/pubsub"
)

// our PubSub data structure
type User struct {
  Name  string `json:"name"`
  Age   int    `json:"age"`
}

func main() {
  app := fiber.New()
  
  // use the middleware
  app.Use(pubsub.New())
  app.Post("/", func(c *fiber.Ctx) {
    msg := c.Locals(pubsub.LocalsKey).(*pubsub.Message)
    
    var user User
    if err := json.Unmarshal(msg.Message.Data, &user); err != nil {
      c.SendStatus(400)
      return
    }
    
    fmt.Println(user.Name, user.Age)
    c.Send("Ok")
  })
  app.Listen("8080")
}

When the middleware successfully decode the message, PubSub data will be available for the next handlers inside the Fiber context Locals called PubSubMessage.

Configuration

You could also initialize the middleware with a config:

app.Use(pubsub.New(pubsub.Config{
  Debug: false,
}))

This middleware has only two configuration options:

type Config struct {
	// Skip this middleware
	Skip func(*fiber.Ctx) bool
  
	// Debug true will log any errors during validation 
        // and marshalling and log the unmarshalled payload, default: true
	Debug bool
}
Example of using Skip
app.Use(pubsub.New(pubsub.Config{
    Skip: func(c *fiber.Ctx) bool {
      // add your logic here
      return true // returning true will skip this middleware.
    }
}))

Documentation

Index

Constants

View Source
const LocalsKey = "PubSubMessage"

LocalsKey is Fiber Locals key that you should use to get PubSubMessage

Variables

This section is empty.

Functions

func New

func New(config ...Config) func(*fiber.Ctx)

New returns the middleware

Types

type Config

type Config struct {
	// Skip this middleware
	Skip func(*fiber.Ctx) bool
	// Debug true will log the unmarshalled payload
	Debug bool
}

Config is this middleware configuration

type Message added in v1.1.0

type Message struct {
	Message struct {
		ID          string                 `json:"message_id"`
		Data        []byte                 `json:"data"`
		Attributes  map[string]interface{} `json:"attributes"`
		PublishTime string                 `json:"publish_time"`
	} `json:"message"`
	Subscription string `json:"subscription"`
}

Message is a format of the pubsub payload

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳