diff --git a/ffplayout-api/src/utils/routes.rs b/ffplayout-api/src/utils/routes.rs index 47088d21..de98af8d 100644 --- a/ffplayout-api/src/utils/routes.rs +++ b/ffplayout-api/src/utils/routes.rs @@ -32,176 +32,6 @@ struct ResponseObj { data: Option, } -/// curl -X GET http://127.0.0.1:8080/api/settings/1 -H "Authorization: Bearer " -#[get("/settings/{id}")] -#[has_any_role("Role::Admin", "Role::User", type = "Role")] -async fn get_settings(id: web::Path) -> Result { - if let Ok(settings) = db_get_settings(&id).await { - return Ok(web::Json(ResponseObj { - message: format!("Settings from {}", settings.channel_name), - status: 200, - data: Some(settings), - })); - } - - Err(ServiceError::InternalServerError) -} - -/// curl -X PATCH http://127.0.0.1:8080/api/settings/1 -H "Content-Type: application/json" \ -/// --data '{"id":1,"channel_name":"Channel 1","preview_url":"http://localhost/live/stream.m3u8", \ -/// "config_path":"/etc/ffplayout/ffplayout.yml","extra_extensions":".jpg,.jpeg,.png"}' \ -/// -H "Authorization: Bearer " -#[patch("/settings/{id}")] -#[has_any_role("Role::Admin", type = "Role")] -async fn patch_settings( - id: web::Path, - data: web::Json, -) -> Result { - if db_update_settings(*id, data.into_inner()).await.is_ok() { - return Ok("Update Success"); - }; - - Err(ServiceError::InternalServerError) -} - -/// curl -X GET http://localhost:8080/api/playout/config/1 --header 'Authorization: ' -#[get("/playout/config/{id}")] -#[has_any_role("Role::Admin", "Role::User", type = "Role")] -async fn get_playout_config( - id: web::Path, - _details: AuthDetails, -) -> Result { - if let Ok(settings) = db_get_settings(&id).await { - if let Ok(config) = read_playout_config(&settings.config_path) { - return Ok(web::Json(config)); - } - }; - - Err(ServiceError::InternalServerError) -} - -/// curl -X PUT http://localhost:8080/api/playout/config/1 -H "Content-Type: application/json" \ -/// --data { } --header 'Authorization: ' -#[put("/playout/config/{id}")] -#[has_any_role("Role::Admin", type = "Role")] -async fn update_playout_config( - id: web::Path, - data: web::Json, -) -> Result { - if let Ok(settings) = db_get_settings(&id).await { - if let Ok(f) = std::fs::OpenOptions::new() - .write(true) - .truncate(true) - .open(&settings.config_path) - { - serde_yaml::to_writer(f, &data).unwrap(); - - return Ok("Update playout config success."); - } else { - return Err(ServiceError::InternalServerError); - }; - }; - - Err(ServiceError::InternalServerError) -} - -/// curl -X PUT http://localhost:8080/api/presets/ --header 'Content-Type: application/json' \ -/// --data '{"email": "", "password": ""}' --header 'Authorization: ' -#[get("/presets/")] -#[has_any_role("Role::Admin", "Role::User", type = "Role")] -async fn get_presets() -> Result { - if let Ok(presets) = db_get_presets().await { - return Ok(web::Json(presets)); - } - - Err(ServiceError::InternalServerError) -} - -/// curl -X PUT http://localhost:8080/api/presets/1 --header 'Content-Type: application/json' \ -/// --data '{"name": "", "text": "TEXT>", "x": "", "y": "", "fontsize": 24, \ -/// "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0}}' \ -/// --header 'Authorization: ' -#[put("/presets/{id}")] -#[has_any_role("Role::Admin", "Role::User", type = "Role")] -async fn update_preset( - id: web::Path, - data: web::Json, -) -> Result { - if db_update_preset(&id, data.into_inner()).await.is_ok() { - return Ok("Update Success"); - } - - Err(ServiceError::InternalServerError) -} - -/// curl -X POST http://localhost:8080/api/presets/ --header 'Content-Type: application/json' \ -/// --data '{"name": "", "text": "TEXT>", "x": "", "y": "", "fontsize": 24, \ -/// "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0}}' \ -/// --header 'Authorization: ' -#[post("/presets/")] -#[has_any_role("Role::Admin", "Role::User", type = "Role")] -async fn add_preset(data: web::Json) -> Result { - if db_add_preset(data.into_inner()).await.is_ok() { - return Ok("Add preset Success"); - } - - Err(ServiceError::InternalServerError) -} - -/// curl -X PUT http://localhost:8080/api/user/1 --header 'Content-Type: application/json' \ -/// --data '{"email": "", "password": ""}' --header 'Authorization: ' -#[put("/user/{id}")] -#[has_any_role("Role::Admin", "Role::User", type = "Role")] -async fn update_user( - id: web::Path, - user: web::ReqData, - data: web::Json, -) -> Result { - if id.into_inner() == user.id { - let mut fields = String::new(); - - if let Some(email) = data.email.clone() { - fields.push_str(format!("email = '{email}'").as_str()); - } - - if !data.password.is_empty() { - if !fields.is_empty() { - fields.push_str(", "); - } - - let salt = SaltString::generate(&mut OsRng); - let password_hash = Argon2::default() - .hash_password(data.password.clone().as_bytes(), &salt) - .unwrap(); - - fields.push_str(format!("password = '{}', salt = '{salt}'", password_hash).as_str()); - } - - if db_update_user(user.id, fields).await.is_ok() { - return Ok("Update Success"); - }; - - return Err(ServiceError::InternalServerError); - } - - Err(ServiceError::Unauthorized) -} - -/// curl -X POST 'http://localhost:8080/api/user/' --header 'Content-Type: application/json' \ -/// -d '{"email": "", "username": "", "password": "", "role_id": 1}' \ -/// --header 'Authorization: Bearer ' -#[post("/user/")] -#[has_any_role("Role::Admin", type = "Role")] -async fn add_user(data: web::Json) -> Result { - match db_add_user(data.into_inner()).await { - Ok(_) => Ok("Add User Success"), - Err(e) => { - error!("{e}"); - Err(ServiceError::InternalServerError) - } - } -} - /// curl -X POST http://127.0.0.1:8080/auth/login/ -H "Content-Type: application/json" \ /// -d '{"username": "", "password": "" }' #[post("/auth/login/")] @@ -259,6 +89,176 @@ pub async fn login(credentials: web::Json) -> impl Responder { } } +/// curl -X PUT http://localhost:8080/api/user/1 --header 'Content-Type: application/json' \ +/// --data '{"email": "", "password": ""}' --header 'Authorization: ' +#[put("/user/{id}")] +#[has_any_role("Role::Admin", "Role::User", type = "Role")] +async fn update_user( + id: web::Path, + user: web::ReqData, + data: web::Json, +) -> Result { + if id.into_inner() == user.id { + let mut fields = String::new(); + + if let Some(email) = data.email.clone() { + fields.push_str(format!("email = '{email}'").as_str()); + } + + if !data.password.is_empty() { + if !fields.is_empty() { + fields.push_str(", "); + } + + let salt = SaltString::generate(&mut OsRng); + let password_hash = Argon2::default() + .hash_password(data.password.clone().as_bytes(), &salt) + .unwrap(); + + fields.push_str(format!("password = '{}', salt = '{salt}'", password_hash).as_str()); + } + + if db_update_user(user.id, fields).await.is_ok() { + return Ok("Update Success"); + }; + + return Err(ServiceError::InternalServerError); + } + + Err(ServiceError::Unauthorized) +} + +/// curl -X POST 'http://localhost:8080/api/user/' --header 'Content-Type: application/json' \ +/// -d '{"email": "", "username": "", "password": "", "role_id": 1}' \ +/// --header 'Authorization: Bearer ' +#[post("/user/")] +#[has_any_role("Role::Admin", type = "Role")] +async fn add_user(data: web::Json) -> Result { + match db_add_user(data.into_inner()).await { + Ok(_) => Ok("Add User Success"), + Err(e) => { + error!("{e}"); + Err(ServiceError::InternalServerError) + } + } +} + +/// curl -X GET http://127.0.0.1:8080/api/settings/1 -H "Authorization: Bearer " +#[get("/settings/{id}")] +#[has_any_role("Role::Admin", "Role::User", type = "Role")] +async fn get_settings(id: web::Path) -> Result { + if let Ok(settings) = db_get_settings(&id).await { + return Ok(web::Json(ResponseObj { + message: format!("Settings from {}", settings.channel_name), + status: 200, + data: Some(settings), + })); + } + + Err(ServiceError::InternalServerError) +} + +/// curl -X PATCH http://127.0.0.1:8080/api/settings/1 -H "Content-Type: application/json" \ +/// --data '{"id":1,"channel_name":"Channel 1","preview_url":"http://localhost/live/stream.m3u8", \ +/// "config_path":"/etc/ffplayout/ffplayout.yml","extra_extensions":".jpg,.jpeg,.png"}' \ +/// -H "Authorization: Bearer " +#[patch("/settings/{id}")] +#[has_any_role("Role::Admin", type = "Role")] +async fn patch_settings( + id: web::Path, + data: web::Json, +) -> Result { + if db_update_settings(*id, data.into_inner()).await.is_ok() { + return Ok("Update Success"); + }; + + Err(ServiceError::InternalServerError) +} + +/// curl -X GET http://localhost:8080/api/playout/config/1 --header 'Authorization: ' +#[get("/playout/config/{id}")] +#[has_any_role("Role::Admin", "Role::User", type = "Role")] +async fn get_playout_config( + id: web::Path, + _details: AuthDetails, +) -> Result { + if let Ok(settings) = db_get_settings(&id).await { + if let Ok(config) = read_playout_config(&settings.config_path) { + return Ok(web::Json(config)); + } + }; + + Err(ServiceError::InternalServerError) +} + +/// curl -X PUT http://localhost:8080/api/playout/config/1 -H "Content-Type: application/json" \ +/// --data { } --header 'Authorization: ' +#[put("/playout/config/{id}")] +#[has_any_role("Role::Admin", type = "Role")] +async fn update_playout_config( + id: web::Path, + data: web::Json, +) -> Result { + if let Ok(settings) = db_get_settings(&id).await { + if let Ok(f) = std::fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(&settings.config_path) + { + serde_yaml::to_writer(f, &data).unwrap(); + + return Ok("Update playout config success."); + } else { + return Err(ServiceError::InternalServerError); + }; + }; + + Err(ServiceError::InternalServerError) +} + +/// curl -X GET http://localhost:8080/api/presets/ --header 'Content-Type: application/json' \ +/// --data '{"email": "", "password": ""}' --header 'Authorization: ' +#[get("/presets/")] +#[has_any_role("Role::Admin", "Role::User", type = "Role")] +async fn get_presets() -> Result { + if let Ok(presets) = db_get_presets().await { + return Ok(web::Json(presets)); + } + + Err(ServiceError::InternalServerError) +} + +/// curl -X PUT http://localhost:8080/api/presets/1 --header 'Content-Type: application/json' \ +/// --data '{"name": "", "text": "", "x": "", "y": "", "fontsize": 24, \ +/// "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0}' \ +/// --header 'Authorization: ' +#[put("/presets/{id}")] +#[has_any_role("Role::Admin", "Role::User", type = "Role")] +async fn update_preset( + id: web::Path, + data: web::Json, +) -> Result { + if db_update_preset(&id, data.into_inner()).await.is_ok() { + return Ok("Update Success"); + } + + Err(ServiceError::InternalServerError) +} + +/// curl -X POST http://localhost:8080/api/presets/ --header 'Content-Type: application/json' \ +/// --data '{"name": "", "text": "TEXT>", "x": "", "y": "", "fontsize": 24, \ +/// "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0}}' \ +/// --header 'Authorization: ' +#[post("/presets/")] +#[has_any_role("Role::Admin", "Role::User", type = "Role")] +async fn add_preset(data: web::Json) -> Result { + if db_add_preset(data.into_inner()).await.is_ok() { + return Ok("Add preset Success"); + } + + Err(ServiceError::InternalServerError) +} + /// ---------------------------------------------------------------------------- /// ffplayout process controlling /// @@ -447,7 +447,7 @@ pub async fn move_rename( /// curl -X DELETE http://localhost:8080/api/file/1/remove/ /// --header 'Content-Type: application/json' --header 'Authorization: ' -/// -d '{"source": "", "target": ""}' +/// -d '{"source": ""}' #[delete("/file/{id}/remove/")] #[has_any_role("Role::Admin", "Role::User", type = "Role")] pub async fn remove(