pantheon

package module
v0.0.0-...-4f633f4 Latest Latest
Warning

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

Go to latest
Published: May 12, 2019 License: GPL-3.0 Imports: 10 Imported by: 0

README

Pantheon

Go Report Card CircleCI

A wrapper framework around Nats for Go. Allows the use of abstracted handlers and context and adds avro schema validation.

Usage


    package main

    import "github.com/valeska-tech/pantheon"

    func main() {
        app := pantheon.NewApp()
        defer app.Run()

        app.Handler("topic.name", handler)
        app.Handler("test.topic", handlerWithSchema).WithAvro("schema.json")
    }

    func handler(ctx *pantheon.Context) {
        data := &DataStuct{}
        ctx.MustBind(data)

        // Do stuff
    }

    func handlerWithSchema(ctx *pantheon.Context) {
        ctx.App.Log.Info("handler")
        ctx.App.Produce("topic.name", nil)
    }

Why?

After moving to Nats from Kafka I found myself building this structure in all my services as a wrapper around Nats.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SchemaDir holds the directory name where schemas are kept
	SchemaDir string
)

Functions

This section is empty.

Types

type Application

type Application struct {
	Log      *logrus.Logger
	Handlers map[string]*HandlerWrapper
	Params   map[string]interface{}
	// contains filtered or unexported fields
}

Application struct takes care of libraries needed to run the app, at the moment this has a single connection to nats, but in the future this will need to be a pooled connection.

func NewApp

func NewApp() *Application

NewApp generates a new application using env vars

func (*Application) Handler

func (a *Application) Handler(subject string, handler EventHandler) *HandlerWrapper

Handler simply adds a new handler to the handlers map

func (*Application) Produce

func (a *Application) Produce(subject string, obj interface{})

Produce puts the given message onto the specified topicz

func (*Application) RegisterHandlers

func (a *Application) RegisterHandlers()

RegisterHandlers loops through the handlers that have been defined and starts consuming

func (*Application) Run

func (a *Application) Run()

Run starts the application daemon, this also starts any consumer

func (*Application) With

func (a *Application) With(key string, param interface{})

With adds a dependency into the params map

type Context

type Context struct {
	App     *Application
	Data    string
	Decoded interface{}
	// contains filtered or unexported fields
}

Context is used to separate nats from the handlers, it abstracts the message and gives them access to the application without the need for global variables

func (*Context) Copy

func (ctx *Context) Copy() *Context

Copy returns a copy of this context

func (*Context) Get

func (ctx *Context) Get(key string) (interface{}, error)

Get fetches a parameter from the application through the context struct

func (*Context) MustBind

func (ctx *Context) MustBind(in interface{})

MustBind ensures that the data from an event binds to the given interface

func (*Context) MustGet

func (ctx *Context) MustGet(key string) interface{}

MustGet either finds the parameter on the application or panics

func (*Context) Unsubscribe

func (ctx *Context) Unsubscribe()

Unsubscribe sends a message to the unsubscribe channel of the handler attached to the current context

type EventHandler

type EventHandler func(ctx *Context)

EventHandler defines the contract for an event handler

type HandlerWrapper

type HandlerWrapper struct {
	Handler EventHandler
	Schema  *avro.Codec
	// contains filtered or unexported fields
}

HandlerWrapper is used to contain an event handler and any options or settings associated with them, you can bind schemas to the wrapper which are evaluated when a message is received before they are sent to the handler.

func NewHandler

func NewHandler(e EventHandler) *HandlerWrapper

NewHandler returns a new HandlerWrapper struct

func (*HandlerWrapper) Listen

func (w *HandlerWrapper) Listen(ctx *Context)

Listen allows the handler to listen to the intake and unsubscribe channels

func (*HandlerWrapper) WithAvro

func (w *HandlerWrapper) WithAvro(schema string)

WithAvro accepts a schema file containing an avro schema this method will panic if the file does not exist or if the file does not contain valid avro

Jump to

Keyboard shortcuts

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