fxt

package module
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 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

Minimum Go version: 1.9

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

$ glide get github.com/goph/fxt

Usage

As you will see, the top level (and some deeper level) packages have the fx prefix in their package name, but not in their directory name. The reason for the prefix is simple: avoiding name collisions. This is however not really idiomatic Go: although the package name can be different from the directory name, it is recommended that they are the same for clarity. In this case the prefix is consistent though and I found it would probably be harder to read directory names, so I left the original versions there. Ideally an IDE should be able to handle this.

I'm going to continue using and testing it and might change the directory names as well if it turns out to be too confusing.

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

Jump to

Keyboard shortcuts

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