Sample backend service which returns request details to the caller.
Open API Specification: request_info_openapi.yaml https://app.swaggerhub.com/apis-docs/renuka-fernando/request-info/2.0.0
docker run --rm -p 8080:8080 -e "NAME=Service A" renukafernando/request-info:latest -addr :8080 -pretty -logH -logB -statusCode 200 -delayMs 1000- Set
NAMEenvironment variable to set the name of the service, which will out as a response - Set
-read-envsargument if it is required to out environment variables set in container - set
-prettyargument if it is required to out prettified JSON - set
-delayMsargument to delay response for a given time in milliseconds - set
-statusCodeargument to set status code of the response - set
-logHargument to log request headers - set
-logBargument to log request body - set
-addrargument to set the address to listen to - set
-httpsargument to enable HTTPS - set
-keyargument to set the path to the key file - set
-certargument to set the path to the cert file - set
-mtlsargument to enable mTLS - set
-caargument to set the path to the CA file - set
-setResponseBodyargument to set the response body via the flag-responseBody - set
-responseBodyargument to set the response body - set
-disable-access-logsargument to disable access logs - set
-wait-before-graceful-shutdown-msargument to set the time to wait before graceful shutdown in milliseconds
Resource Usage
resources:
limits:
cpu: "5m"
memory: "10Mi"
requests:
cpu: "2m"
memory: "5Mi"Override statusCode and responseTime with the following query parameters.
statusCode- HTTP status code to responddelayMs- Time to wait (ms) before responding to request
Get request info:
curl 'http://localhost:8080/hello/world?delayMs=2000-5000&statusCode=201&pretty=true' -iEmpty response:
curl 'http://localhost:8080/empty?delayMs=1000&statusCode=500' -iEcho response:
curl 'http://localhost:8080/echo?statusCode=403' -d 'hello world!' -iRead file from local directory:
curl 'http://localhost:8080/file/read?path=README.md' -i
curl 'http://localhost:8080/file/read?path=main.go&statusCode=200&delayMs=1000' -iList files in directory:
curl 'http://localhost:8080/file/list' -i
curl 'http://localhost:8080/file/list?path=.&statusCode=200&delayMs=500' -iExecute shell command:
curl 'http://localhost:8080/command' -X POST -d 'ls -alh .' -i
curl 'http://localhost:8080/command' -X POST -d 'pwd' -i
curl 'http://localhost:8080/command' -X POST -d 'echo "Hello World"' -i
curl 'http://localhost:8080/command?statusCode=200&delayMs=1000' -X POST -d 'date' -iSet response data:
curl localhost:8080/req-info/response -d 'hello world' -i
curl localhost:8080/foo -i
curl localhost:8080/req-info/response -X DELETERunning backend service.
docker run -d -p 8080:8080 -e "NAME=Service A" --name service-A renukafernando/request-info:latestSending request to backend service.
curl http://localhost:8080/hello/world{"Name":"Service A","Request":{"Method":"GET","Header":{"Accept":["*/*"],"User-Agent":["curl/7.54.0"]},"URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/hello/world","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":""},"ContentLength":0,"TransferEncoding":null,"Host":"localhost:8080","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"172.17.0.1:55922","RequestURI":"/hello/world","TLS":null}}Remove running service
docker rm -f service-ARunning backend service.
docker run -d -p 8080:8080 -e "NAME=Service A" --name service-A renukafernando/request-info:latest -read-envsSending request to backend service.
curl http://localhost:8080/hello/world{"Name":"Service A","Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NAME=Service A","HOME=/root"],"Request":{"Method":"GET","Header":{"Accept":["*/*"],"User-Agent":["curl/7.54.0"]},"URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/hello/world","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":""},"ContentLength":0,"TransferEncoding":null,"Host":"localhost:8080","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"172.17.0.1:35306","RequestURI":"/hello/world","TLS":null}}Remove running service
docker rm -f service-ARunning backend service.
docker run -d -p 8080:8080 -e "NAME=Service A" --name service-A renukafernando/request-info:latest -prettySending request to backend service.
curl http://localhost:8080/hello/world{
"Name": "Service A",
"Request": {
"Method": "GET",
"Header": {
"Accept": [
"*/*"
],
"User-Agent": [
"curl/7.54.0"
]
},
"URL": {
"Scheme": "",
"Opaque": "",
"User": null,
"Host": "",
"Path": "/hello/world",
"RawPath": "",
"ForceQuery": false,
"RawQuery": "",
"Fragment": "",
"RawFragment": ""
},
"ContentLength": 0,
"TransferEncoding": null,
"Host": "localhost:8080",
"Form": null,
"PostForm": null,
"MultipartForm": null,
"Trailer": null,
"RemoteAddr": "172.17.0.1:44222",
"RequestURI": "/hello/world",
"TLS": null
}
}Remove running service
docker rm -f service-AGenerate Certs
./gen-certs.shRunning backend service.
docker run --rm -p 8443:8443 -e "NAME=Service A" -v ./certs:/certs renukafernando/request-info:latest -pretty -logH -logB -addr :8443 -https -key /certs/server.key -cert /certs/server.crtTest the service
curl https://localhost:8443/foo \
--cacert certs/server.crt \
-vgo run main.go -pretty -logH -logB -addr :8443 -https -key ./certs/server.key -cert ./certs/server.crtTest the service
curl https://localhost:8443/foo \
--cacert certs/server.crt \
-vGenerate Certs
./gen-certs.shRunning backend service.
docker run --rm -p 8443:8443 -e "NAME=Service A" -v ./certs:/certs renukafernando/request-info:latest -pretty -logH -logB -addr :8443 -https -key /certs/server.key -cert /certs/server.crt -mtls -ca /certs/client.crtTest the service
curl https://localhost:8443/foo \
--cacert certs/server.crt \
--key certs/client.key \
--cert certs/client.crt \
-vgo run main.go -pretty -logH -logB -addr :8443 -https -key ./certs/server.key -cert ./certs/server.crt -mtls -ca ./certs/client.crtTest the service
curl https://localhost:8443/foo \
--cacert certs/server.crt \
--key certs/client.key \
--cert certs/client.crt \
-vExecute the following command to build the Docker image.
./build-docker.sh- Create a Service component in Choreo.
- Give name and description to the service.
- Select this as the GitHub account. Select the repository as
request-infoand branch asmain. - Set build preset as
Dockerfile. - Set Dockerfile path as
DockerfileFullBuild. - Set Docker context as
/.