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
use actix_cors::Cors;
use actix_web::http::header;

/// Configures and returns a CORS middleware based on provided website origins.
///
/// # Arguments
///
/// * `websites` - A vector of allowed website origins for CORS.
///
/// # Returns
///
/// A configured `Cors` middleware instance.
pub fn get_cors(websites: Vec<String>) -> Cors {
    let mut origins = vec!["http://localhost.com".to_string(), "https://localhost.com".to_string()];
    if !websites.is_empty() {
        origins.extend_from_slice(&websites);
    }
    // Create a clone to append /* to each endpoint, and further extend the same vector
    let cloned = origins.clone().into_iter().map(|x| format!("{}/{}", x, "*"));
    origins.extend(cloned);
    let mut cors = Cors::default()
        .allowed_methods(vec!["GET", "POST"])
        .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
        .allowed_header(header::CONTENT_TYPE)
        .max_age(3600);  // Maximum time (in seconds) for which this CORS request may be cached
    for origin in origins {
        cors = cors.allowed_origin(&origin);
    }
    cors
}