Skip to content

Commit

Permalink
Some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Nov 9, 2024
1 parent aae374a commit 465fe5c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
.DS_Store
*.tar.*
2 changes: 1 addition & 1 deletion rwf-cli/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn package(config: Option<PathBuf>) -> Result<(), Box<dyn std::error::
tar.append_file(&info.name, &mut File::open(executable).expect("binary"))
.expect("binary");

for path in ["static", "templates"] {
for path in ["static", "templates", "migrations"] {
let p = Path::new(path);

if p.is_dir() {
Expand Down
28 changes: 6 additions & 22 deletions rwf-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ enum Subcommands {
/// Remove a controller/view/model/all of the above
Remove(RemoveSubcommand),

/// Build and deploy the application.
Deploy(DeploySubcommand),
/// Package the application into a tarball.
Package {
#[arg(long, short)]
config: Option<PathBuf>,
},
}

#[derive(Args, Debug)]
Expand Down Expand Up @@ -111,21 +114,6 @@ enum Remove {
},
}

#[derive(Args, Debug)]
struct DeploySubcommand {
#[command(subcommand)]
command: Deploy,
}

#[derive(Subcommand, Debug)]
enum Deploy {
/// Package the application for deployment.
Package {
#[arg(long, short, help = "Rwf production configuration file")]
config: Option<PathBuf>,
},
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
// std::env::set_var("RWF_LOG_QUERIES", "1");
Expand Down Expand Up @@ -174,11 +162,7 @@ async fn main() {
}
},

Subcommands::Deploy(deploy) => match deploy.command {
Deploy::Package { config } => {
deploy::package(config).await.unwrap();
}
},
Subcommands::Package { config } => deploy::package(config).await.unwrap(),
}
}

Expand Down
30 changes: 20 additions & 10 deletions rwf/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::env::var;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use time::Duration;
use tracing::info;

use crate::controller::middleware::csrf::Csrf;
use crate::controller::middleware::{request_tracker::RequestTracker, Middleware};
Expand Down Expand Up @@ -64,6 +65,7 @@ impl Default for ConfigFile {
websocket: WebsocketConfig::default(),
}
.transform()
.unwrap()
}
}

Expand All @@ -84,19 +86,12 @@ impl ConfigFile {
let mut config: Self = toml::from_str(&file)?;
config.path = Some(path.as_ref().to_owned());

let mut config = config.transform();

let secret_key = config.general.secret_key()?;

config.general.aes_key =
Key::<AesGcmSiv<Aes128>>::clone_from_slice(&secret_key[0..128 / 8]);
config.general.secure_id_key =
Key::<AesGcmSiv<Aes128>>::clone_from_slice(&secret_key[128 / 8..]);
let config = config.transform()?;

Ok(config)
}

fn transform(mut self) -> Self {
fn transform(mut self) -> Result<Self, Error> {
let mut default_middleware = vec![];

// Request tracker always first. We want it to always run.
Expand All @@ -109,7 +104,22 @@ impl ConfigFile {
}

self.general.default_middleware = MiddlewareSet::without_default(default_middleware);
self

let secret_key = self.general.secret_key()?;

self.general.aes_key = Key::<AesGcmSiv<Aes128>>::clone_from_slice(&secret_key[0..128 / 8]);
self.general.secure_id_key =
Key::<AesGcmSiv<Aes128>>::clone_from_slice(&secret_key[128 / 8..]);

Ok(self)
}

pub fn log_info(&self) {
if let Some(ref path) = self.path {
info!("Configuration file \"{}\" loaded", path.display());
} else {
info!("Configuration file missing, loaded from environment instead");
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions rwf/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
use crate::config::get_config;
use once_cell::sync::OnceCell;
use tracing_subscriber::{filter::LevelFilter, fmt, util::SubscriberInitExt, EnvFilter};

static INITIALIZED: OnceCell<()> = OnceCell::new();

pub struct Logger;

impl Logger {
pub fn init() {
setup_logging()
INITIALIZED.get_or_init(|| {
setup_logging();
get_config().log_info();

()
});
}
}

pub fn setup_logging() {
fn setup_logging() {
fmt()
.with_env_filter(
EnvFilter::builder()
Expand Down
17 changes: 14 additions & 3 deletions rwf/src/model/pool/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ impl Connection {
pub async fn new(database_url: &str) -> Result<Self, Error> {
let (client, connection) = tokio_postgres::connect(database_url, NoTls).await?;

info!("New connection to PostgreSQL created");

let bad = AtomicBool::new(false);
let shutdown = Notify::new();

let inner = Arc::new(ConnectionInner { bad, shutdown });

let guard = Connection {
let mut guard = Connection {
client,
inner: inner.clone(),
last_used: Instant::now(),
Expand All @@ -71,6 +69,19 @@ impl Connection {
}
});

let info = guard
.query_cached("SELECT current_database()::text, current_user::text", &[])
.await?;

let row = info.get(0).unwrap();
let user: String = row.get::<_, String>(1);
let database: String = row.get::<_, String>(0);

info!(
"New connection to database \"{}\" with user \"{}\" created",
database, user
);

Ok(guard)
}

Expand Down

0 comments on commit 465fe5c

Please sign in to comment.