<repo_name>djghostghost/go-lesson-third-week<file_sep>/main.go
package main

import (
	"context"
	"fmt"
	"golang.org/x/sync/errgroup"
	"log"
	"net/http"
	"os"
	"os/signal"
)

func main() {

	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	eg, ctx := errgroup.WithContext(ctx)

	srv := &http.Server{Addr: ":8040"}

	eg.Go(func() error {
		return serverApp(srv)
	})

	eg.Go(func() error {
		<-ctx.Done()
		fmt.Println("Shut down server")
		return srv.Shutdown(ctx)
	})

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs)

	eg.Go(func() error {
		for {
			select {
			case <-ctx.Done():
				return ctx.Err()
			case <-sigs:
				cancel()
			}
		}
	})

	if err := eg.Wait(); err != nil {
		log.Fatal(err)
	}
	fmt.Println("All clear")
}

func serverApp(server *http.Server) error {
	http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
		fmt.Fprintf(writer, "Hello, World")
	})
	fmt.Println("App server start, Listen: 8040")
	return server.ListenAndServe()

}
<file_sep>/go.mod
module github.com/djghostghost/go-lesson-third-week

go 1.16

require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
