Switch to async-fred-session, default config vals

Fixup some issues in README too
This commit is contained in:
Tyler Hallada 2023-10-17 00:15:44 -04:00
parent 835e9dc748
commit 7f86612899
5 changed files with 297 additions and 483 deletions

743
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -16,7 +16,7 @@ ammonia = "3.3.0"
ansi-to-html = "0.1"
anyhow = "1"
argon2 = "0.5"
async-redis-session = "0.2"
async-fred-session = "0.1"
axum = { version = "0.6", features = ["form", "headers", "multipart", "query"] }
axum-client-ip = "0.4"
# waiting for new axum-login release which will support sqlx v. 0.7+

View File

@ -47,6 +47,7 @@ builds
& 'C:\Program Files\PostgreSQL\13\bin\createdb.exe' -U postgres crawlnicle
& 'C:\Program Files\PostgreSQL\13\bin\psql.exe' -U postgres
postgres=# ALTER DATABASE crawlnicle OWNER TO crawlnicle;
postgres=# ALTER USER crawlnicle CREATEDB;
\password crawlnicle
```
@ -69,6 +70,7 @@ builds
SMTP_PASSWORD=password
EMAIL_FROM="crawlnicle <no-reply@mail.crawlnicle.com>"
SESSION_SECRET=64-bytes-of-secret
IP_SOURCE=ConnectInfo
```
1. Run `just migrate` (or `sqlx migrate run`) which will run all the database

View File

@ -33,21 +33,23 @@ impl FromStr for IpSource {
pub struct Config {
#[clap(long, env)]
pub database_url: String,
#[clap(long, env)]
#[clap(long, env, default_value = "5")]
pub database_max_connections: u32,
#[clap(long, env)]
#[clap(long, env, default_value = "redis://localhost")]
pub redis_url: String,
#[clap(long, env)]
#[clap(long, env, default_value = "5")]
pub redis_pool_size: usize,
#[clap(long, env, default_value = "127.0.0.1")]
pub host: String,
#[clap(long, env)]
#[clap(long, env, default_value = "3000")]
pub port: u16,
#[clap(long, env)]
#[clap(long, env, default_value = "http://localhost:3000")]
pub public_url: Url,
#[clap(long, env)]
#[clap(long, env, default_value = "crawlnicle")]
pub title: String,
#[clap(long, env)]
#[clap(long, env, default_value = "1000000")]
pub max_mem_log_size: usize,
#[clap(long, env)]
#[clap(long, env, default_value = "./content")]
pub content_dir: String,
#[clap(long, env)]
pub smtp_server: String,
@ -55,10 +57,10 @@ pub struct Config {
pub smtp_user: String,
#[clap(long, env)]
pub smtp_password: String,
#[clap(long, env)]
#[clap(long, env, default_value = "crawlnicle <no-reply@mail.crawlnicle.com>")]
pub email_from: Mailbox,
#[clap(long, env)]
pub session_secret: String,
#[clap(long, env)]
#[clap(long, env, default_value = "ConnectInfo")]
pub ip_source: IpSource,
}

View File

@ -1,7 +1,7 @@
use std::{collections::HashMap, net::SocketAddr, path::Path, sync::Arc};
use anyhow::Result;
use async_redis_session::RedisSessionStore;
use async_fred_session::{RedisSessionStore, fred::{pool::RedisPool, types::RedisConfig}};
use axum::{
response::IntoResponse,
routing::{get, post},
@ -72,11 +72,16 @@ async fn main() -> Result<()> {
.connect(&config.database_url)
.await?;
let session_store = RedisSessionStore::new(config.redis_url.clone())?;
let redis_config = RedisConfig::from_url(&config.redis_url)?;
let redis_pool = RedisPool::new(redis_config, None, None, config.redis_pool_size)?;
redis_pool.connect();
redis_pool.wait_for_connect().await?;
let session_store = RedisSessionStore::from_pool(redis_pool, Some("async-fred-session/".into()));
let session_layer = SessionLayer::new(session_store, secret).with_secure(false);
let user_store = PostgresStore::<User>::new(pool.clone())
.with_query("select * from users where user_id = $1");
let auth_layer = AuthLayer::new(user_store, &secret);
let auth_layer = AuthLayer::new(user_store, secret);
let creds = Credentials::new(config.smtp_user.clone(), config.smtp_password.clone());