Compare commits

..

No commits in common. "88aaf3d0d66966f093b515ed7bc1f5f499319f08" and "6ca79160129f4fa7b57ffb701c6de3f6706ee3fd" have entirely different histories.

2 changed files with 15 additions and 34 deletions

View file

@ -1,4 +1,4 @@
use std::{net::{Ipv4Addr, SocketAddr}, path::PathBuf, sync::{Arc, LazyLock}};
use std::{net::SocketAddr, path::PathBuf, sync::{Arc, LazyLock}};
use axum::{extract::State, http::HeaderMap, middleware, routing::get};
use clap::{Parser, Subcommand};
@ -21,8 +21,7 @@ pub mod routes;
#[command(version = "1.0")]
#[command(about = "BAL management server", long_about = None)]
struct Cli {
/// Path to the sqlite database [default: ./alexandria.db]
#[arg(long, short, global = true, value_name = "FILE")]
#[arg(long, short, value_name = "FILE")]
database: Option<PathBuf>,
#[command(subcommand)]
@ -31,16 +30,7 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
/// Serves the web server
Run {
/// Port on which to serve the web server
#[arg(short, long, default_value_t = 3000)]
port: u16,
/// How many seconds generated JWTs are valid for. Default equates to 6 months
#[arg(long, default_value_t = 15_778_476)]
token_expiration_time: u64,
},
/// Open a TUI to manage user accounts
Run,
User
}
@ -69,15 +59,13 @@ static KEYS: LazyLock<Keys> = LazyLock::new(|| {
Keys::new(secret.as_bytes())
});
static CLI: LazyLock<Cli> = LazyLock::new(|| {
Cli::parse()
});
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let db_path = match &CLI.database {
let cli = Cli::parse();
let db_path = match cli.database {
Some(path) => {
if path.is_dir() {
log::error!("{path:?} is a directory");
@ -124,13 +112,13 @@ async fn main() {
return;
}
match &CLI.command {
Commands::Run {port,..} => run_server(db, *port).await,
match cli.command {
Commands::Run => run_server(db).await,
Commands::User => utils::cli::manage_users(db).await
}
}
async fn run_server(db: Arc<DatabaseConnection>, port: u16) {
async fn run_server(db: Arc<DatabaseConnection>) {
let (event_bus, _) = broadcast::channel(16);
if std::env::var("JWT_SECRET").is_err() {
@ -171,7 +159,7 @@ async fn run_server(db: Arc<DatabaseConnection>, port: u16) {
}
}
let open_api_router = OpenApiRouter::new()
let (router, mut api) = OpenApiRouter::new()
// Book API
.routes(routes!(routes::book::get_book_by_ean))
.routes(routes!(routes::book::get_book_by_id))
@ -200,12 +188,8 @@ async fn run_server(db: Arc<DatabaseConnection>, port: u16) {
.routes(routes!(routes::auth::check_token))
// Misc
.routes(routes!(routes::websocket::ws_handler))
.route("/", get(index))
.with_state(shared_state.clone());
let (router, mut api) = OpenApiRouter::new()
.nest("/api", open_api_router)
.route("/", get(index)) // temporary index page, will redirect/proxy to flutter app
.with_state(shared_state)
.split_for_parts();
@ -233,7 +217,7 @@ async fn run_server(db: Arc<DatabaseConnection>, port: u16) {
let router = router.merge(swagger);
let listener = tokio::net::TcpListener::bind(SocketAddr::new(std::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), port)).await.unwrap();
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
log::info!("Running on http://{}", listener.local_addr().unwrap());
axum::serve(
listener,

View file

@ -7,9 +7,9 @@ use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::{entities::user, AppState, Commands, CLI, KEYS};
use crate::{entities::user, AppState, KEYS};
//const TOKEN_EXPIRY_TIME: u64 = 15_778_476; // 6 Months
const TOKEN_EXPIRY_TIME: u64 = 15_778_476; // 6 Months
pub async fn auth_middleware(
_claims: Claims,
@ -49,10 +49,7 @@ pub async fn auth(State(state): State<Arc<AppState>>, Json(payload): Json<AuthPa
let claims = Claims {
sub: user.username,
exp: unix_timestamp + match CLI.command {
Commands::Run { token_expiration_time, .. } => token_expiration_time,
_ => panic!("The auth endpoint cannot be used outside of a Run command")
},
exp: unix_timestamp + TOKEN_EXPIRY_TIME,
user_id: user.id
};
let token = encode(&Header::default(), &claims, &KEYS.encoding)