http

package
v0.0.0-...-9c24fb4 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2022 License: MIT Imports: 5 Imported by: 0

README

HTTP package

MakeServerRunner

See example:

package main

import (
	"context"
	"io"
	"net/http"
	"os"
	"os/signal"
	"syscall"

	libHTTP "git.wildberries.ru/courier/libraries/http"
	"github.com/rs/zerolog/log"
	"golang.org/x/sync/errgroup"
)

func main() {

	ctx, cancel := context.WithCancel(context.Background())
	go func() {
		shutdown := make(chan os.Signal, 1)
		signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
		log.Info().Str("signal", (<-shutdown).String()).Msg("signal received")
		cancel()
	}()

	r := http.NewServeMux()
	r.HandleFunc("/status", func(writer http.ResponseWriter, request *http.Request) {
		_, _ = io.WriteString(writer, "ok")
	})

	server := &http.Server{
		Addr:    ":8888",
		Handler: r}

	group, groupCtx := errgroup.WithContext(ctx)
    // groupCtx will expire when ctx expires or one of the jobs returns an error
	group.Go(libHTTP.MakeServerRunner(
		groupCtx,
		log.Logger.With().Str("http_server", "internal API").Logger(),
		server))
	group.Go(func() error {
		// another job
		return nil
	})

	if err := group.Wait(); err != nil {
		log.Error().Err(err).Msg("one of job has been failed")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeServerRunner

func MakeServerRunner(ctx context.Context, logger zerolog.Logger, server *http.Server) func() error

MakeServerRunner creates closure for running HTTP server in separate goroutine

Types

This section is empty.

Jump to

Keyboard shortcuts

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