cerrado @ ce911df583e384d86018e42f9548cdf33d1c1549

 1package worker
 2
 3import (
 4	"context"
 5	"net/http"
 6)
 7
 8type ServerTask struct {
 9	server *http.Server
10}
11
12func NewServerTask(server *http.Server) *ServerTask {
13	return &ServerTask{
14		server: server,
15	}
16}
17
18func (self *ServerTask) Start(ctx context.Context) error {
19	done := make(chan error)
20
21	go func() {
22		done <- self.server.ListenAndServe()
23	}()
24
25	select {
26	// if ListenAndServe error for something other than context.Canceled
27	//(e.g.: address already in use) it trigger done to return sonner with
28	// the return error
29	case err := <-done:
30		return err
31
32	// in case of context canceled it will manually trigger the server to
33	// shutdown, and return its error, which is most cases, but not limited, is
34	// context.Canceled.
35	case <-ctx.Done():
36		return self.server.Shutdown(ctx)
37	}
38}