/httpx

Primary LanguageGoMIT LicenseMIT

httpx

License go.mod Go version GoDoc Latest tag Go Report

Useful set of functions and middlewares to use on top of net/http.

Motivations

On any project where I need a http server, I find myself copy/pasting the same boilerplate over and over again. This project aims at keeping all that code reusable and properly tested.

func runServer(ctx context.Context) error {
	var (
		address string
		handler http.HandlerFunc
		logger  *slog.Logger
	)

	listener, err := httpx.NewListener(ctx, address)
	if err != nil {
		return fmt.Errorf("unable to create listener: %w", err)
	}

	server := httpx.NewServer(handler, httpx.ServerWithErrorLogger(slog.NewLogLogger(logger.Handler(), slog.LevelWarn)))
	return httpx.Serve(ctx, server, listener, time.Second*15)
}

License

This project is under the MIT licence, please see the LICENCE file.