This project is a decentralized API gateway built in Rust. The gateway handles routing, authentication using JWT, WebSocket support, and real-time interaction with the Ethereum blockchain. It can retrieve the latest block from the Ethereum network via Infura.
- JWT Authentication: Protect API routes using JWT tokens.
- WebSocket Support: Enable real-time communication with WebSocket connections.
- Ethereum Integration: Retrieve the latest block from the Ethereum network.
- API Gateway: Acts as a middleware for managing routing and authentication for decentralized applications.
Cargo.toml
: Project configuration and dependencies.src/main.rs
: The main entry point for the Actix Web server.src/blockchain.rs
: Ethereum integration using theweb3
crate to retrieve the latest block.src/jwt.rs
: Handles JWT token creation and validation.src/ws_handler.rs
: WebSocket handler for real-time communication..env.example
: Example environment configuration.
- Rust (1.72 or higher)
- Infura Project ID for Ethereum integration.
This project uses environment variables to securely store API keys and other sensitive information. You'll need to create a .env
file in the root of the project.
Ensure Rust is installed on your system by following the instructions on rust-lang.org.
You can copy the .env.example
file to .env
and fill in your Infura Project ID.
cp .env.example .env
Update the .env file with your Infura Project ID:
INFURA_PROJECT_ID=your_infura_project_id_here
You can get your Infura Project ID by signing up on Infura.
Install the required Rust dependencies by running:
cargo build
To run the API gateway, simply use the following command:
cargo run
The server will start, listening on localhost:9090, unless you specify different port in .env file.
- Endpoint: /auth
- Method: POST
- Description: Generates a JWT token for authentication.
- Request Body:
{ "user_id": "your_user_id" }
- Response:
{ "token": "your_jwt_token" }
- Endpoint: /secure
- Method: GET
- Description: Protected route that requires a JWT token in the Authorization header.
- Authorization Header:
Authorization: Bearer your_jwt_token
- Endpoint: /ws/
- Description: WebSocket connection for real-time communication.
- Use a WebSocket client like wscat:
wscat -c ws://localhost:8080/ws/
- Endpoint: /ethereum/latest_block
- Method: GET
- Description: Retrieves the latest block from the Ethereum blockchain.
- Response:
{ "block_number": 20898871 }
You can use curl or tools like Postman or curl to interact with the API. For example, to get the latest Ethereum block:
curl http://localhost:8080/ethereum/latest_block
To test JWT-protected routes, first generate a token via /auth, then use the token in subsequent requests:
curl -X POST http://localhost:8080/auth -H "Content-Type: application/json" -d '{"user_id": "test_user"}'
Use the returned token in the Authorization header to access /secure:
curl http://localhost:8080/secure -H "Authorization: Bearer your_jwt_token"
The project uses the following dependencies:
- Actix Web: A powerful, pragmatic, and extremely fast web framework for Rust.
- Web3: Ethereum JSON-RPC client for interacting with Ethereum.
- JWT: Used for generating and validating JSON Web Tokens.
- dotenv: Manage environment variables securely.
- Tokio: Asynchronous runtime for Rust.
This project is licensed under the MIT License.