Apikeys-rs
Description
Apikeys-rs is a comprehensive toolkit designed to streamline API key management for your backend services. It offers an intuitive abstraction layer for API key storage and a versatile key-based rate limiter, ensuring seamless integration with your preferred storage solutions.
The library comes with a ready-to-use Axum layer and includes built-in storage options and limiters.
Api Key Storage
- Memory Storage
- MongoDB Storage
Rate Limiter
- Redis Limiter
Todo
- Increase test coverage
- Add more storage interfaces
- Add more limiters
- Implement a weight system to make api calls have their own usage strategy, limits and computational cost
Table of Contents
Installation
cargo add apikeys-rs
Basic Usage
Initialize storage
use ;
dotenv.ok;
let uri = var.expect;
let db_name = var.expect;
let mut storage = new
.await
.expect;
Store a key
// [...] imports
let key = "test_key";
let api_key_config = ApiKey ;
let result = storage.store_api_key.await;
Retrieve a key
// [...] imports
let key = "test_key";
let result = storage.retrieve_api_key.await;
match result
Redis Limiter
use ;
let redis_uri = "redis_connection_string";
let redis_limiter = new.expect;
let api_key = /* [...] get api key from storage (see above) */;
let result = redis_limiter.use_key;
match result
Axum Layer Usage
// [...] imports
// Create the API Key Storage
let api_key_storage = new
.await
.expect;
// Create the API Key Limiter
let api_key_limiter = new
.await
.expect;
// Create the API Key Manager
let api_key_manager = new;
// Create the API Key Axum Layer
let api_key_layer = new;
let app = new
.route
// Configure Axum to use the Api Key layer
.layer
.with_state;
Contributing
Feel free to open issues and send PRs. We will evaluate them together in the comment section.
License
This project is licensed under the MIT License.