From d04ca1b8512de2362d1c722e9690047915349fb9 Mon Sep 17 00:00:00 2001 From: nicumicle <20170987+nicumicleI@users.noreply.github.com> Date: Sat, 13 Dec 2025 09:11:13 +0100 Subject: [PATCH 1/2] feat: Add http server to cli --- README.md | 13 ++++++++++++- cmd/cli/main.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dbfc3f2..c7cc2ca 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,16 @@ docker run --rm -p 8080:8080 -it nicumicle/parallelhttp Open in browser: 👉 [http://localhost:8080](http://localhost:8080) -### ▶️ Option B — Run the UI from Go +### ▶️ Option B — Run the UI from cli + +```shell +./parallelhttp --serve --port=8080 +``` + +Open in browser: +👉 [http://localhost:8080](http://localhost:8080) + +### ▶️ Option C — Run the UI from Go ```shell go run cmd/service/main.go @@ -117,6 +126,8 @@ CLI Flags -method string GET POST PUT PATCH DELETE (default: GET) -parallel int Number of parallel requests (default: 1) -timeout duration Request timeout (default: 10s) + -serve bool Starts the HTTP server + -port int Port for the HTTP Server(default: 8080) ``` Example diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 5989a88..3717e7b 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -6,11 +6,16 @@ import ( "flag" "fmt" "log" + "net" + "net/http" + "os" + "os/signal" "strings" "time" "gopkg.in/yaml.v2" + "github.com/nicumicle/parallel/internal/api" "github.com/nicumicle/parallel/internal/parallelhttp" ) @@ -21,8 +26,10 @@ func main() { method := flag.String("method", "GET", "Request Method. Default GET.") endpoint := flag.String("endpoint", "", "Request endpoint to be called.") parallel := flag.Int("parallel", 1, "Number of parallel calls. Default 1.") - duration := flag.Duration("duration", 0, "Max duration for all calls. Example: 0->no limit, 1ms, 1s, 10m") + duration := flag.Duration("duration", 0, "Max duration for all calls. Example: 0->no limit, 1ms, 1s, 10m. Default 0.") timeout := flag.Duration("timeout", 0*time.Second, "Request timeout. Default 10s") + serve := flag.Bool("serve", false, "Starts the HTTP server.") + port := flag.Int("port", 8080, "HTTP server port. Default 8080.") flag.StringVar(&format, "format", "json", "Response format. One of: text, yaml, json. Default json.") flag.Parse() @@ -32,6 +39,16 @@ func main() { Parallel: *parallel, Duration: *duration, } + + // HTTP Server + if serve != nil && *serve { + if err := runHTTP(*port); err != nil { + log.Fatalf("Error: %s", err.Error()) + } + + return + } + p := parallelhttp.New(*timeout) r, err := p.Run(context.Background(), input) @@ -86,3 +103,31 @@ func main() { fmt.Println(string(result)) } } + +func runHTTP(port int) error { + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() + + srv := &http.Server{ + Addr: fmt.Sprintf(":%d", port), + BaseContext: func(l net.Listener) context.Context { return ctx }, + ReadTimeout: time.Second * 30, + WriteTimeout: time.Second * 30, + Handler: api.NewAPI(), + } + + srvErr := make(chan error, 1) + go func() { + log.Printf("Server started at: %d\n", port) + srvErr <- srv.ListenAndServe() + }() + + select { + case err := <-srvErr: + return err + case <-ctx.Done(): + stop() + } + + return srv.Shutdown(context.Background()) +} From 74b9a8a74ab025062b08c873fa6227b135b02e4e Mon Sep 17 00:00:00 2001 From: nicumicle <20170987+nicumicleI@users.noreply.github.com> Date: Sat, 13 Dec 2025 09:13:45 +0100 Subject: [PATCH 2/2] fix: add parent context --- cmd/cli/main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 3717e7b..46351e5 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -20,6 +20,7 @@ import ( ) func main() { + ctx := context.Background() var format string // Init flags @@ -42,7 +43,7 @@ func main() { // HTTP Server if serve != nil && *serve { - if err := runHTTP(*port); err != nil { + if err := runHTTP(ctx, *port); err != nil { log.Fatalf("Error: %s", err.Error()) } @@ -51,7 +52,7 @@ func main() { p := parallelhttp.New(*timeout) - r, err := p.Run(context.Background(), input) + r, err := p.Run(ctx, input) if err != nil { log.Fatalf("[ERROR]: %s\n", err.Error()) } @@ -104,8 +105,8 @@ func main() { } } -func runHTTP(port int) error { - ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) +func runHTTP(ctx context.Context, port int) error { + ctx, stop := signal.NotifyContext(ctx, os.Interrupt) defer stop() srv := &http.Server{