fix: got release builds actually working

This commit is contained in:
Andrew Rioux
2025-02-17 01:36:01 -05:00
parent 118d56fc36
commit d823603054
16 changed files with 244 additions and 211 deletions

View File

@@ -48,6 +48,7 @@ sparse-actions = { path = "../sparse-actions", optional = true }
sparse-handler = { path = "../sparse-handler", optional = true }
[features]
embed-beacons = []
hydrate = ["leptos/hydrate", "chrono/wasmbind"]
ssr = [
"dep:axum",

View File

@@ -8,22 +8,7 @@ use leptos_router::{
use crate::users::User;
#[server]
pub async fn test_retrieve() -> Result<u64, ServerFnError> {
use leptos::server_fn::error::NoCustomError;
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
let start = std::time::SystemTime::now();
let since_the_epoch = start
.duration_since(std::time::UNIX_EPOCH)
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(e.to_string()))?
.as_secs();
Ok(since_the_epoch)
}
#[server]
#[server(endpoint = "me")]
pub async fn me() -> Result<Option<User>, ServerFnError> {
let user = crate::db::user::get_auth_session().await?;
@@ -33,7 +18,7 @@ pub async fn me() -> Result<Option<User>, ServerFnError> {
}))
}
#[server]
#[server(endpoint = "login")]
async fn login(username: String, password: String, next: String) -> Result<(), ServerFnError> {
crate::db::user::create_auth_session(username, password).await?;
@@ -42,7 +27,7 @@ async fn login(username: String, password: String, next: String) -> Result<(), S
Ok(())
}
#[server]
#[server(endpoint = "logout")]
async fn logout() -> Result<(), ServerFnError> {
crate::db::user::destroy_auth_session().await
}

View File

