Skip to content

Commit

Permalink
Merge pull request #1 from universal-development/feature/simulation-c…
Browse files Browse the repository at this point in the history
…ases

Added env variables to simulate different response codes
  • Loading branch information
denis256 authored Feb 21, 2022
2 parents de9f74d + 27be10b commit c94d6bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Test application to verify environment deployment and reachability over HTTP.
Environment variables:
* `LISTEN_ADDRESS` - listen address, `0.0.0.0` by default
* `LISTEN_PORT` - listen port for requests, `8080` by default
* `APP_NAME` - name of application printed in output
* `HTTP_RESPONSE_CODE` - returned HTTP response code, 200 by default
* `PROCESSING_SLEEP` - block processing of request for specified ms, 0 by default

## Build

Expand Down
21 changes: 20 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/unidev-platform/golang-core/xenv"
)
Expand All @@ -14,13 +16,30 @@ func main() {

// simple handler that prints hostname, requested path and env variables
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

sleep := xenv.Env("PROCESSING_SLEEP", "0")
isleep, err := strconv.Atoi(sleep)
if err != nil {
log.Printf("%v", err)
return
}
time.Sleep(time.Duration(isleep) * time.Millisecond)

log.Println("Handling request")
code := xenv.Env("HTTP_RESPONSE_CODE", "200")
icode, err := strconv.Atoi(code)
if err != nil {
log.Printf("%v", err)
return
}
w.WriteHeader(icode)
hostname, err := os.Hostname()
if err != nil {
log.Printf("%v", err)
return
}
fmt.Fprintf(w, "%s path: %v \n ", hostname, r.URL.Path)
name := xenv.Env("APP_NAME", "TestApp")
fmt.Fprintf(w, "%s %s path: %v \n ", name, hostname, r.URL.Path)
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
fmt.Fprintf(w, "%v = %v \n", pair[0], pair[1])
Expand Down

0 comments on commit c94d6bf

Please sign in to comment.