Skip to content

Commit

Permalink
add more flags
Browse files Browse the repository at this point in the history
  • Loading branch information
pingzhou committed Nov 25, 2021
1 parent aae5840 commit bdd3efa
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# timeout-checker
A simple wait and response api to test retry& timeout. Yep! Everyone loves retry and timeout test 🤠

Install via brew
## Install via brew
```
$ brew tap applegreengrape/timeout-checker https://github.com/applegreengrape/timeout-checker
$ brew install timeout-checker
```
server side:
```
$ checker-api -wait 5 -port 8081
$ checker-api -wait 3 -port 8081
2021/11/22 15:21:30 wait 3 second
2021/11/22 15:21:30 received
2021/11/22 15:21:33 returned
Expand All @@ -25,3 +25,63 @@ $ curl http://localhost:8081/healthcheck
$ curl http://localhost:8081/healthcheck
{"status":"up"}
```

## Customize api response
server side:
```
$ go run main.go -path '/v1/api/echo' -payload echo.json
2021/11/25 16:25:43 wait 3 second
2021/11/25 16:25:43 received
2021/11/25 16:25:46 returned
```

client side:
```
$ curl -X POST http://localhost:8080/v1/api/echo
{
"_id": "619fb7e74eb6e971b55f410a",
"index": 0,
"guid": "32316e07-db6a-437d-887c-6d0c34ee805a",
"isActive": true,
"balance": "$2,259.93",
"picture": "http://placehold.it/32x32",
"age": 36,
"eyeColor": "blue",
"name": "Jan Crane",
"gender": "female",
"company": "COMVEY",
"email": "[email protected]",
"phone": "+1 (953) 460-3063",
"address": "962 Lorraine Street, Hayden, Texas, 2598",
"about": "Est aute exercitation laborum ipsum ad non sit laborum officia. Exercitation cupidatat consequat ea id aute cillum minim veniam Lorem cupidatat consectetur sit esse excepteur. Ex fugiat tempor dolore Lorem et nulla veniam quis in esse. Non est deserunt consequat aute do pariatur laborum amet labore eu ex culpa sit ipsum. Laboris duis nulla voluptate proident. Fugiat sint anim minim anim cupidatat veniam nulla dolore qui labore aute fugiat anim ipsum.\r\n",
"registered": "2018-10-29T11:51:37 -00:00",
"latitude": 65.197781,
"longitude": 46.290158,
"tags": [
"minim",
"ipsum",
"tempor",
"in",
"sit",
"culpa",
"dolore"
],
"friends": [
{
"id": 0,
"name": "Camille Hopkins"
},
{
"id": 1,
"name": "Alyssa Contreras"
},
{
"id": 2,
"name": "Burton Haney"
}
],
"greeting": "Hello, Jan Crane! You have 6 unread messages.",
"favoriteFruit": "strawberry"
}
```
So you should be able to customiz the api path and response
45 changes: 45 additions & 0 deletions echo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"_id": "619fb7e74eb6e971b55f410a",
"index": 0,
"guid": "32316e07-db6a-437d-887c-6d0c34ee805a",
"isActive": true,
"balance": "$2,259.93",
"picture": "http://placehold.it/32x32",
"age": 36,
"eyeColor": "blue",
"name": "Jan Crane",
"gender": "female",
"company": "COMVEY",
"email": "[email protected]",
"phone": "+1 (953) 460-3063",
"address": "962 Lorraine Street, Hayden, Texas, 2598",
"about": "Est aute exercitation laborum ipsum ad non sit laborum officia. Exercitation cupidatat consequat ea id aute cillum minim veniam Lorem cupidatat consectetur sit esse excepteur. Ex fugiat tempor dolore Lorem et nulla veniam quis in esse. Non est deserunt consequat aute do pariatur laborum amet labore eu ex culpa sit ipsum. Laboris duis nulla voluptate proident. Fugiat sint anim minim anim cupidatat veniam nulla dolore qui labore aute fugiat anim ipsum.\r\n",
"registered": "2018-10-29T11:51:37 -00:00",
"latitude": 65.197781,
"longitude": 46.290158,
"tags": [
"minim",
"ipsum",
"tempor",
"in",
"sit",
"culpa",
"dolore"
],
"friends": [
{
"id": 0,
"name": "Camille Hopkins"
},
{
"id": 1,
"name": "Alyssa Contreras"
},
{
"id": 2,
"name": "Burton Haney"
}
],
"greeting": "Hello, Jan Crane! You have 6 unread messages.",
"favoriteFruit": "strawberry"
}
34 changes: 31 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"flag"
"os"
"time"
"log"

"github.com/gorilla/mux"
)
Expand All @@ -14,25 +16,51 @@ type Wait struct {
time int
}

type Echo struct {
time int
payloadPath string
}

func (wait *Wait) healthcheck(w http.ResponseWriter, r *http.Request) {
log.Printf("wait %d second", wait.time)
log.Println("received")
time.Sleep(time.Duration(wait.time) * time.Second)

w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"up"}`))
log.Println("returned")
}

func (echo *Echo) echo(w http.ResponseWriter, r *http.Request) {
log.Printf("wait %d second", echo.time)
log.Println("received")
time.Sleep(time.Duration(echo.time) * time.Second)

w.WriteHeader(http.StatusOK)
jsonFile, err := os.Open(echo.payloadPath)
if err != nil {}

defer jsonFile.Close()

byteValue, _ := ioutil.ReadAll(jsonFile)
w.Write([]byte(byteValue))
log.Println("returned")
}

func main() {
wait := flag.Int("wait", 3, "")
port := flag.Int("port", 8080, "")
path := flag.String("path", "/api/echo", "")
method := flag.String("method", "POST", "")
payload := flag.String("payload", "", "path to payload json file")

flag.Parse()

w := &Wait{time: *wait}
e := &Echo{time: *wait, payloadPath: *payload}

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/healthcheck", w.healthcheck).Methods("GET")
router.HandleFunc(*path, e.echo).Methods(*method)

http.ListenAndServe(fmt.Sprintf(":%d", *port), router)
}

0 comments on commit bdd3efa

Please sign in to comment.