This project is an API Gateway built in Go that sits in front of decentralized applications (dApps) and manages requests to various blockchain networks, currently supporting Ethereum and Polygon. It includes features like JWT-based authentication, rate limiting, and the ability to fetch the latest blockchain block numbers.
- JWT Authentication: Secures the API with JSON Web Tokens (JWT).
- Rate Limiting: Limits the number of requests a client can make to prevent abuse.
- Blockchain Network Integration: Routes requests to Ethereum and Polygon networks to fetch the latest block numbers.
- Modular Middleware Design: Easily extendable for additional middlewares or blockchain networks.
- Go 1.16+ installed on your machine.
- API keys or RPC URLs for Ethereum (e.g., via Infura) and Polygon RPC endpoints.
- Environment variables configured (details below).
- Clone the repository:
git clone https://github.com/your-username/api-gateway-dapp.git cd api-gateway-dapp
- Install dependencies:
go mod tidy
- Set up the environment variables. Create a .env file in the root of the project:
JWT_SECRET=your_secret_key RATE_LIMIT_PER_MINUTE=60 ETHEREUM_ENDPOINT=https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID POLYGON_ENDPOINT=https://polygon-mainnet.infura.io/v3/your-infura-api-key
- Build and run module:
go build go run .
- Send Requests:
- Use a tool like curl or Postman to send requests to the gateway.
- Add an Authorization header with a valid JWT token for access.
curl -H "Authorization: Bearer <your_jwt_token>" -X POST http://localhost:8080/api/v1/blockchain -d '{"network": "ethereum"}'
- Sample Response:
{ "block_number": "14330000" }
JWT_SECRET
: Secret key for encoding and decoding JWT tokens.RATE_LIMIT_PER_MINUTE
: Number of requests allowed per minute.ETHEREUM_ENDPOINT
: RPC endpoint for Ethereum network.POLYGON_ENDPOINT
: RPC endpoint for Polygon network.
api-gateway-dapp/
├── main.go # Entry point of the application
├── auth_middleware.go # JWT Authentication middleware
├── rate_limit_middleware.go # Rate limiting middleware
├── handlers.go # Request handlers for blockchain interactions
├── auth_middleware_test.go # Unit tests for JWT middleware
├── rate_limit_middleware_test.go # Unit tests for rate limiting middleware
├── handlers_test.go # Unit tests for blockchain handlers
└── .env # Environment variables (not included in version control)
To run the tests for this project, use:
go test -v ./...
- Adding New Blockchain Networks.
- Adding More Data to Fetch.
This project is licensed under the MIT License. See the LICENSE file for details.