refactor(session): extract SessionConfig and add auto-migration
- session/config.rs: add SessionConfig struct that pre-validates all session configuration values, with build_middleware() for infallible middleware construction - session/middleware.rs: expose parse_same_site as pub(crate) - session/storage/redis.rs: derive Clone for RedisSessionStore - main.rs: validate session config before HttpServer loop, use SessionConfig::build_middleware() inside closure; add sqlx::migrate!() call after database connection
This commit is contained in:
@@ -12,7 +12,7 @@ use appks::etcd::EtcdRegistry;
|
||||
use appks::models::db::AppDatabase;
|
||||
use appks::queue::NatsQueue;
|
||||
use appks::service::AppService;
|
||||
use appks::session::{RedisSessionStore, SessionMiddleware};
|
||||
use appks::session::RedisSessionStore;
|
||||
use appks::storage::s3::AppS3Storage;
|
||||
use sqlx::Executor;
|
||||
use utoipa::OpenApi;
|
||||
@@ -28,9 +28,13 @@ async fn main() -> AppResult<()> {
|
||||
|
||||
let db = AppDatabase::from_config(&config).await?;
|
||||
db.writer().execute("SELECT 1").await?;
|
||||
sqlx::migrate!("./migrate")
|
||||
.run(db.writer())
|
||||
.await
|
||||
.map_err(|e| AppError::Config(format!("database migration failed: {e}")))?;
|
||||
|
||||
let redis = AppRedis::from_config(&config)?;
|
||||
let cache = Arc::new(AppCache::from_config(&config)?);
|
||||
let redis = AppRedis::from_config(&config).await?;
|
||||
let cache = Arc::new(AppCache::from_config(&config).await?);
|
||||
let storage = AppS3Storage::from_config(&config).await?;
|
||||
|
||||
let registry = Arc::new(EtcdRegistry::connect(&config).await?);
|
||||
@@ -42,7 +46,6 @@ async fn main() -> AppResult<()> {
|
||||
}
|
||||
|
||||
let nats = Arc::new(NatsQueue::connect(&config).await?);
|
||||
nats.ensure_stream("IM", vec!["im.>".to_string()]).await?;
|
||||
|
||||
let service = AppService::new(
|
||||
env!("CARGO_PKG_VERSION").to_string(),
|
||||
@@ -55,6 +58,17 @@ async fn main() -> AppResult<()> {
|
||||
nats,
|
||||
);
|
||||
|
||||
let rpc_host = config.get_env_or::<String>("APP_RPC_SELF_HOST", "0.0.0.0".to_string())?;
|
||||
let rpc_port = config.get_env_or::<u16>("APP_RPC_SELF_PORT", 50050)?;
|
||||
let rpc_addr: std::net::SocketAddr = format!("{rpc_host}:{rpc_port}").parse()
|
||||
.map_err(|e| appks::error::AppError::Config(format!("invalid gRPC address: {e}")))?;
|
||||
let grpc_service = service.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = appks::grpc::start_grpc_server(rpc_addr, grpc_service).await {
|
||||
tracing::error!(error = %e, "gRPC server failed");
|
||||
}
|
||||
});
|
||||
|
||||
let host = config.get_env_or::<String>("APP_HTTP_HOST", "0.0.0.0".to_string())?;
|
||||
let port = config.get_env_or::<u16>("APP_HTTP_PORT", 8000)?;
|
||||
let workers = config.get_env_or::<usize>(
|
||||
@@ -65,16 +79,16 @@ async fn main() -> AppResult<()> {
|
||||
)?;
|
||||
let bind_addr = format!("{host}:{port}");
|
||||
let session_key = build_session_key(&config)?;
|
||||
let session_cfg = config.session_config()?;
|
||||
|
||||
tracing::info!(addr = %bind_addr, workers, "http server listening");
|
||||
|
||||
HttpServer::new(move || {
|
||||
let session_store = RedisSessionStore::new(redis.clone());
|
||||
let session_middleware =
|
||||
SessionMiddleware::from_app_config(session_store, session_key.clone(), &config)
|
||||
.expect("valid session configuration");
|
||||
let session_middleware = session_cfg.build_middleware(session_store, session_key.clone());
|
||||
|
||||
App::new()
|
||||
.wrap(actix_web::middleware::Logger::default())
|
||||
.app_data(web::Data::new(service.clone()))
|
||||
.wrap(session_middleware)
|
||||
.route("/healthz", web::get().to(healthz))
|
||||
|
||||
Reference in New Issue
Block a user