Struct Api

Source
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 Routes builder.

§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

Source

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));
Source

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");
Source

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);
Source

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");
Source

pub fn configure_routes(self, routes: Routes) -> Self

Configure custom routes for the API server.

§Arguments
  • routes - A Routes instance containing the custom routes.
§Returns

A mutable reference to the Api instance.

§Example
use rusty_api::Api;
use rusty_api::Routes;
use rusty_api::Method;

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(Method::GET, "/example", example_route);

let api = Api::new()
   .configure_routes(routes);

assert!(api.get_custom_routes().is_some());
Source

pub fn configure_cors<F>(self, cors_config: F) -> Self
where F: Fn() -> Cors + Send + Sync + 'static,

Configure CORS settings.

§Arguments
  • cors - A closure that takes a Cors instance and returns a modified Cors instance.
§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()
    });
Source

pub fn enable_user_db(self) -> Self

Enable user database with default login and register routes.

Source

pub fn enable_user_db_with_routes( self, login_route: &str, register_route: &str, ) -> Self

Enable user database with optional custom login and register routes.

Source

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();
Source

pub fn get_cert_path(&self) -> &str

Get the path to the TLS certificate file.

§Returns

A string representing the path to the certificate file.

§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");
Source

pub fn get_key_path(&self) -> &str

Get the path to the TLS private key file.

§Returns

A string representing the path to the private key file.

§Example
use rusty_api::Api;

let api = Api::new().certs("path/to/cert.pem", "path/to/key.pem");
assert_eq!(api.get_key_path(), "path/to/key.pem");
Source

pub fn get_addr(&self) -> &str

Get the address the server is bound to.

§Returns

A string representing the address.

§Example
use rusty_api::Api;
 
let api = Api::new().bind("123.4.5.6", 7891);
assert_eq!(api.get_addr(), "123.4.5.6");
Source

pub fn get_port(&self) -> u16

Get the port the server is bound to.

§Returns

A u16 representing the port.

§Example
use rusty_api::Api;
 
let api = Api::new().bind("123.4.5.6", 7891);
assert_eq!(api.get_port(), 7891);
Source

pub fn get_rate_limit(&self) -> (u64, u32)

Get the rate limit configuration.

§Returns

A tuple containing the rate limit configuration: (requests_per_second, burst_size).

§Example
use rusty_api::Api;

let api = Api::new().rate_limit(5, 10);
assert_eq!(api.get_rate_limit(), (5, 10));
Source

pub fn get_bind_addr(&self) -> String

Get the address and port the server is bound to as a single string.

§Returns

A string representing the address and port in the format “address:port”.

§Example
use rusty_api::Api;

let api = Api::new().bind("123.4.5.6", 7891);
assert_eq!(api.get_bind_addr(), "123.4.5.6:7891");
Source

pub fn get_rate_limit_per_second(&self) -> u64

Get the rate limit per second.

§Returns

A u64 representing the number of requests allowed per second.

§Example
use rusty_api::Api;

let api = Api::new().rate_limit(5, 10);
assert_eq!(api.get_rate_limit_per_second(), 5);
Source

pub fn get_rate_limit_burst_size(&self) -> u32

Get the rate limit burst size.

§Returns

A u32 representing the maximum burst size for requests.

§Example
use rusty_api::Api;

let api = Api::new().rate_limit(5, 10);
assert_eq!(api.get_rate_limit_burst_size(), 10);
Source

pub fn get_custom_routes( &self, ) -> Option<&Arc<dyn Fn(&mut ServiceConfig) + Send + Sync>>

Get the custom CORS configuration.

§Returns

A reference to the custom CORS configuration closure.

Auto Trait Implementations§

§

impl Freeze for Api

§

impl !RefUnwindSafe for Api

§

impl Send for Api

§

impl Sync for Api

§

impl Unpin for Api

§

impl !UnwindSafe for Api

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,