Skip to content

Commit

Permalink
custom data endpoint that uses fastfaker
Browse files Browse the repository at this point in the history
  • Loading branch information
bgadrian committed Nov 15, 2018
1 parent ea531dc commit c727d62
Show file tree
Hide file tree
Showing 28 changed files with 1,593 additions and 225 deletions.
115 changes: 115 additions & 0 deletions CUSTOM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
### Custom data
##### template variables and examples

All the variables that can be used with the **/custom/** entrypoint.
For more info see [Readme](./README.md).

| Variable | Example |
|---------|--------|
| **~beeralcohol~** | **4.7%** |
| **~beerblg~** | **9.3°Blg** |
| **~beerhop~** | **Glacier** |
| **~beeribu~** | **42 IBU** |
| **~beermalt~** | **Victory** |
| **~beername~** | **Trappistes Rochefort 10** |
| **~beerstyle~** | **Strong Ale** |
| **~beeryeast~** | **2035 - American Lager** |
| **~bs~** | **plug-and-play** |
| **~buzzword~** | **content-based** |
| **~carmaker~** | **BMW** |
| **~carmodel~** | **Cts** |
| **~chromeuseragent~** | **Mozilla/5.0 (Windows NT 4.0) AppleWebKit/5312 (KHTML, like Gecko) Chrome/39.0.874.0 Mobile Safari/5312** |
| **~city~** | **West Swaniawski** |
| **~color~** | **AliceBlue** |
| **~company~** | **Padberg, Lehner and Lang** |
| **~companysuffix~** | **Inc** |
| **~country~** | **Cyprus** |
| **~countryabr~** | **GL** |
| **~creditcardcvv~** | **778** |
| **~creditcardexp~** | **08/26** |
| **~creditcardnumber~** | **6527420300252086** |
| **~creditcardnumberluhn~** | **4605543610742645** |
| **~creditcardtype~** | **American Express** |
| **~currencylong~** | **Trinidad and Tobago Dollar** |
| **~currencyshort~** | **SLL** |
| **~day~** | **11** |
| **~digit~** | **0** |
| **~domainname~** | **humanvisionary.name** |
| **~domainsuffix~** | **biz** |
| **~email~** | **[email protected]** |
| **~extension~** | **doc** |
| **~firefoxuseragent~** | **Mozilla/5.0 (Windows NT 5.01; en-US; rv:1.9.1.20) Gecko/1983-24-08 Firefox/35.0** |
| **~firstname~** | **Monte** |
| **~float32~** | **3.1917442e+38** |
| **~float64~** | **1.240504735476119e+308** |
| **~fueltype~** | **Methanol** |
| **~gender~** | **female** |
| **~hackerabbreviation~** | **PCI** |
| **~hackeradjective~** | **redundant** |
| **~hackeringverb~** | **copying** |
| **~hackernoun~** | **feed** |
| **~hackerphrase~** | **You can't calculate the program without calculating the optical TCP firewall!** |
| **~hackerverb~** | **copy** |
| **~hexcolor~** | **#n4u8jo** |
| **~hipsterword~** | **lomo** |
| **~hour~** | **19** |
| **~httpmethod~** | **PATCH** |
| **~int16~** | **-4680** |
| **~int32~** | **-814555329** |
| **~int64~** | **-899361807533148519** |
| **~int8~** | **16** |
| **~ipv4address~** | **49.240.114.228** |
| **~ipv6address~** | **2001:cafe:2d10:48c:bec1:2adb:ea5f:e318** |
| **~jobdescriptor~** | **Human** |
| **~joblevel~** | **Implementation** |
| **~jobtitle~** | **Strategist** |
| **~lastname~** | **Erdman** |
| **~latitude~** | **12.386576760734286** |
| **~letter~** | **t** |
| **~longitude~** | **-173.07884672238632** |
| **~mimetype~** | **video/x-scm** |
| **~minute~** | **25** |
| **~month~** | **June** |
| **~name~** | **Dewitt Orn** |
| **~nameprefix~** | **Dr.** |
| **~namesuffix~** | **II** |
| **~nanosecond~** | **442237102** |
| **~operauseragent~** | **Opera/8.95 (Macintosh; PPC Mac OS X 10_6_0; en-US) Presto/2.12.282 Version/12.00** |
| **~phone~** | **8767066027** |
| **~phoneformatted~** | **705-764-6140** |
| **~safariuseragent~** | **Mozilla/5.0 (iPad; CPU OS 9_3_2 like Mac OS X; en-US) AppleWebKit/533.28.8 (KHTML, like Gecko) Version/5.0.5 Mobile/8B119 Safari/6533.28.8** |
| **~safecolor~** | **teal** |
| **~second~** | **9** |
| **~simplestatuscode~** | **200** |
| **~ssn~** | **859461448** |
| **~state~** | **Massachusetts** |
| **~stateabr~** | **IL** |
| **~statuscode~** | **403** |
| **~street~** | **1848 Estatetown** |
| **~streetname~** | **Ports** |
| **~streetnumber~** | **130** |
| **~streetprefix~** | **Port** |
| **~streetsuffix~** | **town** |
| **~timezone~** | **Pacific Standard Time** |
| **~timezoneabv~** | **MST** |
| **~timezonefull~** | **(UTC-11:00) Coordinated Universal Time-11** |
| **~timezoneoffset~** | **3** |
| **~transmissiongeartype~** | **Manual** |
| **~uint16~** | **58530** |
| **~uint32~** | **138464171** |
| **~uint64~** | **4273607561850633676** |
| **~uint8~** | **90** |
| **~url~** | **http://www.principalintegrated.com/strategic** |
| **~useragent~** | **Mozilla/5.0 (X11; Linux i686) AppleWebKit/5331 (KHTML, like Gecko) Chrome/36.0.863.0 Mobile Safari/5331** |
| **~username~** | **Littel5445** |
| **~uuid~** | **7789ae1a-9d4c-41ed-800c-6695cc1541c1** |
| **~vehicletype~** | **Passenger car medium** |
| **~weekday~** | **Friday** |
| **~word~** | **perferendis** |
| **~year~** | **1919** |
| **~zip~** | **13340** |



> This file is generated automatically at build.
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
FROM golang:1.11.1 AS builder
WORKDIR /src

#avoid downloading the dependencies on succesive builds
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN make build

#TODO make this work (not found error if I activate multi-stage build)
#FROM scratch
#COPY --from=builder /src/build/pseudoservice .
FROM scratch
COPY --from=builder /src/build/pseudoservice .
ENV PORT=8080
EXPOSE 8080
ENTRYPOINT ["./build/pseudoservice", "--read-timeout=1s", "--write-timeout=1s", "--keep-alive=15s", "--listen-limit=1024", "--max-header-size=3KiB", "--host=0.0.0.0"]
ENTRYPOINT ["./pseudoservice", "--read-timeout=1s", "--write-timeout=1s", "--keep-alive=15s", "--listen-limit=1024", "--max-header-size=3KiB", "--host=0.0.0.0"]
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ pre:
env GO111MODULE=on go get -d ./
env GO111MODULE=on go test -race ./...

generate:
go run cmd/custom-key-generator/main.go go > handlers/custom_keys.go
go run cmd/custom-key-generator/main.go mark > CUSTOM.md

run: pre
env PORT=8080 go run $(source)

build: pre
rm -f ./build/pseudoservice
env GO111MODULE=on go build -o ./build/pseudoservice $(source)
env GO111MODULE=on CGO_ENABLED=0 go build -o ./build/pseudoservice $(source)
@echo "See ./build/pseudoservice --help"

buildall: pre
Expand Down
72 changes: 50 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The need to generate random (user or other type) data, to (stress) test your own
#### The solution
I've built this project to be used as an internal service, to generate large amounts of fake data.

It was originally built for this project: [davidescus/10minFor300Gb](https://github.com/davidescus/10minFor300Gb) as a wrapper on [brianvoe/gofakeit](https://github.com/brianvoe/gofakeit).
It was originally built for this project: [davidescus/10minFor300Gb](https://github.com/davidescus/10minFor300Gb) as a wrapper on [bgadrian/fastfaker](https://github.com/bgadrian/gofakeit).

### Performance

Expand All @@ -24,16 +24,57 @@ For the best performance:

You run it as a simple HTTP server, in a private network, and clients can make requests to get random data.

All the endpoints have 2 modes:
* deterministic - for each `?seed=42` received as input, the same data will be generated.
* random - if a `seed` is not given, random data will be generated. This is the most optimal method (performance).
Global optional parameters:
* `?seed=42` - if given, the result will be deterministic, as in for all calls the same data will be returned.
* `token=SECRET42` - the APIKEY
* `{count}` - how many results should be generated, integer [1,500]

### Endpoints (data types)

###### /users/{count}
`count` = how many users should generate, an integer between 1 and 500.
##### /docs
Contains the OpenID/swagger documentation for this API.

##### /health
Returns `200` if the service is available.

##### /custom/{count}?template="Hello ~name~!"
Generate random data based on a given template. Supports any string, including JSON schema.

The template can contains variables like `~name~` or `~email~`.

```bash
#the template is URL encoded: Hello ~name~!
curl -X GET "http://localhost:8080/custom/3?token=SECRET42&seed=42&template=Hello%20~name~%21"
{"results":[
"Hello Jeromy Schmeler!",
"Hello Kim Steuber!",
"Hello Jacky Borer!"
],"seed":42}

#template: {name:"~name~",age:~digit~~digit~}
curl -X GET "http://localhost:8080/custom/3?token=SECRET42&seed=42&template=%22%7Bname%3A%22~name~%22%2Cage%3A~digit~%7D%22"
{"results":[
"\"{name:\"Jeromy Schmeler\",age:53}\"",
"\"{name:\"Dustin Jones\",age:62}\"",
"\"{name:\"Keely Hartmann\",age:12}\""
],"seed":42}

#template: ~country~
curl -X GET "http://localhost:8080/custom/6?token=SECRET42&seed=42&template=~country~"
{"results":[
"Tajikistan",
"Cameroon",
"Cote Divoire",
"Turkmenistan",
"Ethiopia",
"Afghanistan"
],"seed":42}

```
For all supported template variables see [CUSTOM.md](./CUSTOM.md)

##### /users/{count}
Generate random users based on a preconfigured set of properties.
```bash
#fast and random user
curl "http://localhost:8080/api/v1/users/1?token=SECRET42"
Expand Down Expand Up @@ -93,24 +134,11 @@ The binary has the following env variables:
* `APIKEY` - secret string to be served at `?token=SECRET42`
* for more see `pseudoservice --help`

All the endpoint have the following query params:
* `token` - required, the APIKEY
* `seed` - optional, for deterministic responses


The server accepts `gzip`.

The server was generated using swagger (open API), to see a full documentation of the service access the [127.0.0.1:8080/docs](http://127.0.0.1:8080/docs) endpoint.
The server accepts `gzip` and it was generated using swagger (open API), to see a full documentation of the service access the [127.0.0.1:8080/docs](http://127.0.0.1:8080/docs) endpoint.

### TODO
* make the docker multi-stage build work (lower the container from 700mb to 7mb)
* add a general/custom endpoint, where the payload is an object with the types of data it requires to be generated (each client decide what objects to be generated)
All the [issues](https://github.com/bgadrian/pseudoservice/issues)

### Copyright
Bledea Georgescu Adrian

Free to use but not for commercial purposes.



Bledea Georgescu Adrian https://coder.today

2 changes: 1 addition & 1 deletion call_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

docker stop pseudoservice
docker rm pseudoservice
docker rmi bgadrian/pseudoservice
#docker rmi bgadrian/pseudoservice

docker build -t bgadrian/pseudoservice:latest .
docker run -d -p 8080:8080 --name pseudoservice bgadrian/pseudoservice:latest
4 changes: 3 additions & 1 deletion call_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
curl --compressed -sH 'Accept-encoding: gzip' -X GET "http://localhost:8080/users/3?token=SECRET42&seed=66"

#random
curl --compressed -sH 'Accept-encoding: gzip' -X GET "http://localhost:8080/users/3?token=SECRET42"
curl -X GET "http://localhost:8080/users/3?token=SECRET42"

curl -X GET "http://localhost:8080/custom/3?token=SECRET42&seed=42&template=~country~"
Loading

0 comments on commit c727d62

Please sign in to comment.