feature: Adds preliminary API support.
Co-authored-by: dess <dess@kyoline.dev>
This commit is contained in:
parent
6063a541d5
commit
e226418ed8
4 changed files with 102 additions and 13 deletions
|
@ -94,7 +94,6 @@ export function route(routeOptions: RouteOptions) {
|
||||||
sourcePort: body.sourcePort,
|
sourcePort: body.sourcePort,
|
||||||
|
|
||||||
destPort: body.destinationPort,
|
destPort: body.destinationPort,
|
||||||
|
|
||||||
destProviderID: body.providerID,
|
destProviderID: body.providerID,
|
||||||
|
|
||||||
enabled: Boolean(body.autoStart)
|
enabled: Boolean(body.autoStart)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "gui"
|
name = "nextnet-gui"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
@ -8,3 +8,6 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
eframe = "0.27.2"
|
eframe = "0.27.2"
|
||||||
egui = "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
55
gui/src/api.rs
Normal 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;
|
||||||
|
}
|
|
@ -1,28 +1,60 @@
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
|
|
||||||
|
mod api;
|
||||||
|
|
||||||
fn main() -> Result<(), eframe::Error> {
|
fn main() -> Result<(), eframe::Error> {
|
||||||
|
let api = api::new("http://localhost:3000".to_string());
|
||||||
|
|
||||||
let options = eframe::NativeOptions {
|
let options = eframe::NativeOptions {
|
||||||
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
|
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Our application state:
|
// Our application state:
|
||||||
let mut name = "Arthur".to_owned();
|
let mut username: String = "replace@gmail.com".to_owned();
|
||||||
let mut age = 42;
|
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| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
ui.heading("My egui Application");
|
ui.heading("Login");
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
let name_label = ui.label("Your name: ");
|
ui.label("Email: ");
|
||||||
ui.text_edit_singleline(&mut name)
|
ui.text_edit_singleline(&mut username);
|
||||||
.labelled_by(name_label.id);
|
|
||||||
});
|
});
|
||||||
ui.add(egui::Slider::new(&mut age, 0..=120).text("age"));
|
|
||||||
if ui.button("Increment").clicked() {
|
ui.horizontal(|ui| {
|
||||||
age += 1;
|
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}"));
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue