Skip to content

Commit

Permalink
Finish setting up session store
Browse files Browse the repository at this point in the history
I think
  • Loading branch information
Celeo committed Feb 24, 2024
1 parent 5e07186 commit eb13d9d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 25 deletions.
64 changes: 62 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ sqlx = { version = "0.7.3", features = ["runtime-tokio", "sqlite", "chrono", "uu
tokio = { version = "1.36.0", features = ["full"] }
toml = "0.8.10"
tower = "0.4.13"
tower-sessions-sqlx-store = "0.10.0"
tower-sessions = "0.10.3"
tower-sessions-sqlx-store = { version = "0.10.0", features = ["sqlite"] }
23 changes: 19 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ use std::{
};
use tokio::signal;
use tower::ServiceBuilder;
use tower_sessions::SessionManagerLayer;
use tower_sessions_sqlx_store::SqliteStore;

mod endpoints;
mod middleware;
mod shared;

/// New vZDV website.
/// vZDV website.
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
Expand Down Expand Up @@ -79,10 +81,17 @@ async fn load_db(config: &Config) -> Result<SqlitePool> {
}

/// Create all the endpoints, include middleware, and connect with the state.
fn load_router(app_state: Arc<AppState>) -> Router {
fn load_router(
app_state: Arc<AppState>,
sessions_layer: SessionManagerLayer<SqliteStore>,
) -> Router {
Router::new()
.route("/", get(endpoints::handler_home))
.layer(ServiceBuilder::new().layer(axum_middleware::from_fn(middleware::logging)))
.layer(
ServiceBuilder::new()
.layer(axum_middleware::from_fn(middleware::logging))
.layer(sessions_layer),
)
.with_state(app_state)
}

Expand Down Expand Up @@ -143,6 +152,12 @@ async fn main() {
std::process::exit(1);
}
};
let sessions = SqliteStore::new(db.clone());
if let Err(e) = sessions.migrate().await {
error!("Could not create table for sessions: {e}");
return;
}
let session_layer = SessionManagerLayer::new(sessions);
let templates = load_templates();
debug!("Loaded");

Expand All @@ -152,7 +167,7 @@ async fn main() {
db,
templates,
});
let app = load_router(app_state);
let app = load_router(app_state, session_layer);
debug!("Set up");

let host_and_port = format!("{}:{}", cli.host, cli.port);
Expand Down
4 changes: 3 additions & 1 deletion src/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Structs and data to be shared across multiple parts of the site.
#![allow(unused, clippy::upper_case_acronyms)]
#![allow(unused)]

use minijinja::Environment;
use sqlx::SqlitePool;
Expand All @@ -16,6 +16,7 @@ pub struct AppState {
pub templates: Environment<'static>,
}

#[allow(clippy::upper_case_acronyms)]
pub enum ControllerRating {
OBS,
S1,
Expand All @@ -36,6 +37,7 @@ pub enum ControllerStatus {
LeaveOfAbsence,
}

#[allow(clippy::upper_case_acronyms)]
pub enum StaffPosition {
None,
ATM,
Expand Down
17 changes: 0 additions & 17 deletions src/shared/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ pub struct Certification {
pub set_by: Uuid,
}

#[derive(Debug, FromRow)]
pub struct Session {
pub id: Uuid,
pub controller_id: Uuid,
pub vatsim_token: String,
pub started: DateTime<Utc>,
}

/// Statements to create tables. Only ran when the DB file does not exist,
/// so no migration or "IF NOT EXISTS" conditions need to be added.
pub const CREATE_TABLES: &str = "
Expand All @@ -64,13 +56,4 @@ CREATE TABLE certification (
FOREIGN KEY (controller_id) REFERENCES controller(id)
);
CREATE TABLE session (
id TEXT PRIMARY KEY NOT NULL,
controller_id TEXT NOT NULL UNIQUE,
vatsim_token TEXT NOT NULL UNIQUE,
started TEXT NOT NULL,
FOREIGN KEY (controller_id) REFERENCES controller(id)
);
";

0 comments on commit eb13d9d

Please sign in to comment.