pub struct Api { /* private fields */ }Expand description
The Api struct is the main entry point for configuring and running the API server.
This struct provides methods to set up TLS, configure routes, manage CORS settings, apply rate limiting, and bind the server to a specific address and port. It is designed to simplify the process of building secure and high-performance APIs using the Actix Web framework.
§Features
- TLS Support: Configure paths to certificate and key files for secure HTTPS communication.
 - Rate Limiting: Set request limits to prevent abuse.
 - CORS Configuration: Customize cross-origin resource sharing settings.
 - Custom Routes: Define and configure API routes using the 
Routesbuilder. 
§Example
use rusty_api::Api;
let api = Api::new()
     .certs("certs/cert.pem", "certs/key.pem")
     .rate_limit(5, 10)
     .bind("127.0.0.1", 8443)
     .configure_cors(|| {
         rusty_api::Cors::default()
             .allow_any_origin()
             .allow_any_method()
     });
 
api.start();Implementations§
Source§impl Api
 
impl Api
Sourcepub fn new() -> Self
 
pub fn new() -> Self
Create a new instance of the API server with default settings.
§Returns
A new Api instance with default values.
§Example
use rusty_api::Api;
let api = Api::new();
assert_eq!(api.get_cert_path(), "certs/cert.pem");
assert_eq!(api.get_key_path(), "certs/key.pem");
assert_eq!(api.get_addr(), "127.0.0.1");
assert_eq!(api.get_port(), 8443);
assert_eq!(api.get_rate_limit(), (3, 20));Sourcepub fn certs(self, cert: &str, key: &str) -> Self
 
pub fn certs(self, cert: &str, key: &str) -> Self
Set the certificate and key paths for TLS.
§Arguments
cert- Path to the certificate file.key- Path to the private key file.
§Returns
A mutable reference to the Api instance.
§Example
use rusty_api::Api;
let api = Api::new().certs("path/to/cert.pem", "path/to/key.pem");
assert_eq!(api.get_cert_path(), "path/to/cert.pem");
assert_eq!(api.get_key_path(), "path/to/key.pem");Sourcepub fn rate_limit(self, per_second: u64, burst_size: u32) -> Self
 
pub fn rate_limit(self, per_second: u64, burst_size: u32) -> Self
Set the rate limit for API requests.
§Arguments
per_second- Number of requests allowed per second.burst_size- Maximum burst size for requests.
§Returns
A mutable reference to the Api instance.
§Example
use rusty_api::Api;
let api = Api::new().rate_limit(5, 10);
assert_eq!(api.get_rate_limit_per_second(), 5);
assert_eq!(api.get_rate_limit_burst_size(), 10);Sourcepub fn bind(self, addr: &str, port: u16) -> Self
 
pub fn bind(self, addr: &str, port: u16) -> Self
Set the address and port for the API server.
§Arguments
addr- Address to bind the server to.port- Port to bind the server to.
§Returns
A mutable reference to the Api instance.
§Example
use rusty_api::Api;
let api = Api::new().bind("127.0.0.1", 8443);
assert_eq!(api.get_bind_addr(), "127.0.0.1:8443");Sourcepub fn configure_routes(self, routes: Routes) -> Self
 
pub fn configure_routes(self, routes: Routes) -> Self
Configure custom routes for the API server.
§Arguments
routes- ARoutesinstance containing the custom routes.
§Returns
A mutable reference to the Api instance.
§Example
use rusty_api::Api;
use rusty_api::Routes;
async fn example_route(_req: rusty_api::HttpRequest) -> rusty_api::HttpResponse {
  rusty_api::HttpResponse::Ok().body("Example route accessed!")
}
let routes = Routes::new()
   .add_route("/example", example_route);
let api = Api::new()
   .configure_routes(routes);
assert!(api.get_custom_routes().is_some());Sourcepub fn configure_cors<F>(self, cors_config: F) -> Self
 
pub fn configure_cors<F>(self, cors_config: F) -> Self
Configure CORS settings.
§Arguments
cors- A closure that takes aCorsinstance and returns a modifiedCorsinstance.
§Returns
A mutable reference to the Api instance.
§Example
use rusty_api;
let api = rusty_api::Api::new()
    .configure_cors(|| {
        rusty_api::Cors::default()
            .allow_any_origin()
            .allow_any_method()
    });Sourcepub fn start(self)
 
pub fn start(self)
Start the API server.
This method initializes the server and begins listening for incoming requests. It will block the current thread until the server is stopped.
§Example
use rusty_api::Api;
let api = Api::new().start();