feature: Adds preliminary API support.

Co-authored-by: dess <dess@kyoline.dev>
This commit is contained in:
greysoh 2024-04-28 19:33:36 -04:00
parent 6063a541d5
commit e226418ed8
No known key found for this signature in database
GPG key ID: FE0F173B8FC01571
4 changed files with 102 additions and 13 deletions

View file

@ -94,7 +94,6 @@ export function route(routeOptions: RouteOptions) {
sourcePort: body.sourcePort,
destPort: body.destinationPort,
destProviderID: body.providerID,
enabled: Boolean(body.autoStart)

View file

@ -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"

55
gui/src/api.rs Normal file
View file

@ -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<String>,
pub token: Option<String>
}
#[allow(non_snake_case)]
pub struct Backend {
pub id: Option<u32>,
pub backend: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
pub connectionDetails: Option<String>,
pub logs: Option<Vec<String>>
}
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<ehttp::Response>| {
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;
}

View file

@ -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<Mutex<String>> = 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}"));
});
})
}