From 6cffad95dbe107f81de772d651478cfa20fbbbcb Mon Sep 17 00:00:00 2001 From: Daniele Martinoli Date: Tue, 27 Sep 2022 17:38:38 +0200 Subject: [PATCH] Changed to POST method --- README.md | 6 +++--- pkg/exporter/exporter_rest.go | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cec7dbc..c7ec82a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Go application to export the configuration of applications deployed in OpenShift * Filter namespaces by configurable label(s) * For each application (e.g., any `Deplopyment`, `DeploymentConfig` and `StatefulSet` in the matching namespaces), collect the image name and version and the resource configuration and usage (optional) * Export configuration in configurable format (text or CSV) -* Run as a script or a REST service +* Run as a script or a REST service (`POST` to `/inventory` endpoint) * Run as a standalone executable or in OpenShift containerized environment (REST service only) Sample output in CSV format without the resource configuration and usage data: @@ -83,8 +83,8 @@ go run main.go --help The following are examples of requests performed using `curl`: ```bash -curl "http://localhost:8080/inventory" -curl "http://localhost:8080/inventory?content-type=CSV&ns-selector=mylabel=myvalue" +curl -X POST "http://localhost:8080/inventory" +curl -X POST "http://localhost:8080/inventory?content-type=CSV&ns-selector=mylabel=myvalue" ``` ### Running the binary executable diff --git a/pkg/exporter/exporter_rest.go b/pkg/exporter/exporter_rest.go index 6876d76..b4f33e3 100644 --- a/pkg/exporter/exporter_rest.go +++ b/pkg/exporter/exporter_rest.go @@ -29,7 +29,11 @@ func (s *ExporterService) Run() { router.Path("/inventory").Queries("content-type", "{content-type}").Queries("ns-selector", "{ns-selector}").Queries("output", "{output}").Queries("with-resources", "{with-resources}").HandlerFunc(s.inventoryHandler).Name("inventoryHandler") router.Path("/inventory").HandlerFunc(s.inventoryHandler).Name("inventoryHandler") - url := fmt.Sprintf("0.0.0.0:%d", s.config.ServerPort()) + host := "localhost" + if s.config.RunInContainer() { + host = "0.0.0.0" + } + url := fmt.Sprintf("%s:%d", host, s.config.ServerPort()) logger.Infof("Starting listener as %s", url) if err := http.ListenAndServe(url, router); err != nil { logger.Fatal(err) @@ -57,10 +61,10 @@ func (s *ExporterService) inventoryHandler(rw http.ResponseWriter, req *http.Req } if req.URL.Path == "/inventory" { - if req.Method == "GET" { + if req.Method == "POST" { s.inventory(&newConfig, rw) } else { - http.Error(rw, fmt.Sprintf("Expect method GET at /, got %v", req.Method), http.StatusMethodNotAllowed) + http.Error(rw, fmt.Sprintf("Expect method POST at /, got %v", req.Method), http.StatusMethodNotAllowed) } } else { http.Error(rw, fmt.Sprintf("Unmanaged path %s", req.URL.Path), http.StatusNotFound)