get host addr/port from database when requesting credentials
This commit is contained in:
parent
616600687d
commit
94400ba7d5
@ -1,8 +1,3 @@
|
|||||||
#![cfg_attr(
|
|
||||||
all(not(debug_assertions), target_os = "windows"),
|
|
||||||
windows_subsystem = "windows"
|
|
||||||
)]
|
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
@ -32,33 +27,6 @@ use crate::{
|
|||||||
pub static APP: OnceCell<AppHandle> = OnceCell::new();
|
pub static APP: OnceCell<AppHandle> = OnceCell::new();
|
||||||
|
|
||||||
|
|
||||||
async fn setup(app: &mut App) -> Result<(), Box<dyn Error>> {
|
|
||||||
APP.set(app.handle()).unwrap();
|
|
||||||
|
|
||||||
let conn_opts = SqliteConnectOptions::new()
|
|
||||||
.filename(config::get_or_create_db_path()?)
|
|
||||||
.create_if_missing(true);
|
|
||||||
let pool_opts = SqlitePoolOptions::new();
|
|
||||||
let pool: SqlitePool = pool_opts.connect_with(conn_opts).await?;
|
|
||||||
sqlx::migrate!().run(&pool).await?;
|
|
||||||
|
|
||||||
let conf = AppConfig::load(&pool).await?;
|
|
||||||
let session = Session::load(&pool).await?;
|
|
||||||
let srv = Server::new(conf.listen_addr, conf.listen_port, app.handle()).await?;
|
|
||||||
|
|
||||||
config::set_auto_launch(conf.start_on_login)?;
|
|
||||||
if !conf.start_minimized {
|
|
||||||
app.get_window("main")
|
|
||||||
.ok_or(HandlerError::NoMainWindow)?
|
|
||||||
.show()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let state = AppState::new(conf, session, srv, pool);
|
|
||||||
app.manage(state);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn run() -> tauri::Result<()> {
|
pub fn run() -> tauri::Result<()> {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
|
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
|
||||||
@ -89,4 +57,37 @@ pub fn run() -> tauri::Result<()> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub async fn connect_db() -> Result<SqlitePool, SetupError> {
|
||||||
|
let conn_opts = SqliteConnectOptions::new()
|
||||||
|
.filename(config::get_or_create_db_path()?)
|
||||||
|
.create_if_missing(true);
|
||||||
|
let pool_opts = SqlitePoolOptions::new();
|
||||||
|
let pool: SqlitePool = pool_opts.connect_with(conn_opts).await?;
|
||||||
|
Ok(pool)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async fn setup(app: &mut App) -> Result<(), Box<dyn Error>> {
|
||||||
|
APP.set(app.handle()).unwrap();
|
||||||
|
|
||||||
|
let pool = connect_db().await?;
|
||||||
|
sqlx::migrate!().run(&pool).await?;
|
||||||
|
|
||||||
|
let conf = AppConfig::load(&pool).await?;
|
||||||
|
let session = Session::load(&pool).await?;
|
||||||
|
let srv = Server::new(conf.listen_addr, conf.listen_port, app.handle()).await?;
|
||||||
|
|
||||||
|
config::set_auto_launch(conf.start_on_login)?;
|
||||||
|
if !conf.start_minimized {
|
||||||
|
app.get_window("main")
|
||||||
|
.ok_or(HandlerError::NoMainWindow)?
|
||||||
|
.show()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let state = AppState::new(conf, session, srv, pool);
|
||||||
|
app.manage(state);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use std::io::{Read, Write};
|
|
||||||
use std::process::Command as ChildCommand;
|
use std::process::Command as ChildCommand;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::process::CommandExt;
|
use std::os::unix::process::CommandExt;
|
||||||
@ -9,9 +8,14 @@ use clap::{
|
|||||||
ArgMatches,
|
ArgMatches,
|
||||||
ArgAction
|
ArgAction
|
||||||
};
|
};
|
||||||
use std::net::TcpStream;
|
use tokio::{
|
||||||
|
net::TcpStream,
|
||||||
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
use crate::app;
|
||||||
|
use crate::config::AppConfig;
|
||||||
use crate::credentials::{BaseCredentials, SessionCredentials};
|
use crate::credentials::{BaseCredentials, SessionCredentials};
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
|
|
||||||
@ -98,16 +102,19 @@ pub fn exec(args: &ArgMatches) -> Result<(), CliError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn get_credentials(base: bool) -> Result<String, RequestError> {
|
#[tokio::main]
|
||||||
|
async fn get_credentials(base: bool) -> Result<String, RequestError> {
|
||||||
|
let pool = app::connect_db().await?;
|
||||||
|
let config = AppConfig::load(&pool).await?;
|
||||||
let path = if base {"/creddy/base-credentials"} else {"/"};
|
let path = if base {"/creddy/base-credentials"} else {"/"};
|
||||||
|
|
||||||
let mut stream = TcpStream::connect("127.0.0.1:12345")?;
|
let mut stream = TcpStream::connect((config.listen_addr, config.listen_port)).await?;
|
||||||
let req = format!("GET {path} HTTP/1.0\r\n\r\n");
|
let req = format!("GET {path} HTTP/1.0\r\n\r\n");
|
||||||
stream.write_all(req.as_bytes())?;
|
stream.write_all(req.as_bytes()).await?;
|
||||||
|
|
||||||
// some day we'll have a proper HTTP parser
|
// some day we'll have a proper HTTP parser
|
||||||
let mut buf = vec![0; 8192];
|
let mut buf = vec![0; 8192];
|
||||||
stream.read_to_end(&mut buf)?;
|
stream.read_to_end(&mut buf).await?;
|
||||||
|
|
||||||
let status = buf.split(|&c| &[c] == b" ")
|
let status = buf.split(|&c| &[c] == b" ")
|
||||||
.skip(1)
|
.skip(1)
|
||||||
|
@ -198,6 +198,8 @@ pub enum RequestError {
|
|||||||
InvalidJson,
|
InvalidJson,
|
||||||
#[error("Error reading/writing stream: {0}")]
|
#[error("Error reading/writing stream: {0}")]
|
||||||
StreamIOError(#[from] std::io::Error),
|
StreamIOError(#[from] std::io::Error),
|
||||||
|
#[error("Error loading configuration data: {0}")]
|
||||||
|
Setup(#[from] SetupError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
#![cfg_attr(
|
||||||
|
all(not(debug_assertions), target_os = "windows"),
|
||||||
|
windows_subsystem = "windows"
|
||||||
|
)]
|
||||||
|
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod cli;
|
mod cli;
|
||||||
mod config;
|
mod config;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user