rusty-api 0.2.1

A secure Rust API crate for rapid development, featuring HTTPS, authentication, privilege levels, and rate limiting.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*!
 * The `api` module provides the core functionality for setting up and running a secure, high-performance API server with rusty-api.
 *
 * This module integrates with the Actix Web framework and includes features such as:
 * - **TLS Support**: Secure communication using Rustls.
 * - **Rate Limiting**: Configurable request limits to prevent abuse.
 * - **CORS Configuration**: Flexible settings for managing cross-origin requests.
 * - **Custom Routes**: Easily define and configure API routes/endpoints.
 *
 * The `Api` struct serves as the main entry point for configuring and starting the server, offering methods for setting
 * up TLS, binding to an address, configuring routes, and more.
 */
use crate::core::config::load_rustls_config;
use crate::routes::Routes;

use actix_web::{App, HttpServer, web};
use actix_governor::{Governor, GovernorConfigBuilder};
use actix_cors::Cors;
use std::sync::{Arc, Once};

static INIT: Once = Once::new();

/**
 * Initialize the crypto provider for Rustls.
 *
 * This function sets up the default crypto provider for Rustls using the `ring` library.
 * It is called once to ensure that the provider is initialized only once during the lifetime
 * of the application.
 */
fn initialize_crypto_provider() {
    INIT.call_once(|| {
        let _ = rustls::crypto::CryptoProvider::install_default(rustls::crypto::ring::default_provider());
    });
}

/**
 * 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
 * ```rust,no_run
 * 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();
 * ```
 */
pub struct Api {
    /// Path to the TLS certificate file used for secure HTTPS communication.
    cert_path: String,

    /// Path to the private key used for TLS.
    key_path: String,

    /// Address to bind the API server to (e.g., "127.0.0.1").
    addr: String,
    
    /// Port to bind the API server to (e.g., 8443).
    port: u16,

    /// Rate limiting configuration: `(requests_per_second, burst_size)`.
    rate_limit: (u64, u32),

    /// Optional custom routes configuration, provided as a closure.
    custom_routes: Option<Arc<dyn Fn(&mut web::ServiceConfig) + Send + Sync>>,

    /// Custom CORS configuration, provided as a closure.
    custom_cors: Arc<dyn Fn() -> Cors + Send + Sync>,

    /// Optional enable user database.
    user_db: bool,

    /// Optional custom route configuration for login.
    login_route: String,

    /// Optional custom route configuration for register.
    register_route: String,
}

impl Api {
    /**
     * Create a new instance of the API server with default settings.
     *
     * # Returns
     * A new `Api` instance with default values.
     *
     * # Example
     * ```rust
     * 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));
     * ```
     */
    pub fn new() -> Self {
        initialize_crypto_provider(); // Ensure the crypto provider is initialized
        Self {
            cert_path: "certs/cert.pem".into(),
            key_path: "certs/key.pem".into(),
            addr: "127.0.0.1".into(),
            port: 8443,
            rate_limit: (3, 20),
            custom_routes: None,
            custom_cors: Arc::new(|| Cors::default()),
            user_db: false,
            login_route: "/login".into(),
            register_route: "/register".into(),
        }
    }