@@ -11,7 +11,7 @@ pub struct Category {
pub category_name: String,
}
#[server]
#[server(prefix = "/api/categories", endpoint = "get")]
pub async fn get_categories() -> Result<Vec<Category>, ServerFnError> {
let user = user::get_auth_session().await?;
@@ -21,14 +21,14 @@ pub async fn get_categories() -> Result<Vec<Category>, ServerFnError> {
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
Ok(sqlx::query_as!(Category, "SELECT * FROM beacon_category")
.fetch_all(&db)
.await?)
}
#[server]
#[server(prefix = "/api/categories", endpoint = "add")]
pub async fn add_category(name: String) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -38,7 +38,7 @@ pub async fn add_category(name: String) -> Result<(), ServerFnError> {
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
sqlx::query!(
"INSERT INTO beacon_category (category_name) VALUES (?)",
@@ -50,7 +50,7 @@ pub async fn add_category(name: String) -> Result<(), ServerFnError> {
Ok(())
}
#[server]
#[server(prefix = "/api/categories", endpoint = "remove")]
pub async fn remove_category(id: i64) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -60,7 +60,7 @@ pub async fn remove_category(id: i64) -> Result<(), ServerFnError> {
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
sqlx::query!("DELETE FROM beacon_category WHERE category_id = ?", id)
.execute(&db)
@@ -69,7 +69,7 @@ pub async fn remove_category(id: i64) -> Result<(), ServerFnError> {
Ok(())
}
#[server]
#[server(prefix = "/api/categories", endpoint = "rename")]
pub async fn rename_category(id: i64, name: String) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -79,7 +79,7 @@ pub async fn rename_category(id: i64, name: String) -> Result<(), ServerFnError>
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
sqlx::query!(
"UPDATE beacon_category SET category_name = ? WHERE category_id = ?",

View File

@@ -48,7 +48,7 @@ pub struct BeaconConfig {
pub config_type: BeaconConfigTypes,
}
#[server]
#[server(prefix = "/api/configs", endpoint = "get")]
pub async fn get_beacon_configs() -> Result<Vec<BeaconConfig>, ServerFnError> {
let user = user::get_auth_session().await?;
@@ -58,14 +58,14 @@ pub async fn get_beacon_configs() -> Result<Vec<BeaconConfig>, ServerFnError> {
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
Ok(sqlx::query_as("SELECT * FROM beacon_config")
.fetch_all(&db)
.await?)
}
#[server]
#[server(prefix = "/api/configs", endpoint = "add")]
pub async fn add_beacon_config(
name: String,
mode: String,
@@ -83,7 +83,7 @@ pub async fn add_beacon_config(
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
match &*mode {
"single" => {
@@ -165,7 +165,7 @@ pub async fn add_beacon_config(
}
}
#[server]
#[server(prefix = "/api/configs", endpoint = "remove")]
pub async fn remove_beacon_config(id: i64) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -175,7 +175,7 @@ pub async fn remove_beacon_config(id: i64) -> Result<(), ServerFnError> {
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
sqlx::query!("DELETE FROM beacon_config WHERE config_id = ?", id)
.execute(&db)

View File

@@ -31,7 +31,7 @@ pub struct PubListener {
pub active: bool,
}
#[server]
#[server(prefix = "/api/listeners", endpoint = "get")]
pub async fn get_listeners() -> Result<Vec<PubListener>, ServerFnError> {
let user = user::get_auth_session().await?;
@@ -41,7 +41,7 @@ pub async fn get_listeners() -> Result<Vec<PubListener>, ServerFnError> {
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
let beacon_handles = expect_context::<BeaconListenerMap>();
let listeners = sqlx::query_as!(
@@ -84,7 +84,7 @@ pub fn generate_cert_from_keypair(
params.self_signed(&kp)
}
#[server]
#[server(prefix = "/api/listeners", endpoint = "add")]
pub async fn add_listener(
public_ip: String,
port: i16,
@@ -113,7 +113,7 @@ pub async fn add_listener(
})
.await??;
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
let public_ip = public_ip.to_string();
let cert = cert.der().to_vec();
@@ -133,7 +133,7 @@ pub async fn add_listener(
Ok(())
}
#[server]
#[server(prefix = "/api/listeners", endpoint = "remove")]
pub async fn remove_listener(listener_id: i64) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -163,7 +163,7 @@ pub async fn remove_listener(listener_id: i64) -> Result<(), ServerFnError> {
blm_handle.remove(&listener_id);
}
let pool = expect_context::<SqlitePool>();
let pool = crate::db::get_db()?;
sqlx::query!(
"DELETE FROM beacon_listener WHERE listener_id = ?",
@@ -175,7 +175,7 @@ pub async fn remove_listener(listener_id: i64) -> Result<(), ServerFnError> {
Ok(())
}
#[server]
#[server(prefix = "/api/listeners", endpoint = "start")]
pub async fn start_listener(listener_id: i64) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;

View File

@@ -59,7 +59,7 @@ cfg_if::cfg_if! {
}
}
#[server]
#[server(prefix = "/api/templates", endpoint = "add")]
pub async fn add_template(
template_name: String,
operating_system: String,
@@ -104,7 +104,7 @@ pub async fn add_template(
srverr!("Source MAC address is formatted incorrectly");
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
let listener = sqlx::query!(
"SELECT domain_name, certificate, privkey FROM beacon_listener WHERE listener_id = ?",
@@ -199,7 +199,7 @@ pub async fn add_template(
}
}
#[server]
#[server(prefix = "/api/templates", endpoint = "remove")]
pub async fn remove_template(template_id: i64) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -207,7 +207,7 @@ pub async fn remove_template(template_id: i64) -> Result<(), ServerFnError> {
srverr!("You are not signed in!");
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
sqlx::query!(
"DELETE FROM beacon_template WHERE template_id = ?",
@@ -219,7 +219,7 @@ pub async fn remove_template(template_id: i64) -> Result<(), ServerFnError> {
Ok(())
}
#[server]
#[server(prefix = "/api/templates", endpoint = "get")]
pub async fn get_templates() -> Result<Vec<BeaconTemplate>, ServerFnError> {
let user = user::get_auth_session().await?;
@@ -229,7 +229,7 @@ pub async fn get_templates() -> Result<Vec<BeaconTemplate>, ServerFnError> {
));
}
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
Ok(sqlx::query_as("SELECT * FROM beacon_template")
.fetch_all(&db)

View File

@@ -1,2 +1,28 @@
#[cfg(feature = "ssr")]
pub mod user;
#[cfg(feature = "ssr")]
pub fn get_db() -> Result<sqlx::SqlitePool, leptos::prelude::ServerFnError> {
let owner = leptos::prelude::Owner::current().unwrap();
tracing::debug!(
"Getting DB; debug ID: {:?}; ancestry: {:?}; type ID: {:?}",
owner.debug_id(),
owner.ancestry(),
std::any::TypeId::of::<sqlx::SqlitePool>(),
);
use leptos::{prelude::*, server_fn::error::NoCustomError};
match use_context::<sqlx::SqlitePool>() {
Some(v) => {
tracing::debug!("Got db pool!");
Ok(v)
}
None => {
tracing::debug!("Didn't get db pool!");
Err(ServerFnError::<NoCustomError>::ServerError(
"Could not use database connection".to_string(),
))
}
}
}

View File

@@ -1,5 +1,4 @@
use leptos::prelude::ServerFnError;
use leptos::{prelude::expect_context, server_fn::error::NoCustomError};
use leptos::{prelude::*, server_fn::error::NoCustomError};
use leptos_axum::{extract, ResponseOptions};
use pbkdf2::{
password_hash::{
@@ -121,7 +120,7 @@ pub async fn create_auth_session(username: String, password: String) -> Result<(
use axum::http::{header, HeaderValue};
use axum_extra::extract::cookie::{Cookie, SameSite};
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
let resp = expect_context::<ResponseOptions>();
let user: Option<User> =
@@ -181,7 +180,7 @@ pub async fn create_auth_session(username: String, password: String) -> Result<(
pub async fn destroy_auth_session() -> Result<(), ServerFnError> {
use axum_extra::extract::cookie::CookieJar;
let db = expect_context::<SqlitePool>();
let db = crate::db::get_db()?;
let jar = extract::<CookieJar>().await?;
let Some(cookie) = jar.get(SESSION_ID_KEY) else {
@@ -200,7 +199,10 @@ pub async fn destroy_auth_session() -> Result<(), ServerFnError> {
pub async fn get_auth_session() -> Result<Option<User>, ServerFnError> {
use axum_extra::extract::cookie::CookieJar;
let db = expect_context::<SqlitePool>();
println!("In get auth session");
let owner = leptos::prelude::Owner::current().unwrap();
let db = crate::db::get_db()?;
let jar = extract::<CookieJar>().await?;
let Some(cookie) = jar.get(SESSION_ID_KEY) else {

View File

@@ -21,7 +21,7 @@ async fn main() -> anyhow::Result<std::process::ExitCode> {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
format!("{}=debug,sparse_handler=debug", env!("CARGO_CRATE_NAME")).into()
format!("{}=debug,sparse_handler=debug,tower_http=trace", env!("CARGO_CRATE_NAME")).into()
}),
)
.with(tracing_subscriber::fmt::layer())

View File

@@ -58,7 +58,7 @@ pub struct PubUser {
last_active: Option<DateTime<Utc>>,
}
#[server]
#[server(prefix = "/api/users", endpoint = "delete")]
async fn delete_user(user_id: i64) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -68,7 +68,7 @@ async fn delete_user(user_id: i64) -> Result<(), ServerFnError> {
));
}
let pool = expect_context::<SqlitePool>();
let pool = crate::db::get_db()?;
sqlx::query!("DELETE FROM users WHERE user_id = ?", user_id)
.execute(&pool)
@@ -77,7 +77,7 @@ async fn delete_user(user_id: i64) -> Result<(), ServerFnError> {
Ok(())
}
#[server]
#[server(prefix = "/api/users", endpoint = "reset")]
async fn reset_password(user_id: i64, password: String) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -87,7 +87,7 @@ async fn reset_password(user_id: i64, password: String) -> Result<(), ServerFnEr
));
}
let pool = expect_context::<SqlitePool>();
let pool = crate::db::get_db()?;
crate::db::user::reset_password(&pool, user_id as i16, password).await?;
@@ -226,7 +226,7 @@ pub fn RenderUser(refresh_user_list: Action<(), ()>, user: PubUser) -> impl Into
}
}
#[server]
#[server(prefix = "/api/users", endpoint = "list")]
async fn list_users() -> Result<Vec<PubUser>, ServerFnError> {
let user = user::get_auth_session().await?;
@@ -238,7 +238,7 @@ async fn list_users() -> Result<Vec<PubUser>, ServerFnError> {
use futures::stream::StreamExt;
let pool = expect_context::<SqlitePool>();
let pool = crate::db::get_db()?;
let users = sqlx::query_as!(DbUser, "SELECT user_id, user_name, last_active FROM users")
.fetch(&pool)
@@ -260,7 +260,7 @@ async fn list_users() -> Result<Vec<PubUser>, ServerFnError> {
Ok(users?)
}
#[server]
#[server(prefix = "/api/users", endpoint = "add")]
async fn add_user(name: String, password: String) -> Result<(), ServerFnError> {
let user = user::get_auth_session().await?;
@@ -270,7 +270,7 @@ async fn add_user(name: String, password: String) -> Result<(), ServerFnError> {
));
}
let pool = expect_context::<SqlitePool>();
let pool = crate::db::get_db()?;
crate::db::user::create_user(&pool, name, password).await?;

View File

@@ -3,7 +3,7 @@ use std::{net::SocketAddrV4, process::ExitCode};
use axum::{
extract::{FromRef, Path, Query, State},
response::IntoResponse,
routing::get,
routing::{get, post},
Router,
};
use leptos::prelude::*;
@@ -323,18 +323,30 @@ pub async fn serve_web(
db: db.clone(),
};
println!("{:?}", std::any::TypeId::of::<SqlitePool>());
dbg!(&routes);
let app = Router::new()
.route(
"/binaries/installer/:template_id",
get(download_beacon_installer),
)
.route("/binaries/beacon/:template_id", get(download_beacon))
.route("/api/*fn_name", post(leptos_axum::handle_server_fns))
.leptos_routes_with_context(
&state,
routes,
move || {
let owner = leptos::prelude::Owner::current().unwrap();
tracing::debug!(
"Providing DB; debug ID: {:?}; ancestry: {:?}; type ID: {:?}",
owner.debug_id(),
owner.ancestry(),
std::any::TypeId::of::<sqlx::SqlitePool>(),
);
provide_context(beacon_listeners.clone());
provide_context::<SqlitePool>(db.clone());
provide_context(db.clone());
},
{
let leptos_options = leptos_options.clone();