fx-medium-example

command module
v0.0.0-...-72ff5c6 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2020 License: MIT Imports: 12 Imported by: 0

README

Fx Medium Example

Full blog post can be found here https://medium.com/@erez.levi/using-uber-fx-to-simplify-dependency-injection-875363245c4c

To Install

go get github.com/ErezLevip/fx-medium-example

To Run

go run main.go

TL:DR

The main looks like this:

func main() {
  ctx, cancel := context.WithCancel(context.Background())
  kill := make(chan os.Signal, 1)
  signal.Notify(kill)

  go func() {
	  <-kill
	  cancel()
  }()

  app := fx.New(
	  fx.Provide(newZapLogger),
	  fx.Provide(newRedisClient),
	  fx.Provide(cache.NewMeaningOfLifeCacheRedis),
	  fx.Provide(handlers.NewMeaningOfLifeHandler),
	  fx.Invoke(runHttpServer),
  )
  if err := app.Start(ctx); err != nil {
	  fmt.Println(err)
  }
}

func runHttpServer(lifecycle fx.Lifecycle, molHandler *handlers.MeaningOfLife) {
lifecycle.Append(fx.Hook{OnStart: func(context.Context) error {
	r := fasthttprouter.New()
	r.Handle(http.MethodGet, "/what-is-the-meaning-of-life", molHandler.Handle)
	return fasthttp.ListenAndServe("localhost:8080", r.Handler)
}})}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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