    /**
     * 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
     * ```rust
     * 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");
     * ```
     */
    pub fn certs(mut self, cert: &str, key: &str) -> Self {
        self.cert_path = cert.into();
        self.key_path = key.into();
        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);
     * ```
     */
    pub fn rate_limit(mut self, per_second: u64, burst_size: u32) -> Self {
        self.rate_limit = (per_second, burst_size);
        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
     * ```rust
     * 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");
     * ```
     */
    pub fn bind(mut self, addr: &str, port: u16) -> Self {
        self.addr = addr.into();
        self.port = port;
        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
     * ```rust
     * 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());
     * ```
     */
    pub fn configure_routes(mut self, routes: Routes) -> Self {
        self.custom_routes = Some(Arc::new(move |cfg| routes.configure(cfg)));
        self
    }

    /**
     * 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
     * ```rust
     * use rusty_api;
     *
     * let api = rusty_api::Api::new()
     *     .configure_cors(|| {
     *         rusty_api::Cors::default()
     *             .allow_any_origin()
     *             .allow_any_method()
     *     });
     * ```
     */
    pub fn configure_cors<F>(mut self, cors_config: F) -> Self
    where
        F: Fn() -> Cors + Send + Sync + 'static,
    {
        self.custom_cors = Arc::new(cors_config);
        self
    }

    /// Enable user database with default login and register routes.
    pub fn enable_user_db(self) -> Self {
        self.enable_user_db_with_routes("/login", "/register")
    }

    /// Enable user database with optional custom login and register routes.
    pub fn enable_user_db_with_routes(mut self, login_route: &str, register_route: &str) -> Self {
        self.user_db = true;
        self.login_route = login_route.into();
        self.register_route = register_route.into();
        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
     * ```rust,no_run
     * use rusty_api::Api;
     *
     * let api = Api::new().start();
     */
    pub fn start(self) {
        let rt = actix_web::rt::System::new();
        if let Err(e) = rt.block_on(async {
            println!("INFO: Starting API server...");

            dotenv::dotenv().ok();
            let pool = if self.user_db {
                Some(crate::core::db::init_db().await.expect("Failed to init DB"))
            } else {
                None
            };

            let tls_config = load_rustls_config(&self.cert_path, &self.key_path).expect("TLS failed");

            let governor_config = GovernorConfigBuilder::default()
                .per_second(self.rate_limit.0)
                .burst_size(self.rate_limit.1)
                .finish()
                .unwrap();

            let cors_config = self.custom_cors.clone();

            let bind_addr = format!("{}:{}", self.addr, self.port);

            println!("INFO: Server binding to {}", bind_addr);
            HttpServer::new(move || {
            let cors = (cors_config)();
                let mut app = App::new()
                    .wrap(cors)
                    .wrap(Governor::new(&governor_config));

                // Add app_data for the pool if it exists
                if let Some(pool) = pool.clone() {
                    app = app.app_data(web::Data::new(pool));
                    app = app.configure(|cfg| {
                        crate::core::auth_routes::configure_auth_routes(
                            cfg,
                            &self.login_route,
                            &self.register_route
                        );
                    });
                }

                // Apply custom routes if provided
                if let Some(custom_routes) = &self.custom_routes {
                    app = app.configure(|cfg| custom_routes(cfg));
                }

                app
            })
            .bind_rustls_0_23((self.addr.to_string(), self.port), tls_config)?
            .run()
            .await
        }) {
            println!("ERROR: Failed to start API server: {:?}", e);
        }
    }

    /**
     * Get the path to the TLS certificate file.
     *
     * # Returns
     * A string representing the path to the certificate file.
     *
     * # Example
     * ```rust
     * 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");
     * ```
     */
    pub fn get_cert_path(&self) -> &str { &self.cert_path }

    /**
     * Get the path to the TLS private key file.
     *
     * # Returns
     * A string representing the path to the private key file.
     *
     * # Example
     * ```rust
     * 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");
     * ```
     */
    pub fn get_key_path(&self) -> &str { &self.key_path }

    /**
     * Get the address the server is bound to.
     *
     * # Returns
     * A string representing the address.
     *
     * # Example
     * ```rust
     * use rusty_api::Api;
     * 
     * let api = Api::new().bind("123.4.5.6", 7891);
     * assert_eq!(api.get_addr(), "123.4.5.6");
     * ```
     */
    pub fn get_addr(&self) -> &str { &self.addr }

    /**
     * Get the port the server is bound to.
     *
     * # Returns
     * A u16 representing the port.
     * 
     * # Example
     * ```rust
     * use rusty_api::Api;
     * 
     * let api = Api::new().bind("123.4.5.6", 7891);
     * assert_eq!(api.get_port(), 7891);
     * ```
     */
    pub fn get_port(&self) -> u16 { self.port }

    /**
     * Get the rate limit configuration.
     *
     * # Returns
     * A tuple containing the rate limit configuration: `(requests_per_second, burst_size)`.
     *
     * # Example
     * ```rust
     * use rusty_api::Api;
     *
     * let api = Api::new().rate_limit(5, 10);
     * assert_eq!(api.get_rate_limit(), (5, 10));
     * ```
     */
    pub fn get_rate_limit(&self) -> (u64, u32) { self.rate_limit }

    /**
     * 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
     * ```rust
     * 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");
     * ```
     */
    pub fn get_bind_addr(&self) -> String { format!("{}:{}", self.addr, self.port) }

    /**
     * Get the rate limit per second.
     *
     * # Returns
     * A u64 representing the number of requests allowed per second.
     *
     * # Example
     * ```rust
     * use rusty_api::Api;
     *
     * let api = Api::new().rate_limit(5, 10);
     * assert_eq!(api.get_rate_limit_per_second(), 5);
     * ```
     */
    pub fn get_rate_limit_per_second(&self) -> u64 { self.rate_limit.0 }

    /**
     * Get the rate limit burst size.
     *
     * # Returns
     * A u32 representing the maximum burst size for requests.
     *
     * # Example
     * ```rust
     * use rusty_api::Api;
     *
     * let api = Api::new().rate_limit(5, 10);
     * assert_eq!(api.get_rate_limit_burst_size(), 10);
     * ```
     */
    pub fn get_rate_limit_burst_size(&self) -> u32 { self.rate_limit.1 }

    /**
     * Get the custom CORS configuration.
     *
     * # Returns
     * A reference to the custom CORS configuration closure.
     */
    pub fn get_custom_routes(&self) -> Option<&Arc<dyn Fn(&mut web::ServiceConfig) + Send + Sync>> { self.custom_routes.as_ref() }
}