fxt

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2018 License: MIT Imports: 2 Imported by: 3

README

Extensions for Uber's fx

Build Status Go Report Card GoDoc

fxt is an application builder kit mainly based on go.uber.org/fx.

Installation

Since this library uses Glide I recommend using it in your project as well.

$ glide get github.com/goph/fxt

License

The MIT License (MIT). Please see License File for more information.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Bootstrap = fx.Provide(NewLifecycle)

Bootstrap can be used as an alias for must have provider options composed into one fx option.

Functions

func NewLifecycle

func NewLifecycle(l fx.Lifecycle) (Lifecycle, Closer)

NewLifecycle creates a new lifecycle required by all constructors providing closers. It requires an fx.Lifecycle instance to register OnStart and OnStop hooks in.

func WithTimeout added in v0.3.0

func WithTimeout(ctx context.Context, f func(context.Context) error) error

WithTimeout makes a function susceptible to deadlines.

Types

type App added in v0.12.0

type App struct {
	*fx.App
	// contains filtered or unexported fields
}

App wraps the original fx.App instance and adds a closer method to it.

func New added in v0.12.0

func New(opts ...fx.Option) *App

New creates a new App and in turn an fx.App instance.

func (*App) Close added in v0.12.0

func (a *App) Close() error

Close invokes the OnClose hook of the extended lifecycle.

type ApplicationInfo added in v0.15.0

type ApplicationInfo struct {
	Version    string
	CommitHash string
	BuildDate  string
}

ApplicationInfo is an optional set of information that can be set by the runtime environment (eg. console application).

type Closer

type Closer interface {
	Close() error
}

Closer is the interface modelled after io.Closer for post-shutdown cleanups.

type Hook

type Hook struct {
	OnStart func(context.Context) error
	OnStop  func(context.Context) error
	OnClose func() error
}

Hook is a set of callbacks extending fx.Hook. It adds an OnClose hook which allows post-shutdown cleanups.

type Lifecycle

type Lifecycle interface {
	Append(Hook)
}

Lifecycle extends fx.Lifecycle by adding the extra OnClose in Hook.

Example
package main

import (
	"fmt"

	"github.com/goph/fxt"
	"go.uber.org/fx"
)

func main() {
	var ctx struct {
		Closer fxt.Closer
	}

	type A struct{}

	fx.New(
		fx.Provide(fxt.NewLifecycle),
		fx.Provide(func(l fxt.Lifecycle) *A {
			l.Append(fxt.Hook{
				// OnStart and OnStop are valid fx hooks as well.
				OnClose: func() error {
					fmt.Print("closing")

					return nil
				},
			})

			return &A{}
		}),
		fx.Extract(&ctx),
		fx.Invoke(func(a *A) {}),
		fx.NopLogger,
	)
	defer ctx.Closer.Close()

}
Output:

closing

Directories

Path Synopsis
database
sql
middleware/correlationid/internal/mocks
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0
metrics
is

Jump to

Keyboard shortcuts

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