This commit is contained in:
Simon
2025-05-31 09:28:30 +00:00
parent f0fcd91545
commit 96b914fb2e
11 changed files with 859 additions and 1 deletions

80
src/main.rs Normal file
View File

@@ -0,0 +1,80 @@
use ntex_files as fs;
use ntex::web;
use ntex::web::HttpResponse;
use serde::Deserialize;
use serde_json::{json};
use std::thread;
use std::time::Duration;
mod api;
mod status;
mod videos;
mod providers;
mod util;
#[derive(Clone, Debug, Deserialize)]
struct Metadata {
name: String,
version: String,
author: String,
}
// type getVideosFn = fn get_videos(channel: Option<String>, sort: Option<String>, query: Option<String>, page: Option<String>, per_page: Option<String>) -> Videos;
async fn metadata(data: web::types::State<Metadata>) -> HttpResponse {
async fn counter(x: String) -> String{
for i in 1..=5 {
println!("{}: {}", x, i);
thread::sleep(Duration::from_secs(1));
}
return x;
}
let meta = data.get_ref().clone();
let mut handles = vec![];
for i in 1..=3 {
let name = format!("{}-{}", meta.name, i);
handles.push(thread::spawn(move || {
futures::executor::block_on(counter(name.clone()))
}));
}
let results: Vec<String> = handles
.into_iter()
.map(|handle| handle.join().unwrap())
.collect();
println!("Results: {:?}", results);
HttpResponse::Ok().json(
&json!({
"description": "A simple web server for the Hot Tub app.",
"documentation": ""
}),
)
}
#[ntex::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "ntex=warn");
std::env::set_var("RUST_BACKTRACE", "1");
env_logger::init(); // You need this to actually see logs
web::HttpServer::new(|| {
const METADATA_JSON: &str = include_str!("../metadata.json");
let mut meta: Metadata = serde_json::from_str(METADATA_JSON).unwrap();
web::App::new()
.wrap(web::middleware::Logger::default())
.state(meta.clone())
.route("/meta", web::get().to(metadata))
.service(web::scope("/api").configure(api::config))
.service(fs::Files::new("/", "static"))
})
// .bind_openssl(("0.0.0.0", 18080), builder)?
.bind(("0.0.0.0", 18080))?
.run()
.await
}