rustream/
lib.rs

1#![allow(rustdoc::bare_urls)]
2#![doc = include_str!("../README.md")]
3
4#[macro_use]
5extern crate actix_web;
6
7use std::io;
8
9use actix_web::{App, HttpServer, middleware, web};
10use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
11
12/// Module for the structs and functions called during startup.
13mod constant;
14/// Module for all the API entry points.
15mod routes;
16/// Module to store all the helper functions.
17mod squire;
18/// Module to load all the templates for the UI.
19mod templates;
20
21/// Contains entrypoint and initializer settings to trigger the asynchronous `HTTPServer`
22///
23/// # Examples
24///
25/// ```no_run
26/// #[actix_rt::main]
27/// async fn main() {
28///     match rustream::start().await {
29///         Ok(_) => {
30///             println!("RuStream session terminated")
31///         }
32///         Err(err) => {
33///             eprintln!("Error starting rustream: {}", err)
34///         }
35///     }
36/// }
37/// ```
38pub async fn start() -> io::Result<()> {
39    let metadata = constant::build_info();
40    let config = squire::startup::get_config(&metadata);
41
42    squire::startup::init_logger(config.debug, config.utc_logging, &metadata.crate_name);
43    println!("{}[v{}] - {}", &metadata.pkg_name, &metadata.pkg_version, &metadata.description);
44    squire::ascii_art::random();
45
46    // Log a warning message for max payload size beyond 1 GB
47    if config.max_payload_size > 1024 * 1024 * 1024 {
48        // Since the default is just 100 MB, the only way to get here is to have an env var
49        log::warn!("Max payload size is set to '{}' which exceeds the optimal upload size.",
50            std::env::var("max_payload_size").unwrap());
51        log::warn!("Please consider network bandwidth and latency, before using RuStream to upload such high-volume data.");
52    }
53
54    if config.secure_session {
55        log::warn!(
56            "Secure session is turned on! This means that the server can ONLY be hosted via HTTPS or localhost"
57        );
58    }
59    // Create a dedicated clone, since it will be used within closure
60    let config_clone = config.clone();
61    let host = format!("{}:{}", config.media_host, config.media_port);
62    log::info!("{} [workers:{}] running on http://{} (Press CTRL+C to quit)",
63        &metadata.pkg_name, &config.workers, &host);
64    let jinja = templates::environment();
65    let fernet = constant::fernet_object();
66    let session = constant::session_info();
67    /*
68        || syntax is creating a closure that serves as the argument to the HttpServer::new() method.
69        The closure is defining the configuration for the Actix web server.
70        The purpose of the closure is to configure the server before it starts listening for incoming requests.
71     */
72    let application = move || {
73        App::new()  // Creates a new Actix web application
74            .app_data(web::Data::new(config_clone.clone()))
75            .app_data(web::Data::new(jinja.clone()))
76            .app_data(web::Data::new(fernet.clone()))
77            .app_data(web::Data::new(session.clone()))
78            .app_data(web::Data::new(metadata.clone()))
79            .app_data(web::PayloadConfig::default().limit(config_clone.max_payload_size))
80            .wrap(squire::middleware::get_cors(config_clone.websites.clone()))
81            .wrap(middleware::Logger::default())  // Adds a default logger middleware to the application
82            .service(routes::basics::health)  // Registers a service for handling requests
83            .service(routes::basics::root)
84            .service(routes::auth::login)
85            .service(routes::auth::logout)
86            .service(routes::auth::home)
87            .service(routes::basics::profile)
88            .service(routes::fileio::edit)
89            .service(routes::auth::error)
90            .service(routes::media::track)
91            .service(routes::media::stream)
92            .service(routes::media::streaming_endpoint)
93            .service(routes::upload::upload_files)
94            .service(routes::upload::save_files)
95    };
96    let server = HttpServer::new(application)
97        .workers(config.workers)
98        .max_connections(config.max_connections);
99    // Reference: https://actix.rs/docs/http2/
100    if config.cert_file.exists() && config.key_file.exists() {
101        log::info!("Binding SSL certificate to serve over HTTPS");
102        let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
103        builder.set_private_key_file(&config.key_file, SslFiletype::PEM).unwrap();
104        builder.set_certificate_chain_file(&config.cert_file).unwrap();
105        server.bind_openssl(host, builder)?
106            .run()
107            .await
108    } else {
109        server.bind(host)?
110            .run()
111            .await
112    }
113}