Añade soporte para sesiones y cookies

This commit is contained in:
Manuel Cillero 2025-07-27 22:55:14 +02:00
parent a1bb6cd12d
commit 66289e131e
6 changed files with 246 additions and 21 deletions

View file

@ -5,6 +5,10 @@ mod figfont;
use crate::core::{extension, extension::ExtensionRef};
use crate::{global, locale, service, trace};
use actix_session::config::{BrowserSession, PersistentSession, SessionLifecycle};
use actix_session::storage::CookieSessionStore;
use actix_session::SessionMiddleware;
use substring::Substring;
use std::io::Error;
@ -111,9 +115,27 @@ impl Application {
/// Devuelve [`std::io::Error`] si el *socket* no puede enlazarse (por puerto en uso, permisos,
/// etc.).
pub fn run(self) -> Result<service::Server, Error> {
// Genera clave secreta para firmar y verificar cookies.
let secret_key = service::cookie::Key::generate();
// Prepara el servidor web.
Ok(service::HttpServer::new(move || {
Self::service_app().wrap(tracing_actix_web::TracingLogger::default())
Self::service_app()
.wrap(tracing_actix_web::TracingLogger::default())
.wrap(
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
.session_lifecycle(match global::SETTINGS.server.session_lifetime {
0 => SessionLifecycle::BrowserSession(BrowserSession::default()),
_ => SessionLifecycle::PersistentSession(
PersistentSession::default().session_ttl(
service::cookie::time::Duration::seconds(
global::SETTINGS.server.session_lifetime,
),
),
),
})
.build(),
)
})
.bind(format!(
"{}:{}",

View file

@ -6,23 +6,24 @@ use serde::Deserialize;
include_config!(SETTINGS: Settings => [
// [app]
"app.name" => "Sample",
"app.description" => "Developed with the amazing PageTop framework.",
"app.theme" => "Basic",
"app.language" => "en-US",
"app.startup_banner" => "Slant",
"app.name" => "Sample",
"app.description" => "Developed with the amazing PageTop framework.",
"app.theme" => "Basic",
"app.language" => "en-US",
"app.startup_banner" => "Slant",
// [log]
"log.enabled" => true,
"log.tracing" => "Info",
"log.rolling" => "Stdout",
"log.path" => "log",
"log.prefix" => "tracing.log",
"log.format" => "Full",
"log.enabled" => true,
"log.tracing" => "Info",
"log.rolling" => "Stdout",
"log.path" => "log",
"log.prefix" => "tracing.log",
"log.format" => "Full",
// [server]
"server.bind_address" => "localhost",
"server.bind_port" => 8080,
"server.bind_address" => "localhost",
"server.bind_port" => 8080,
"server.session_lifetime" => 604_800,
]);
#[derive(Debug, Deserialize)]
@ -80,4 +81,8 @@ pub struct Server {
pub bind_address: String,
/// Puerto de escucha del servidor web.
pub bind_port: u16,
/// Duración de la cookie de sesión en segundos (p.ej. `604_800` para una semana).
///
/// El valor `0` indica que la cookie permanecerá activa hasta que se cierre el navegador.
pub session_lifetime: i64,
}

View file

@ -34,7 +34,7 @@ pub use crate::locale::*;
pub use crate::datetime::*;
pub use crate::service;
pub use crate::service::{HttpRequest, HttpResponse};
pub use crate::service::{HttpMessage, HttpRequest, HttpResponse};
pub use crate::core::{AnyCast, AnyInfo, TypeInfo};

View file

@ -1,12 +1,13 @@
//! Gestión del servidor y servicios web (con [Actix Web](https://docs.rs/actix-web)).
pub use actix_session::Session;
pub use actix_web::body::BoxBody;
pub use actix_web::dev::Server;
pub use actix_web::dev::ServiceFactory as Factory;
pub use actix_web::dev::ServiceRequest as Request;
pub use actix_web::dev::ServiceResponse as Response;
pub use actix_web::{http, rt, web};
pub use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer};
pub use actix_web::{cookie, http, rt, web};
pub use actix_web::{App, Error, HttpMessage, HttpRequest, HttpResponse, HttpServer};
#[doc(hidden)]
pub use actix_web::test;