From f0cba3c2c6b22eecb445b8ace2e1eef61b52620f Mon Sep 17 00:00:00 2001 From: Kamin Fay Date: Tue, 28 May 2024 15:28:20 -0400 Subject: [PATCH] Adding the timeout flag for fileserver command --- cmd/hauler/cli/store/serve.go | 7 +++++-- internal/server/file.go | 15 ++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/hauler/cli/store/serve.go b/cmd/hauler/cli/store/serve.go index fcf7d49..140669d 100644 --- a/cmd/hauler/cli/store/serve.go +++ b/cmd/hauler/cli/store/serve.go @@ -80,6 +80,7 @@ type ServeFilesOpts struct { *RootOpts Port int + Timeout int RootDir string storedir string @@ -89,6 +90,7 @@ func (o *ServeFilesOpts) AddFlags(cmd *cobra.Command) { f := cmd.Flags() f.IntVarP(&o.Port, "port", "p", 8080, "Port to listen on.") + f.IntVarP(&o.Timeout, "timeout", "t", 60, "Set the http request timeout duration in seconds for both reads and write.") f.StringVar(&o.RootDir, "directory", "fileserver", "Directory to use for backend. Defaults to $PWD/fileserver") } @@ -102,8 +104,9 @@ func ServeFilesCmd(ctx context.Context, o *ServeFilesOpts, s *store.Layout) erro } cfg := server.FileConfig{ - Root: o.RootDir, - Port: o.Port, + Root: o.RootDir, + Port: o.Port, + Timeout: o.Timeout, } f, err := server.NewFile(ctx, cfg) diff --git a/internal/server/file.go b/internal/server/file.go index 55a6515..136e994 100644 --- a/internal/server/file.go +++ b/internal/server/file.go @@ -12,9 +12,10 @@ import ( ) type FileConfig struct { - Root string - Host string - Port int + Root string + Host string + Port int + Timeout int } // NewFile returns a fileserver @@ -30,11 +31,15 @@ func NewFile(ctx context.Context, cfg FileConfig) (Server, error) { cfg.Port = 8080 } + if cfg.Timeout == 0 { + cfg.Timeout = 60 + } + srv := &http.Server{ Handler: r, Addr: fmt.Sprintf(":%d", cfg.Port), - WriteTimeout: 15 * time.Second, - ReadTimeout: 15 * time.Second, + WriteTimeout: time.Duration(cfg.Timeout) * time.Second, + ReadTimeout: time.Duration(cfg.Timeout) * time.Second, } return srv, nil