Tradebot is a tiny web api made with Go that takes array of stock prices and returns the best profit one could have made from 1 purchase and 1 sale using best practices of running microservices in Kubernetes.
- Separation of concerns with middlewares, transport layers and handlers
- Multiple transport support ( json/http + ws/http + grpc)
- Health checks (readiness and liveness)
- Graceful shutdown on interrupt signals
- File watcher for secrets and configmaps
- Instrumented with Prometheus
- Tracing with Istio and Jaeger
- Linkerd service profile
- Structured logging with zap
- 12-factor app with viper
- Fault injection (random errors and latency) for chaos engineering
- Helm and Kustomize installers
- End-to-End testing with Kubernetes Kind and Helm
- Kustomize testing with GitHub Actions and Open Policy Agent
- Expose Kubernetes services over HTTPS with Ngrok
- Managing Helm releases the GitOps way
- Swagger docs
- Web Socket implementation (Since this is a trading API, I thought web socket implementation should be made available. So added a sample implmentation)
GET /trade/profit
calculates max profit for given pricesGET /version
prints tradebot version and git commit hashGET /metrics
return HTTP requests duration and Go runtime metricsGET /healthz
used by Kubernetes liveness probeGET /readyz
used by Kubernetes readiness probePOST /readyz/enable
signals the Kubernetes LB that this instance is ready to receive trafficPOST /readyz/disable
signals the Kubernetes LB to stop sending requests to this instanceGET /configs
returns a JSON with configmaps and/or secrets mounted in theconfig
volumeGET /ws/echo
echos content via websocketscli ws ws://localhost:9898/ws/echo
GET /swagger.json
returns the API Swagger docs, used for Linkerd service profiling and Gloo routes discovery
gRPC API:
/grpc.health.v1.Health/Check
health checking
Docker:
docker run -dp 9898:9898 praveensastry/tradebot
curl -X POST \
http://localhost:9898/trade/profit \
-H 'Cache-Control: no-cache' \
-H 'Postman-Token: 01d1b1da-64cf-406f-b7f7-ce08859382f1' \
-d '{
"prices": [10, 7, 5, 8, 11, 9]
}'
Local:
# Clone the source
# In the project root directory run the below command
go run cmd/tradebot/main.go
curl -X POST \
http://localhost:9898/trade/profit \
-H 'Cache-Control: no-cache' \
-H 'Postman-Token: 01d1b1da-64cf-406f-b7f7-ce08859382f1' \
-d '{
"prices": [10, 7, 5, 8, 11, 9]
}'
P.S. While testing you might have approve access if you see the below dialog
Helm:
helm repo add tradebot https://praveensastry.github.io/tradebot
helm upgrade --install --wait frontend \
--namespace test \
--set replicaCount=2 \
--set backend=http://backend-tradebot:9898/echo \
tradebot/tradebot
helm test frontend --cleanup
helm upgrade --install --wait backend \
--namespace test \
--set hpa.enabled=true \
tradebot/tradebot
Kustomize:
kubectl apply -k github.com/praveensastry/tradebot//kustomize
- As of now, the API only calculates profit for 1 transcation (1 buy and 1 sell). Ideally the API should accept the no of transactions as request parameter and calculate profit accordingly.
- Improve API folder structure by moving handlers, middlewares and transport layers to separate packages / folders
- Use a framework like go-kit to get most API features OOB