diff --git a/angular-client/src/api/rules.api.ts b/angular-client/src/api/rules.api.ts index c9914a21..be8aae4c 100644 --- a/angular-client/src/api/rules.api.ts +++ b/angular-client/src/api/rules.api.ts @@ -21,27 +21,21 @@ export interface RulesResponse { client_rules: ClientRule[]; } -const basicAuthHeader = (clientId: string): string => 'Basic ' + btoa(`${clientId}:`); - export const getRulesByClientId = (clientId: string): Promise => { return fetch(urls.getRulesByClientId(clientId)); }; export const addRule = (clientId: string, rule: RulePayload): Promise => { - return fetch(urls.addRule(), { + return fetch(urls.addRule(clientId), { method: 'PUT', - headers: { - 'Content-Type': 'application/json', - Authorization: basicAuthHeader(clientId) - }, + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(rule) }); }; export const deleteRule = (clientId: string, ruleId: string): Promise => { - return fetch(urls.deleteRule(ruleId), { - method: 'POST', - headers: { Authorization: basicAuthHeader(clientId) } + return fetch(urls.deleteRule(clientId, ruleId), { + method: 'POST' }); }; @@ -53,23 +47,18 @@ export const editRule = (ruleId: string, rule: object): Promise => { }); }; -export interface RuleSubscriptionRequest { - rule_ids: string[]; - client_id: string; -} - -export const subscribeToRules = (request: RuleSubscriptionRequest): Promise => { - return fetch(urls.subscribeToRule(), { +export const subscribeToRules = (clientId: string, ruleIds: string[]): Promise => { + return fetch(urls.subscribeToRule(clientId), { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(request) + body: JSON.stringify(ruleIds) }); }; -export const unsubscribeFromRules = (request: RuleSubscriptionRequest): Promise => { - return fetch(urls.unsubscribeFromRule(), { +export const unsubscribeFromRules = (clientId: string, ruleIds: string[]): Promise => { + return fetch(urls.unsubscribeFromRule(clientId), { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(request) + body: JSON.stringify(ruleIds) }); }; diff --git a/angular-client/src/api/urls.ts b/angular-client/src/api/urls.ts index cdff4943..1a43b8ae 100644 --- a/angular-client/src/api/urls.ts +++ b/angular-client/src/api/urls.ts @@ -37,11 +37,11 @@ const carCommandConfig = (key: string, values: number[]) => /* Rules */ const getRulesByClientId = (clientId: string) => `${baseURL}/rules/${clientId}`; -const addRule = () => `${baseURL}/rules/add`; -const deleteRule = (ruleId: string) => `${baseURL}/rules/delete/${ruleId}`; +const addRule = (clientId: string) => `${baseURL}/rules/${clientId}/add`; +const deleteRule = (clientId: string, ruleId: string) => `${baseURL}/rules/${clientId}/delete/${ruleId}`; const editRule = (ruleId: string) => `${baseURL}/rules/edit/${ruleId}`; -const subscribeToRule = () => `${baseURL}/rules/subscribe`; -const unsubscribeFromRule = () => `${baseURL}/rules/unsubscribe`; +const subscribeToRule = (clientId: string) => `${baseURL}/rules/${clientId}/subscribe`; +const unsubscribeFromRule = (clientId: string) => `${baseURL}/rules/${clientId}/unsubscribe`; /* Authentication */ const authenticate = () => `${baseURL}/authenticate`; diff --git a/angular-client/src/pages/notification-rules-page/notification-rules-page.component.ts b/angular-client/src/pages/notification-rules-page/notification-rules-page.component.ts index 78845b8a..84830bf6 100644 --- a/angular-client/src/pages/notification-rules-page/notification-rules-page.component.ts +++ b/angular-client/src/pages/notification-rules-page/notification-rules-page.component.ts @@ -142,10 +142,10 @@ export default class NotificationRulesPageComponent implements OnInit { try { const promises: Promise[] = []; if (toSubscribe.length > 0) { - promises.push(subscribeToRules({ rule_ids: toSubscribe, client_id: this.clientId })); + promises.push(subscribeToRules(this.clientId, toSubscribe)); } if (toUnsubscribe.length > 0) { - promises.push(unsubscribeFromRules({ rule_ids: toUnsubscribe, client_id: this.clientId })); + promises.push(unsubscribeFromRules(this.clientId, toUnsubscribe)); } const results = await Promise.all(promises); diff --git a/angular-client/src/pages/notification-rules-page/rules-table/rules-table.component.ts b/angular-client/src/pages/notification-rules-page/rules-table/rules-table.component.ts index a4a7ec7c..5c542591 100644 --- a/angular-client/src/pages/notification-rules-page/rules-table/rules-table.component.ts +++ b/angular-client/src/pages/notification-rules-page/rules-table/rules-table.component.ts @@ -73,9 +73,11 @@ export class RulesTableComponent implements OnInit { } async onToggleSubscription(rule: ClientRule, subscribed: boolean): Promise { - const request = { rule_ids: [rule.id], client_id: this.clientId() }; + const ruleIds = [rule.id]; try { - const response = subscribed ? await subscribeToRules(request) : await unsubscribeFromRules(request); + const response = subscribed + ? await subscribeToRules(this.clientId(), ruleIds) + : await unsubscribeFromRules(this.clientId(), ruleIds); if (response.ok) { this.rules.update((rules) => rules.map((r) => (r.id === rule.id ? { ...r, is_subscribed: subscribed } : r))); } else { diff --git a/scylla-server/Cargo.lock b/scylla-server/Cargo.lock index 83eaf087..45e87cbb 100644 --- a/scylla-server/Cargo.lock +++ b/scylla-server/Cargo.lock @@ -202,7 +202,6 @@ dependencies = [ "form_urlencoded", "futures-core", "futures-util", - "headers", "http", "http-body", "http-body-util", @@ -1035,30 +1034,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "headers" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3314d5adb5d94bcdf56771f2e50dbbc80bb4bdf88967526706205ac9eff24eb" -dependencies = [ - "base64 0.22.1", - "bytes", - "headers-core", - "http", - "httpdate", - "mime", - "sha1", -] - -[[package]] -name = "headers-core" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54b4a22553d4242c49fddb9ba998a99962b5cc6f22cb5a3482bec22522403ce4" -dependencies = [ - "http", -] - [[package]] name = "heck" version = "0.5.0" diff --git a/scylla-server/Cargo.toml b/scylla-server/Cargo.toml index 4ca7df11..d575e554 100644 --- a/scylla-server/Cargo.toml +++ b/scylla-server/Cargo.toml @@ -22,7 +22,7 @@ tracing = "0.1.43" tracing-subscriber = { version = "0.3.23", features = ["ansi", "env-filter"] } console-subscriber = { version = "0.5.0", optional = true } ringbuffer = "0.16.0" -axum-extra = { version = "0.12.5", features = ["query", "typed-header"] } +axum-extra = { version = "0.12.5", features = ["query"] } clap = { version = "4.6.0", features = ["derive", "env"] } chrono = { version = "0.4.43", features = ["serde"] } serde_json = "1.0.149" diff --git a/scylla-server/src/controllers/rule_controller.rs b/scylla-server/src/controllers/rule_controller.rs index 3f2f365a..bef736ae 100644 --- a/scylla-server/src/controllers/rule_controller.rs +++ b/scylla-server/src/controllers/rule_controller.rs @@ -1,10 +1,6 @@ use std::sync::Arc; use axum::{Extension, Json, debug_handler, extract::Path}; -use axum_extra::{ - TypedHeader, - headers::{Authorization, authorization::Basic}, -}; use serde::Deserialize; use serde_with::DurationSeconds; use serde_with::serde_as; @@ -16,27 +12,14 @@ use crate::{ rule_structs::{ClientId, Rule, RuleId, RuleManager, RulesResponse}, }; -#[derive(Deserialize)] -pub struct RuleSubscriptionRequest { - rule_ids: Vec, - client_id: String, -} - #[debug_handler] pub async fn add_rule( - TypedHeader(auth): TypedHeader>, + Path(client_id): Path, Extension(rules_manager): Extension>, Json(rule): Json, ) -> Result, ScyllaError> { - info!( - "Incoming rules reg: {}, from {}", - rule.topic, - auth.username().to_string() - ); - match rules_manager - .add_rule(ClientId(auth.username().to_string()), rule) - .await - { + info!("Incoming rules reg: {}, from {}", rule.topic, client_id); + match rules_manager.add_rule(ClientId(client_id), rule).await { Ok(_) => Ok(Json::from("Rule added!".to_owned())), Err(err) => Err(ScyllaError::RuleError(err)), } @@ -44,17 +27,12 @@ pub async fn add_rule( #[debug_handler] pub async fn delete_rule( - TypedHeader(auth): TypedHeader>, + Path((client_id, rule_id)): Path<(String, String)>, Extension(rules_manager): Extension>, - Path(rule_id): Path, ) -> Result<(), ScyllaError> { - info!( - "Incoming rules del: {}, from {}", - rule_id, - auth.username().to_string() - ); + info!("Incoming rules del: {}, from {}", rule_id, client_id); match rules_manager - .delete_rule(ClientId(auth.username().to_string()), RuleId(rule_id)) + .delete_rule(ClientId(client_id), RuleId(rule_id)) .await { Ok(_) => Ok(()), @@ -126,19 +104,20 @@ pub async fn edit_rule( #[debug_handler] pub async fn unsubscribe_rules( + Path(client_id): Path, Extension(rules_manager): Extension>, - Json(request): Json, + Json(rule_ids): Json>, ) -> Result, ScyllaError> { info!( "Unsubscribing client {} from {} rules", - request.client_id, - request.rule_ids.len() + client_id, + rule_ids.len() ); - let rule_ids: Vec = request.rule_ids.into_iter().map(RuleId).collect(); + let rule_ids: Vec = rule_ids.into_iter().map(RuleId).collect(); match rules_manager - .unsubscribe_rules(ClientId(request.client_id), rule_ids) + .unsubscribe_rules(ClientId(client_id), rule_ids) .await { Ok(_) => Ok(Json::from( @@ -150,19 +129,20 @@ pub async fn unsubscribe_rules( #[debug_handler] pub async fn subscribe_rules( + Path(client_id): Path, Extension(rules_manager): Extension>, - Json(request): Json, + Json(rule_ids): Json>, ) -> Result, ScyllaError> { info!( "Subscribing client {} to {} rules", - request.client_id, - request.rule_ids.len() + client_id, + rule_ids.len() ); - let rule_ids: Vec = request.rule_ids.into_iter().map(RuleId).collect(); + let rule_ids: Vec = rule_ids.into_iter().map(RuleId).collect(); match rules_manager - .subscribe_rules(ClientId(request.client_id), rule_ids) + .subscribe_rules(ClientId(client_id), rule_ids) .await { Ok(_) => Ok(Json::from("Successfully subscribed to rules".to_owned())), diff --git a/scylla-server/src/main.rs b/scylla-server/src/main.rs index 11eb7499..aa850951 100755 --- a/scylla-server/src/main.rs +++ b/scylla-server/src/main.rs @@ -440,18 +440,17 @@ async fn main() -> Result<(), Box> { ) .merge( Router::new() - .route("/rules/add", put(add_rule)) - .route("/rules/delete/{rule_id}", post(delete_rule)) .route("/rules", get(get_all_rules)) .route("/rules/{client_id}", get(get_all_rules_with_client_info)) + .route("/rules/{client_id}/add", put(add_rule)) + .route("/rules/{client_id}/delete/{rule_id}", post(delete_rule)) .route( - "/rules/subscribed/{client_id}", + "/rules/{client_id}/subscribed", get(get_client_subscribed_rules), ) - .route("/rules/unsubscribe", post(unsubscribe_rules)) + .route("/rules/{client_id}/subscribe", post(subscribe_rules)) + .route("/rules/{client_id}/unsubscribe", post(unsubscribe_rules)) .route("/rules/edit/{rule_id}", put(edit_rule)) - .route("/rules/subscribe", post(subscribe_rules)) - //.route("/rules/delete/{rule_id}", post()).route("/rules/poll") .layer(Extension(rules_manager)), ) // for CORS handling