Skip to content

Commit

Permalink
More changes based on CR
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed May 8, 2024
1 parent 97d655e commit 8330cea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions rust/agama-lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum ServiceError {
// specific error will be printed too
#[error("Error: {0}")]
Anyhow(#[from] anyhow::Error),
// FIXME: It is too generic and starting to looks like an Anyhow error
#[error("Network client error: '{0}'")]
NetworkClientError(String),
#[error("Wrong user parameters: '{0:?}'")]
Expand Down
16 changes: 8 additions & 8 deletions rust/agama-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ pub async fn connection() -> Result<zbus::Connection, ServiceError> {
connection_to(ADDRESS).await
}

pub async fn connection_to(address: &str) -> Result<zbus::Connection, ServiceError> {
let connection = zbus::ConnectionBuilder::address(address)?
.build()
.await
.map_err(|e| ServiceError::DBusConnectionError(address.to_string(), e))?;
Ok(connection)
}

pub fn http_client(token: String) -> Result<reqwest::Client, ServiceError> {
let mut headers = header::HeaderMap::new();
let value = header::HeaderValue::from_str(format!("Bearer {}", token).as_str())
Expand All @@ -63,11 +71,3 @@ pub fn http_client(token: String) -> Result<reqwest::Client, ServiceError> {

Ok(client)
}

pub async fn connection_to(address: &str) -> Result<zbus::Connection, ServiceError> {
let connection = zbus::ConnectionBuilder::address(address)?
.build()
.await
.map_err(|e| ServiceError::DBusConnectionError(address.to_string(), e))?;
Ok(connection)
}
9 changes: 3 additions & 6 deletions rust/agama-server/src/agama-web-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use tracing_subscriber::prelude::*;
use utoipa::OpenApi;

const DEFAULT_WEB_UI_DIR: &str = "/usr/share/agama/web_ui";
const TOKEN_FILE: &str = "/run/agama/token";

#[derive(Subcommand, Debug)]
enum Commands {
Expand Down Expand Up @@ -95,8 +96,6 @@ struct ServeArgs {
// Directory containing the web UI code.
#[arg(long)]
web_ui_dir: Option<PathBuf>,
#[arg(long)]
generate_token: Option<PathBuf>,
}

impl ServeArgs {
Expand Down Expand Up @@ -301,9 +300,7 @@ async fn serve_command(args: ServeArgs) -> anyhow::Result<()> {

let config = web::ServiceConfig::load()?;

if let Some(token_file) = args.generate_token.clone() {
write_token(&token_file, &config.jwt_secret).context("could not create the token file")?;
}
write_token(TOKEN_FILE, &config.jwt_secret).context("could not create the token file")?;

let dbus = connection_to(&args.dbus_address).await?;
let web_ui_dir = args.web_ui_dir.clone().unwrap_or(find_web_ui_dir());
Expand Down Expand Up @@ -351,7 +348,7 @@ async fn run_command(cli: Cli) -> anyhow::Result<()> {
}
}

fn write_token(path: &PathBuf, secret: &str) -> io::Result<()> {
fn write_token(path: &str, secret: &str) -> io::Result<()> {
let token = generate_token(secret);
let mut file = fs::OpenOptions::new()
.create(true)
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-server/src/network/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async fn devices(
}

#[utoipa::path(get, path = "/network/connections/:id", responses(
(status = 200, description = "Get connection given by its id", body = NetworkConnection)
(status = 200, description = "Get connection given by its ID", body = NetworkConnection)
))]
async fn connection(
State(state): State<NetworkServiceState>,
Expand Down

0 comments on commit 8330cea

Please sign in to comment.