From e226418ed8b210be762e1c0f1661a5f20df07850 Mon Sep 17 00:00:00 2001 From: greysoh Date: Sun, 28 Apr 2024 19:33:36 -0400 Subject: [PATCH] feature: Adds preliminary API support. Co-authored-by: dess --- api/src/routes/forward/create.ts | 1 - gui/Cargo.toml | 5 ++- gui/src/api.rs | 55 ++++++++++++++++++++++++++++++++ gui/src/main.rs | 54 ++++++++++++++++++++++++------- 4 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 gui/src/api.rs diff --git a/api/src/routes/forward/create.ts b/api/src/routes/forward/create.ts index 22b38e5..27864d2 100644 --- a/api/src/routes/forward/create.ts +++ b/api/src/routes/forward/create.ts @@ -94,7 +94,6 @@ export function route(routeOptions: RouteOptions) { sourcePort: body.sourcePort, destPort: body.destinationPort, - destProviderID: body.providerID, enabled: Boolean(body.autoStart) diff --git a/gui/Cargo.toml b/gui/Cargo.toml index c0af2f4..fecdde1 100644 --- a/gui/Cargo.toml +++ b/gui/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gui" +name = "nextnet-gui" version = "0.1.0" edition = "2021" @@ -8,3 +8,6 @@ edition = "2021" [dependencies] eframe = "0.27.2" egui = "0.27.2" +ehttp = { version = "0.5.0", features = ["json"] } +serde = "1.0.199" +serde_json = "1.0.116" diff --git a/gui/src/api.rs b/gui/src/api.rs new file mode 100644 index 0000000..b983df6 --- /dev/null +++ b/gui/src/api.rs @@ -0,0 +1,55 @@ +use ehttp::{fetch, Request}; +use serde::Deserialize; +use serde_json::json; + +pub struct NextAPIClient { + pub url: String +} + +#[derive(Deserialize, Debug, Default)] +pub struct LoginResponse { + pub error: Option, + pub token: Option +} + +#[allow(non_snake_case)] +pub struct Backend { + pub id: Option, + pub backend: Option, + pub name: Option, + pub description: Option, + pub connectionDetails: Option, + + pub logs: Option> +} + +impl NextAPIClient { + pub fn login(&self, email: &str, password: &str, mut callback: impl 'static + Send + FnMut(LoginResponse)) { + let json_data = json!({ + "email": email, + "password": password + }); + + let json_str_raw: String = json_data.to_string(); + let json_str: &str = json_str_raw.as_str(); + + println!("{}", json_str); + + let mut request = Request::post(self.url.clone() + "/api/v1/users/login", json_str.as_bytes().to_vec()); + request.headers.insert("Content-Type", "application/json"); + + fetch(request, move |result: ehttp::Result| { + let res = result.unwrap(); + let json: LoginResponse = res.json().unwrap(); + callback(json); + }); + } +} + +pub fn new(url: String) -> NextAPIClient { + let api_client: NextAPIClient = NextAPIClient { + url + }; + + return api_client; +} \ No newline at end of file diff --git a/gui/src/main.rs b/gui/src/main.rs index 647d29f..f987748 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -1,28 +1,60 @@ +use std::sync::{Arc, Mutex}; use eframe::egui; +mod api; + fn main() -> Result<(), eframe::Error> { + let api = api::new("http://localhost:3000".to_string()); + let options = eframe::NativeOptions { viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]), ..Default::default() }; // Our application state: - let mut name = "Arthur".to_owned(); - let mut age = 42; + let mut username: String = "replace@gmail.com".to_owned(); + let mut password: String = "replace123".to_owned(); - eframe::run_simple_native("My egui App", options, move |ctx, _frame| { + let token: Arc> = Arc::new(Mutex::new("".to_string())); + + eframe::run_simple_native("NextNet GUI", options, move |ctx, _frame| { egui::CentralPanel::default().show(ctx, |ui| { - ui.heading("My egui Application"); + ui.heading("Login"); ui.horizontal(|ui| { - let name_label = ui.label("Your name: "); - ui.text_edit_singleline(&mut name) - .labelled_by(name_label.id); + ui.label("Email: "); + ui.text_edit_singleline(&mut username); }); - ui.add(egui::Slider::new(&mut age, 0..=120).text("age")); - if ui.button("Increment").clicked() { - age += 1; + + ui.horizontal(|ui| { + let label = ui.label("Password: "); + ui.add(egui::TextEdit::singleline(&mut password).password(true)) + .labelled_by(label.id); + }); + + if ui.button("Login").clicked() { + let token_clone = Arc::clone(&token); + api.login(username.as_str(), password.as_str(), Box::new(move |res: api::LoginResponse| { + match res.token { + Some(x) => { + let mut token = token_clone.lock().unwrap(); + *token = x; + }, + None => { + let mut token = token_clone.lock().unwrap(); + *token = "".to_string(); + } + } + })); + } + + match token.lock() { + Ok(x) => { + ui.label(format!("Token: {:?}", *x)); + }, + Err(_) => { + ui.label(format!("No token.")); + } } - ui.label(format!("Hello '{name}', age {age}")); }); }) } \ No newline at end of file