Outerspace is a demo app that uses the SpaceX API to fetch data about SpaceX launches, rockets, and capsules. It also talks to a numbers API that consistently generates random results.
This will run a server on port :8080
with the API endpoints.
go run main.go
Once the app is running you can make API requests.
The simplest way to make a bunch of requests is to run the build in client script.
cd cmd/client
go run .
But you can also make requests directly.
curl localhost:8080/api/latest-launch
Request with no path to see the full list of API endpoints:
curl localhost:8080/ | jq
{
"/": "Shows this list of available endpoints",
"/api/latest-launch": "Get the latest SpaceX launch",
"/api/numbers": "Get a random math fact",
"/api/rocket": "Get a specific rocket by ID (use ?id=[rocket_id])",
"/api/rockets": "Get a list of all SpaceX rockets"
}
There are unit tests all through the code that you can easily run:
go test -v ./...
For apps that make lots of API calls, proxymock can be used to record, mock, and replay those downstream calls.
All you have to do is set the following environment variables before you run the go code:
export http_proxy=http://localhost:4140
export https_proxy=http://localhost:4140
export grpc_proxy=http://$(hostname):4140
go run main.go
Then in a new terminal window run proxymock record
to start recording:
proxymock record
Now, in yet another new terminal window, run the script to make requests to the server:
go run cmd/client/main.go
Note that this will only record requests made from the API server to the external APIs. To record the requests from the client you will need to export the same proxy environment variables in that terminal.
For examples of the kind of data collected, check the proxymock
directory of the repository. If you want to see what the data looks like, you can use the inspect command, it will automatically show you all the data in the proxymock
directory.
proxymock inspect
You can see a list of every API call that was recorded, both inbound to your application and outbound to downstream systems.
Using your arrow keys you can navigate to a particular call and see all the details of what request was sent and how the application responded. Here we can see the exact value returned from the Numbers API.
If you want to run a mock server with all of those recorded responses, you can easily launch it with:
proxymock mock
Now if another terminal window you can run the outerspace-go
application:
export http_proxy=http://localhost:4140
export https_proxy=http://localhost:4140
export grpc_proxy=http://$(hostname):4140
go run main.go
In a third terminal window you can either use curl
or just run through all the API client requests with:
go run cmd/client/main.go
You can see all the calls that were mocked out by running this (put in the correct TIMESTAMP from your machine):
proxymock inspect --in proxymock/mocked-TIMESTAMP
Feel free to join the Speedscale Community to learn more ways to use proxymock
on your next project!