http

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2021 License: MIT Imports: 8 Imported by: 0

README

⚙️ Fx Http Module

uber fx module for builtin http server

Installation

go get github.com/lEx0/fx-http-module

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Module = fx.Options(

	fx.Provide(
		mux.NewRouter,
		NewServer,
	),

	fx.Invoke(func(lifecycle fx.Lifecycle, server *http.Server, logger *zap.Logger) {
		lifecycle.Append(fx.Hook{

			OnStart: func(_ context.Context) error {
				listener, err := net.Listen("tcp", server.Addr)
				if err != nil {
					return err
				}

				go func() {
					if err = server.Serve(listener); err != nil {
						logger.Sugar().Fatalw(
							"serve http server failed",
							"error", err,
						)
					}
				}()

				logger.Sugar().Info("http server started")

				return nil
			},

			OnStop: func(ctx context.Context) error {
				logger.Sugar().Info("stopping http server")

				return server.Shutdown(ctx)
			},
		})
	}),
)

Module модуль http сервера

Functions

func NewServer

func NewServer(params Params) (*http.Server, error)

Types

type Options

type Options struct {
	Listen       string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	IdleTimeout  time.Duration
}

type Params

type Params struct {
	fx.In

	Options Options
	Router  *mux.Router
}

Params указываем полями в структуре нужные нам зависимости

Jump to

Keyboard shortcuts

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