diff --git a/README.md b/README.md index 699c82e..ca0a88d 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ A lightweight, scalable JSON REST API framework designed for speed and agility. - [Installation](#installation) - [Usage](#usage) -- [Api](#api) - - [`Server.start(addr)`](#serverstartaddr) - - [`Server.add_process(name, func)`](#serveradd_processname-func) +- [API](#api) + - [`Server.start(addr: SocketAddr)`](#serverstartaddr-socketaddr) + - [`Server.add_process(name: &str, func: F)`](#serveradd_processfname-str-func-f) - [Benchmarking](#benchmarking) ## Installation @@ -68,13 +68,80 @@ async fn main() -> Result<(), Box> { This basic setup provides an ideal starting point for building scalable and efficient servers with **Pterodactyl**, while still giving you flexibility over most components. -## API -The **Pterodactyl** package has a public class `Server`, this class is the core of the package everything goes through this class. +## API Documentation -### `Server.start(addr)` -### `Server.add_process(name, func)` +The **Pterodactyl** package provides a public class called `Server`, which serves as the core of the package. All operations are performed through this class. -## Benchmarking +### `Server.start(addr: SocketAddr)` + +This function starts the server, binding it to the provided socket address (`addr`). + +- **Parameters**: + - `addr`: A `SocketAddr` that specifies the address and port on which the server will listen. + +#### Example + +```rust +use pterodactyl::Server; +use std::net::SocketAddr; +use std::sync::Arc; + +#[tokio::main] +async fn main() { + // Define the address where the server will listen (127.0.0.1:3000) + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + + // Create a new instance of the Server + let server = Server::new(); + + // Start the server asynchronously + Arc::new(server).start(addr).await; +} +``` + +This example demonstrates how to start a new server instance listening on `127.0.0.1:3000` using the Tokio runtime for asynchronous execution. + +### `Server.add_process(name: &str, func: F)` + +This method allows you to add a new process (handler) to the server. The process is identified by its name, and the associated function is invoked when the process is triggered. + +- **Parameters**: + - `name`: A `&str` that specifies the name of the process. + - `func`: A function or closure with the signature `Fn() -> Result`, which returns a `ProcessResponse` on success or failure. + +#### Example + +```rust +use pterodactyl::{Server, ProcessResponse}; +use std::net::SocketAddr; +use std::sync::Arc; + +fn main() { + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + + // Create a new server instance + let server = Server::new(); + + // Add a new process named "example" that returns a JSON response + server.add_process("example", || { + let response = ProcessResponse::new( + 200, // HTTP status code + "Success".to_string(), // Status message + serde_json::json!({ // JSON response body + "message": "Hello, World!", + "data": { + "key": "value" + } + }), + [].to_vec(), // Optional headers + ); + Ok(response) // Return the response + }); +} +``` +In this example, we create a process named `"example"` that returns a JSON response with a status code of 200. This process will be available on the server once it's added. + +## Benchmarking