Skip to content

Commit

Permalink
Use compile time macros to load build information
Browse files Browse the repository at this point in the history
Add project metadata in Cargo.toml
Update README.md
Release `v0.0.1`
  • Loading branch information
dormant-user committed Feb 11, 2024
1 parent 76cc1d2 commit f2e8913
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# https://doc.rust-lang.org/cargo/getting-started/first-steps.html#first-steps-with-cargo
[package]
name = "RuStream"
version = "0.0.1-temp"
version = "0.0.1"
description = "An API written in Rust, to stream videos using Actix framework, via authenticated sessions"
license = "MIT"
documentation = "https://thevickypedia.github.io/RuStream"
documentation = "https://docs.rs/RuStream"
homepage = "https://github.com/thevickypedia/RuStream"
repository = "https://github.com/thevickypedia/RuStream"
rust-version = "1.76.0"
keywords = ["streaming", "video-player", "media-server", "rust", "actix"]
categories = ["web-programming::http-server", "asynchronous", "algorithms", "authentication", "rendering::engine"]
include = ["/src", "LICENSE"]
exclude = [".github", ".gitignore", "README.md"]
edition = "2021"
authors = ["Vignesh Rao"]

[lib]
name = "rustream"
path = "src/lib.rs"

[[bin]]
name = "stream"
name = "rustream"
path = "src/main.rs"

[dependencies]
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ curl -o asset -LH "Accept: application/octet-stream" "https://github.com/thevick
```
</details>

<details>
<summary><strong>Add to existing project</strong></summary>

> It is always update to the latest version before running.
###### Sample main.rs
```rust
use rustream;

#[actix_rt::main]
async fn main() {
rustream::start().await.unwrap();
}
```

</details>

#### Arguments
- `debug` - Enable debug level logging

Expand Down
50 changes: 48 additions & 2 deletions src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::{env, path};
use std::collections::HashMap;
use std::sync::Mutex;

Expand All @@ -7,14 +8,59 @@ use minijinja::Environment;

use crate::template;

pub fn get_binary() -> String {
let binary = env::args().next().unwrap();
path::Path::new(&binary).file_name().unwrap().to_str().unwrap().to_string()
}

#[derive(Debug)]
pub struct Cargo {
pub binary: String,
pub crate_name: String,
pub manifest_dir: String,
pub authors: Vec<String>,
pub description: String,
pub homepage: String,
pub pkg_name: String,
pub pkg_repo: String,
pub pkg_version: String,
pub pkg_version_major: String,
pub pkg_version_minor: String,
pub pkg_version_patch: String,
pub pkg_version_pre: String,
}

/// ## References
/// - https://doc.rust-lang.org/cargo/reference/environment-variables.html
/// - https://github.com/rust-lang/cargo/issues/8251#issuecomment-631731144
/// - https://github.com/rust-lang/cargo/issues/11966#issue-1664748892
pub fn build_info() -> Cargo {
let cargo = Cargo {
binary: get_binary(),
crate_name: env!("CARGO_CRATE_NAME").to_string(),
manifest_dir: env!("CARGO_MANIFEST_DIR").to_string(),
authors: env!("CARGO_PKG_AUTHORS").split(',').map(String::from).collect(),
description: env!("CARGO_PKG_DESCRIPTION").to_string(),
homepage: env!("CARGO_PKG_HOMEPAGE").to_string(),
pkg_name: env!("CARGO_PKG_NAME").to_string(),
pkg_repo: env!("CARGO_PKG_REPOSITORY").to_string(),
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
pkg_version_major: env!("CARGO_PKG_VERSION_MAJOR").to_string(),
pkg_version_minor: env!("CARGO_PKG_VERSION_MINOR").to_string(),
pkg_version_patch: env!("CARGO_PKG_VERSION_PATCH").to_string(),
pkg_version_pre: env!("CARGO_PKG_VERSION_PRE").to_string(),
};
cargo
}

lazy_static! {
pub static ref FERNET: Fernet = Fernet::new(&generate_key()).unwrap();
}

/// Create a Fernet object to encrypt and decrypt session token.
///
/// References:
/// https://docs.rs/fernet/latest/fernet/
/// ## References:
/// https://docs.rs/fernet/latest/fernet/
fn generate_key() -> String {
Fernet::generate_key()
}
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate actix_web;

use std::{env, io};
use std::io;

use actix_web::{App, HttpServer, middleware, web};
use rand::prelude::SliceRandom;
Expand All @@ -12,18 +12,20 @@ mod constant;
mod routes;

pub async fn start() -> io::Result<()> {
let cargo = constant::build_info();
let args = squire::parser::arguments();

squire::startup::init_logger(args.debug);
println!("Welcome to RuStream - A Rust API, to stream videos using Actix framework, via authenticated sessions");
squire::startup::init_logger(args.debug, &cargo);
println!("{}[v{}] - {}", cargo.pkg_name, cargo.pkg_version, cargo.description);
let arts = [squire::ascii_art::DOG, squire::ascii_art::DOLPHIN, squire::ascii_art::HORSE];
println!("{}", arts.choose(&mut rand::thread_rng()).unwrap());

let config = squire::startup::get_config(args);
// Create a dedicated clone, since it will be used within closure
let config_clone = config.clone();
let host = format!("{}:{}", config.video_host, config.video_port);
log::info!("{} [workers:{}] running on http://{} (Press CTRL+C to quit)", env!("CARGO_PKG_NAME"), config.workers, host);
log::info!("{} [workers:{}] running on http://{} (Press CTRL+C to quit)",
cargo.pkg_name, config.workers, host);
/*
|| syntax is creating a closure that serves as the argument to the HttpServer::new() method.
The closure is defining the configuration for the Actix web server.
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[allow(non_snake_case)]
use RuStream::start;
use rustream;

#[actix_rt::main]
async fn main() {
start().await.unwrap();
rustream::start().await.unwrap();
}
11 changes: 6 additions & 5 deletions src/squire/startup.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use std::{env, path};
use std::sync::Arc;
use crate::constant::Cargo;

use crate::squire::parser::Args;
use crate::squire::settings::Config;

pub fn init_logger(debug: bool) {
let logging_level;
pub fn init_logger(debug: bool, cargo: &Cargo) {
if debug {
logging_level = "actix_web=debug,actix_server=info,stream=debug,RuStream=debug";
env::set_var("RUST_LOG", format!("actix_web=debug,actix_server=info,{}=debug,{}=debug",
cargo.binary, cargo.crate_name));
env::set_var("RUST_BACKTRACE", "1");
} else {
// set actix logging to warning mode since it becomes too noisy when streaming a giant video file
logging_level = "actix_web=warn,actix_server=warn,stream=info,RuStream=info";
env::set_var("RUST_LOG", format!("actix_web=warn,actix_server=warn,{}=info,{}=info",
cargo.binary, cargo.crate_name));
env::set_var("RUST_BACKTRACE", "0");
}
env::set_var("RUST_LOG", logging_level);
env_logger::init();
}

Expand Down

0 comments on commit f2e8913

Please sign in to comment.