| Crates.io | corex-api |
| lib.rs | corex-api |
| version | 0.1.1 |
| created_at | 2025-01-30 22:37:43.45442+00 |
| updated_at | 2025-01-30 22:40:38.818968+00 |
| description | A lightweight,modular API framework for building extensible systems in Rust. |
| homepage | |
| repository | https://github.com/denver-code/corex |
| max_upload_size | |
| id | 1536933 |
| size | 10,451 |
CoreX is a modular API framework for building extensible systems in Rust. It allows you to create a core system and extend its functionality through plugins (extensions). This crate is designed to be simple, flexible, and easy to use, making it ideal for building modular web applications.
axum and tokio for high-performance asynchronous operations.Add corex-api to your Cargo.toml:
[dependencies]
corex-api = "0.1.0"
Create an extension by implementing the ExtensionTrait:
use axum::{Router, routing::get, response::Json};
use serde_json::json;
use corex_api::{CoreX, ExtensionTrait};
use std::sync::Arc;
struct AuthExtension;
impl ExtensionTrait for AuthExtension {
fn name(&self) -> &'static str {
"AuthExtension"
}
fn extend(&self, router: Router) -> Router {
router.route("/auth", get(|| async { Json(json!({ "message": "Auth endpoint" })) }))
}
}
Use the CoreX system to register extensions and start the server:
#[tokio::main]
async fn main() {
let mut core = CoreX::new("127.0.0.1".to_string(), 3000);
core.register_extension(Arc::new(AuthExtension));
core.run().await;
}
Start the server and test the endpoints:
curl http://localhost:3000/auth
Response:
{
"message": "Auth endpoint"
}
You can register multiple extensions to extend the core system:
struct ExampleExtension;
impl ExtensionTrait for ExampleExtension {
fn name(&self) -> &'static str {
"ExampleExtension"
}
fn extend(&self, router: Router) -> Router {
router.route("/example", get(|| async { Json(json!({ "message": "Example endpoint" })) }))
}
}
#[tokio::main]
async fn main() {
let mut core = Core::new("127.0.0.1".to_string(), 3000);
core.register_extension(Arc::new(AuthExtension));
core.register_extension(Arc::new(ExampleExtension));
core.run().await;
}
Test the endpoints:
curl http://localhost:3000/auth
curl http://localhost:3000/example
CoreXnew(host: String, port: u16): Creates a new Core system.register_extension(extension: Arc<dyn ExtensionTrait>): Registers an extension.build() -> Router: Builds the final router by applying all registered extensions.run(): Starts the server and listens for incoming requests.ExtensionTraitname() -> &'static str: Returns the name of the extension.extend(router: Router) -> Router: Extends the router with additional routes or middleware.To run the tests, use the following command:
cargo test
Contributions are welcome! If you'd like to contribute, please:
This project is licensed under:
If you have any questions or need help, feel free to open an issue or reach out to the maintainers.