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
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
- ARoutes
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());
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 aCors
instance and returns a modifiedCors
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()
});
Sourcepub fn enable_user_db(self) -> Self
pub fn enable_user_db(self) -> Self
Enable user database with default login and register routes.
Sourcepub fn enable_user_db_with_routes(
self,
login_route: &str,
register_route: &str,
) -> Self
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.
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();
Sourcepub fn get_cert_path(&self) -> &str
pub fn get_cert_path(&self) -> &str
Sourcepub fn get_key_path(&self) -> &str
pub fn get_key_path(&self) -> &str
Sourcepub fn get_rate_limit(&self) -> (u64, u32)
pub fn get_rate_limit(&self) -> (u64, u32)
Sourcepub fn get_bind_addr(&self) -> String
pub fn get_bind_addr(&self) -> String
Sourcepub fn get_rate_limit_per_second(&self) -> u64
pub fn get_rate_limit_per_second(&self) -> u64
Sourcepub fn get_rate_limit_burst_size(&self) -> u32
pub fn get_rate_limit_burst_size(&self) -> u32
Sourcepub fn get_custom_routes(
&self,
) -> Option<&Arc<dyn Fn(&mut ServiceConfig) + Send + Sync>>
pub fn get_custom_routes( &self, ) -> Option<&Arc<dyn Fn(&mut ServiceConfig) + Send + Sync>>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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