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