fxglue

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2022 License: MIT Imports: 11 Imported by: 0

README

Uber Fx Helpers

Some helper for the https://github.com/uber-go/fx

CI

Install

go get -u github.com/albenik/fx-glue

Use

package main

import (
    "net/http"

    "github.com/albenik/fx-glue"
    "github.com/albenik/huenv"
    "github.com/rs/zerolog"
    "github.com/uber-go/fx"
)

type Config struct {
    App        fxglue.AppConfig
    ListenAddr string `fx:"supply,name=listen_addr"`
}

func main() {
    conf := new(Config)
    if err := huenv.Init(conf); err != nil {
		panic(err)
    }

    mod := fx.Module("app",
        fxglue.SupplyConfig(conf),
        fx.Provide(
            func() http.Handler {
                return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                    // ...
                })
            },
        ),
        fx.Invoke(listenAndServer),
    )

    app := fx.New(mod)
    app.Run()
}

type listenAndServeArgs struct {
    fx.In

    ListenAddr string `name:"listen_addr"`
    Handler    http.Handler
    Log        *zerolog.Logger
}

func listenAndServe(lc fx.Lifecycle, args listenAndServeArgs) {
    srv := &http.Server{
        Addr:    args.ListenAddr,
        Handler: args.Handler,
    }
    lc.Append(fxglue.NewHTTPServerHook(srv, args.Log))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFxApplicationConfigMissing = errors.New("fx application config missing")

Functions

func NewHTTPServerHook

func NewHTTPServerHook(srv *http.Server, log *zerolog.Logger) fx.Hook

func PrintErrorHandler

func PrintErrorHandler(w io.Writer) fx.ErrorHandler

func SupplyConfig

func SupplyConfig(conf interface{}) fx.Option

Types

type AppConfig

type AppConfig struct {
	StartTimeout time.Duration `env:"APP_START_TIMEOUT"`
	StopTimeout  time.Duration `env:"APP_STOP_TIMEOUT"`
}

type ErrorHandlerFunc

type ErrorHandlerFunc func(err error)

func (ErrorHandlerFunc) HandleError

func (fn ErrorHandlerFunc) HandleError(err error)

Jump to

Keyboard shortcuts

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