rusty_api/lib.rs
1/*!
2 * [![github]](https://github.com/AlexanderHeffernan/rusty-api) [![crates-io]](https://crates.io/crates/rusty-api) [![docs-rs]](crate)
3 * [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4 * [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5 * [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
6 *
7 * Rusty API is lightweight and secure library for building backend APIs in Rust, designed to simplify the development of modern web services.
8 *
9 * Features:
10 * - **Modular Design**: Oragnized into modules for API logic, routing, and core configurations, making it easy to extend and maintain.
11 * - **Actix Web Integration**: Built on the Actix Web framework for high-performance and asynchronous web applications.
12 * - **TLS Support**: Includes utilities for configuring Rustls for secure HTTPS communication.
13 * - **CORS Handling**: Provides seamless integration with Actix CROS for managing croos-origin requests.
14 * - **Password Protection**: Offers built-in support for password-protected routes, enhancing security for sensitive endpoints.
15 *
16 * Rust API is ideal for developers looking to quickly build secure and scalable APIs with minimal boilerplate.
17 *
18 * <br>
19 *
20 * ## Installation
21 * To use rusty-api in your project, add the following to your `Cargo.toml`:
22 * ```toml
23 * [dependencies]
24 * rusty-api = "0.1.8"
25 * ```
26 *
27 * ## Example
28 * ### Setting up your API
29 * Here's an example of how to use rusty-api to create an API with public and password-protected routes:
30 * ```rust,no_run,ignore
31 * use rusty_api;
32 *
33 * async fn password_route(_req: rusty_api::HttpRequest) -> rusty_api::HttpResponse {
34 * rusty_api::HttpResponse::Ok().body("Password route accessed!")
35 * }
36 *
37 * async fn open_route(_req: rusty_api::HttpRequest) -> rusty_api::HttpResponse {
38 * rusty_api::HttpResponse::Ok().body("Open route accessed!")
39 * }
40 *
41 * fn main() {
42 * let routes = rusty_api::Routes::new()
43 * .add_route_with_password("/password_route", password_route, "Password123")
44 * .add_route("/open_route", open_route);
45 *
46 * rusty_api::Api::new()
47 * .certs("certs/cert.pem", "certs/key.pem")
48 * .rate_limit(3, 20)
49 * .bind("127.0.0.1", 8443)
50 * .configure_routes(routes)
51 * .configure_cors(|| {
52 * rusty_api::Cors::default()
53 * .allow_any_origin()
54 * .allow_any_method()
55 * .allowed_header("ngrok-skip-browser-warning")
56 * })
57 * .start();
58 * }
59 * ```
60 *
61 * ### Generating Self-Signed Certificates
62 * HTTPS requires TLS certificates. You can generate self-signed certificates using OpenSSL:
63 * ```bash
64 * mkdir -p certs
65 * openssl req -x509 -newkey rsa:4096 -keyout certs/key.pem -out certs/cert.pem
66 * ```
67 *
68 * ### Running the API
69 * To run the API, use the following command:
70 * ```bash
71 * cargo run
72 * ```
73 */
74
75pub mod api;
76pub mod routes;
77pub mod core;
78
79pub use crate::api::Api;
80pub use crate::routes::Routes;
81pub use crate::core::config::load_rustls_config;
82
83pub use actix_web::{HttpResponse, HttpRequest};
84pub use actix_web::http::StatusCode;
85pub use actix_cors::Cors;