Files
tuliprox/src/api/main_api.rs
T

79 lines
2.5 KiB
Rust
Raw Normal View History

2023-10-25 13:27:15 +02:00
use std::collections::HashMap;
use std::io::ErrorKind;
2021-10-07 20:11:54 +02:00
use std::path::PathBuf;
2023-10-25 13:27:15 +02:00
use std::sync::{Arc, Mutex};
2021-10-07 20:11:54 +02:00
use actix_cors::Cors;
use actix_files::NamedFile;
2023-10-25 09:57:44 +02:00
use actix_web::{App, get, HttpRequest, HttpServer, web};
use actix_web::middleware::Logger;
2023-11-02 20:03:22 +01:00
use crate::api::m3u_api::{m3u_api_register};
2021-10-07 20:11:54 +02:00
2023-10-25 09:57:44 +02:00
use crate::api::api_model::{AppState};
2023-11-02 20:03:22 +01:00
use crate::api::scheduler::start_scheduler;
2023-10-25 09:57:44 +02:00
use crate::api::v1_api::{v1_api_register};
2023-11-02 20:03:22 +01:00
use crate::api::xmltv_api::{xmltv_api_register};
use crate::api::xtream_player_api::{xtream_api_register};
2023-10-25 09:57:44 +02:00
use crate::model::config::{Config,ProcessTargets};
2021-10-07 20:11:54 +02:00
#[get("/")]
async fn index(
_req: HttpRequest,
_app_state: web::Data<AppState>,
) -> std::io::Result<NamedFile> {
2023-10-17 09:03:37 +02:00
let path: PathBuf = [&_app_state.config.api.web_root, "index.html"].iter().collect();
2023-05-03 17:46:39 +02:00
NamedFile::open(path)
2021-10-07 20:11:54 +02:00
}
#[actix_web::main]
2023-10-19 18:56:08 +02:00
pub(crate) async fn start_server(cfg: Arc<Config>, targets: Arc<ProcessTargets>) -> futures::io::Result<()> {
2023-10-17 09:31:57 +02:00
let host = cfg.api.host.to_string();
2021-10-07 20:11:54 +02:00
let port = cfg.api.port;
2023-10-17 09:31:57 +02:00
let web_dir = cfg.api.web_root.to_string();
2021-10-15 16:59:20 +02:00
let web_dir_path = PathBuf::from(&web_dir);
if !web_dir_path.exists() || !web_dir_path.is_dir() {
return Err(std::io::Error::new(ErrorKind::NotFound, format!("web_root does not exists or is not an directory: {:?}", &web_dir_path)));
2021-10-15 16:59:20 +02:00
}
2021-10-07 20:11:54 +02:00
let schedule = cfg.schedule.clone();
2023-09-26 19:41:02 +02:00
2023-09-26 19:53:45 +02:00
let shared_data = web::Data::new(AppState {
config: cfg,
targets,
2023-10-25 13:27:15 +02:00
downloads: Arc::new(Mutex::new(HashMap::new()))
2023-09-26 19:53:45 +02:00
});
2023-09-26 19:41:02 +02:00
// Scheduler
if let Some(expression) = schedule {
2023-09-26 19:53:45 +02:00
let cloned_data = shared_data.clone();
2023-09-26 19:41:02 +02:00
actix_rt::spawn(async move {
2023-09-26 19:53:45 +02:00
start_scheduler(&expression, cloned_data).await
2023-09-26 19:41:02 +02:00
});
}
// Web Server
2021-10-07 20:11:54 +02:00
HttpServer::new(move || App::new()
.wrap(Logger::default())
2021-10-07 20:11:54 +02:00
.wrap(Cors::default()
.supports_credentials()
.allow_any_origin()
.allowed_methods(vec!["GET", "POST", "OPTIONS", "HEAD"])
.allow_any_header()
.max_age(3600)
)
.app_data(shared_data.clone())
2023-10-25 09:57:44 +02:00
.service(v1_api_register())
2023-11-02 20:03:22 +01:00
.service(xtream_api_register())
.service(m3u_api_register())
.service(xmltv_api_register())
2021-10-07 20:11:54 +02:00
.service(index)
2023-10-17 09:31:57 +02:00
.service(actix_files::Files::new("/", web_dir.to_string()))
2021-10-07 20:11:54 +02:00
)
.bind(format!("{}:{}", host, port))?
.run().await
//
// .service(actix_files::Files::new("/static", ".").show_files_listing())
2023-09-26 19:41:02 +02:00
}