From 94816903c3404dacae59523d18e39a45a5ba5449 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Tue, 19 Jul 2022 11:11:23 +0200 Subject: [PATCH] no rpc logging, add default config, better naming, code cleanup --- Cargo.lock | 2 +- docs/api.md | 6 ++-- ffplayout-api/src/utils/channels.rs | 48 +++++++++++------------------ ffplayout-api/src/utils/handles.rs | 23 +++++++------- ffplayout-api/src/utils/models.rs | 2 +- ffplayout-api/src/utils/playlist.rs | 4 +-- ffplayout-api/src/utils/routes.rs | 6 ++-- ffplayout-engine/Cargo.toml | 2 ++ lib/Cargo.toml | 2 +- lib/src/utils/logging.rs | 1 + 10 files changed, 44 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1c64ed6d..f3b08ed6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1056,7 +1056,7 @@ dependencies = [ [[package]] name = "ffplayout-lib" -version = "0.10.4" +version = "0.10.5" dependencies = [ "chrono 0.4.20-beta.1", "crossbeam-channel 0.5.5", diff --git a/docs/api.md b/docs/api.md index fe7dc596..0c9d32de 100644 --- a/docs/api.md +++ b/docs/api.md @@ -66,7 +66,7 @@ curl -X GET http://127.0.0.1:8000/api/channel/1 -H "Authorization: Bearer ```BASH curl -X PATCH http://127.0.0.1:8000/api/channel/1 -H "Content-Type: application/json" \ --d '{ "id": 1, "channel_name": "Channel 1", "preview_url": "http://localhost/live/stream.m3u8", \ +-d '{ "id": 1, "name": "Channel 1", "preview_url": "http://localhost/live/stream.m3u8", \ "config_path": "/etc/ffplayout/ffplayout.yml", "extra_extensions": "jpg,jpeg,png", "timezone": "Europe/Berlin"}' \ -H "Authorization: Bearer " ``` @@ -94,7 +94,7 @@ curl -X PATCH http://127.0.0.1:8000/api/channel/1 -H "Content-Type: application/ ```BASH curl -X POST http://127.0.0.1:8000/api/channel/ -H "Content-Type: application/json" \ --d '{ "channel_name": "Channel 2", "preview_url": "http://localhost/live/channel2.m3u8", \ +-d '{ "name": "Channel 2", "preview_url": "http://localhost/live/channel2.m3u8", \ "config_path": "/etc/ffplayout/channel2.yml", "extra_extensions": "jpg,jpeg,png", "timezone": "Europe/Berlin", "service": "ffplayout@channel2.service" }' \ -H "Authorization: Bearer " diff --git a/ffplayout-api/src/utils/channels.rs b/ffplayout-api/src/utils/channels.rs index 6492f27d..9c6c145b 100644 --- a/ffplayout-api/src/utils/channels.rs +++ b/ffplayout-api/src/utils/channels.rs @@ -18,39 +18,27 @@ pub async fn create_channel(target_channel: Channel) -> Result { - if let Err(e) = control_service(c.id, "enable").await { - return Err(e); - } - return Ok(c); - } - Err(e) => { - return Err(ServiceError::Conflict(e.to_string())); - } - }; - } - } + fs::copy( + "/usr/share/ffplayout/ffplayout.yml.orig", + &target_channel.config_path, + )?; - Err(ServiceError::InternalServerError) + let new_channel = db_add_channel(target_channel).await?; + control_service(new_channel.id, "enable").await?; + + Ok(new_channel) } pub async fn delete_channel(id: i64) -> Result<(), ServiceError> { - if let Ok(channel) = db_get_channel(&id).await { - if control_service(channel.id, "stop").await.is_ok() - && control_service(channel.id, "disable").await.is_ok() - { - if let Err(e) = fs::remove_file(channel.config_path) { - error!("{e}"); - }; - match db_delete_channel(&id).await { - Ok(_) => return Ok(()), - Err(e) => return Err(ServiceError::Conflict(e.to_string())), - } - } - } + let channel = db_get_channel(&id).await?; + control_service(channel.id, "stop").await?; + control_service(channel.id, "disable").await?; - Err(ServiceError::InternalServerError) + if let Err(e) = fs::remove_file(channel.config_path) { + error!("{e}"); + }; + + db_delete_channel(&id).await?; + + Ok(()) } diff --git a/ffplayout-api/src/utils/handles.rs b/ffplayout-api/src/utils/handles.rs index 76ac0841..6ab132e0 100644 --- a/ffplayout-api/src/utils/handles.rs +++ b/ffplayout-api/src/utils/handles.rs @@ -36,13 +36,13 @@ async fn create_schema() -> Result { CREATE TABLE IF NOT EXISTS channels ( id INTEGER PRIMARY KEY AUTOINCREMENT, - channel_name TEXT NOT NULL, + name TEXT NOT NULL, preview_url TEXT NOT NULL, config_path TEXT NOT NULL, extra_extensions TEXT NOT NULL, timezone TEXT NOT NULL, service TEXT NOT NULL, - UNIQUE(channel_name, service) + UNIQUE(name, service) ); CREATE TABLE IF NOT EXISTS presets ( @@ -111,7 +111,7 @@ pub async fn db_init(domain: Option) -> Result<&'static str, Box Result { pub async fn db_get_all_channels() -> Result, sqlx::Error> { let conn = db_connection().await?; - let query = "SELECT * FROM settings"; + let query = "SELECT * FROM channels"; let result: Vec = sqlx::query_as(query).fetch_all(&conn).await?; conn.close().await; @@ -171,13 +171,14 @@ pub async fn db_update_channel( ) -> Result { let conn = db_connection().await?; - let query = "UPDATE channels SET channel_name = $2, preview_url = $3, config_path = $4, extra_extensions = $5 WHERE id = $1"; + let query = "UPDATE channels SET name = $2, preview_url = $3, config_path = $4, extra_extensions = $5, timezone = $6 WHERE id = $1"; let result: SqliteQueryResult = sqlx::query(query) .bind(id) - .bind(channel.channel_name.clone()) - .bind(channel.preview_url.clone()) - .bind(channel.config_path.clone()) - .bind(channel.extra_extensions.clone()) + .bind(channel.name) + .bind(channel.preview_url) + .bind(channel.config_path) + .bind(channel.extra_extensions) + .bind(channel.timezone) .execute(&conn) .await?; conn.close().await; @@ -188,9 +189,9 @@ pub async fn db_update_channel( pub async fn db_add_channel(channel: Channel) -> Result { let conn = db_connection().await?; - let query = "INSERT INTO channels (channel_name, preview_url, config_path, extra_extensions, timezone, service) VALUES($1, $2, $3, $4, $5, $6)"; + let query = "INSERT INTO channels (name, preview_url, config_path, extra_extensions, timezone, service) VALUES($1, $2, $3, $4, $5, $6)"; let result = sqlx::query(query) - .bind(channel.channel_name) + .bind(channel.name) .bind(channel.preview_url) .bind(channel.config_path) .bind(channel.extra_extensions) diff --git a/ffplayout-api/src/utils/models.rs b/ffplayout-api/src/utils/models.rs index 9dae435f..0e8fdd1d 100644 --- a/ffplayout-api/src/utils/models.rs +++ b/ffplayout-api/src/utils/models.rs @@ -62,7 +62,7 @@ pub struct TextPreset { pub struct Channel { #[serde(skip_deserializing)] pub id: i64, - pub channel_name: String, + pub name: String, pub preview_url: String, pub config_path: String, pub extra_extensions: String, diff --git a/ffplayout-api/src/utils/playlist.rs b/ffplayout-api/src/utils/playlist.rs index 11022044..f917a4e8 100644 --- a/ffplayout-api/src/utils/playlist.rs +++ b/ffplayout-api/src/utils/playlist.rs @@ -75,10 +75,10 @@ pub async fn write_playlist(id: i64, json_data: JsonPlaylist) -> Result Result { - let (mut config, settings) = playout_config(&id).await?; + let (mut config, channel) = playout_config(&id).await?; config.general.generate = Some(vec![date.clone()]); - match playlist_generator(&config, Some(settings.channel_name)) { + match playlist_generator(&config, Some(channel.name)) { Ok(playlists) => { if !playlists.is_empty() { Ok(playlists[0].clone()) diff --git a/ffplayout-api/src/utils/routes.rs b/ffplayout-api/src/utils/routes.rs index 43dddcd0..6f96ebd7 100644 --- a/ffplayout-api/src/utils/routes.rs +++ b/ffplayout-api/src/utils/routes.rs @@ -231,7 +231,7 @@ async fn add_user(data: web::Json) -> Result /// ```JSON /// { /// "id": 1, -/// "channel_name": "Channel 1", +/// "name": "Channel 1", /// "preview_url": "http://localhost/live/preview.m3u8", /// "config_path": "/etc/ffplayout/ffplayout.yml", /// "extra_extensions": "jpg,jpeg,png", @@ -268,7 +268,7 @@ async fn get_all_channels() -> Result { /// /// ```BASH /// curl -X PATCH http://127.0.0.1:8000/api/channel/1 -H "Content-Type: application/json" \ -/// -d '{ "id": 1, "channel_name": "Channel 1", "preview_url": "http://localhost/live/stream.m3u8", \ +/// -d '{ "id": 1, "name": "Channel 1", "preview_url": "http://localhost/live/stream.m3u8", \ /// "config_path": "/etc/ffplayout/ffplayout.yml", "extra_extensions": "jpg,jpeg,png", "timezone": "Europe/Berlin"}' \ /// -H "Authorization: Bearer " /// ``` @@ -289,7 +289,7 @@ async fn patch_channel( /// /// ```BASH /// curl -X POST http://127.0.0.1:8000/api/channel/ -H "Content-Type: application/json" \ -/// -d '{ "channel_name": "Channel 2", "preview_url": "http://localhost/live/channel2.m3u8", \ +/// -d '{ "name": "Channel 2", "preview_url": "http://localhost/live/channel2.m3u8", \ /// "config_path": "/etc/ffplayout/channel2.yml", "extra_extensions": "jpg,jpeg,png", /// "timezone": "Europe/Berlin", "service": "ffplayout@channel2.service" }' \ /// -H "Authorization: Bearer " diff --git a/ffplayout-engine/Cargo.toml b/ffplayout-engine/Cargo.toml index 4e39ca7a..2784399e 100644 --- a/ffplayout-engine/Cargo.toml +++ b/ffplayout-engine/Cargo.toml @@ -52,6 +52,7 @@ assets = [ ["../assets/11-ffplayout", "/etc/sudoers.d/", "644"], ["../assets/ffplayout.yml", "/etc/ffplayout/", "644"], ["../assets/logo.png", "/usr/share/ffplayout/", "644"], + ["../assets/ffplayout.yml", "/usr/share/ffplayout/ffplayout.yml.orig", "644"], ["../README.md", "/usr/share/doc/ffplayout/README", "644"], ] maintainer-scripts = "../debian/" @@ -72,6 +73,7 @@ assets = [ { source = "../README.md", dest = "/usr/share/doc/ffplayout/README", mode = "644", doc = true }, { source = "../LICENSE", dest = "/usr/share/doc/ffplayout/LICENSE", mode = "644" }, { source = "../assets/logo.png", dest = "/usr/share/ffplayout/logo.png", mode = "644" }, + { source = "../assets/ffplayout.yml", dest = "/usr/share/ffplayout/ffplayout.yml.orig", mode = "644" }, { source = "../debian/postinst", dest = "/usr/share/ffplayout/postinst", mode = "755" }, ] auto-req = "no" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 13353080..ea65a76a 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -4,7 +4,7 @@ description = "Library for ffplayout" license = "GPL-3.0" authors = ["Jonathan Baecker jonbae77@gmail.com"] readme = "README.md" -version = "0.10.4" +version = "0.10.5" edition = "2021" [dependencies] diff --git a/lib/src/utils/logging.rs b/lib/src/utils/logging.rs index b2f74f60..466e85e4 100644 --- a/lib/src/utils/logging.rs +++ b/lib/src/utils/logging.rs @@ -188,6 +188,7 @@ pub fn init_logging( .add_filter_ignore_str("hyper") .add_filter_ignore_str("sqlx") .add_filter_ignore_str("reqwest") + .add_filter_ignore_str("rpc") .set_level_padding(LevelPadding::Left) .set_time_level(time_level) .clone();