github.com/blong14/gache@v0.0.0-20240124023949-89416fd8bbfa/internal/io/http/srv.go (about) 1 package http 2 3 import ( 4 "context" 5 "log" 6 "net/http" 7 "time" 8 ) 9 10 type Handler map[string]http.HandlerFunc 11 12 func Start(srv *http.Server, routes Handler) { 13 for pattern, handler := range routes { 14 http.HandleFunc(pattern, handler) 15 } 16 log.Printf("listening on %s\n", srv.Addr) 17 if err := srv.ListenAndServe(); err != http.ErrServerClosed { 18 log.Println(err) 19 } 20 } 21 22 func Stop(ctx context.Context, srvs ...*http.Server) { 23 ctx, cancel := context.WithTimeout(ctx, 3*time.Second) 24 defer cancel() 25 for _, srv := range srvs { 26 if err := srv.Shutdown(ctx); err != nil { 27 log.Printf("HTTP server Shutdown: %v", err) 28 } 29 } 30 }