♻️ Reestructura la API del servidor web
This commit is contained in:
parent
7149980e5d
commit
bdedede3f1
22 changed files with 77 additions and 75 deletions
|
|
@ -21,8 +21,8 @@ impl ModuleTrait for Admin {
|
|||
Some(l("module_description"))
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
cfg.service(app::web::scope("/admin").route("", app::web::get().to(summary::summary)));
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
|
||||
cfg.service(server::web::scope("/admin").route("", server::web::get().to(summary::summary)));
|
||||
}
|
||||
|
||||
fn actions(&self) -> Vec<HookAction> {
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ impl ModuleTrait for Node {
|
|||
Some(l("module_description"))
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
cfg.route("/node", app::web::get().to(node));
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
|
||||
cfg.route("/node", server::web::get().to(node));
|
||||
}
|
||||
|
||||
fn actions(&self) -> Vec<HookAction> {
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ impl ModuleTrait for User {
|
|||
Some(l("module_description"))
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
cfg.route("/user/login", app::web::get().to(login));
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
|
||||
cfg.route("/user/login", server::web::get().to(login));
|
||||
}
|
||||
|
||||
fn migrations(&self) -> Vec<MigrationItem> {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,3 @@
|
|||
pub use actix_web::{
|
||||
http, web, App, HttpMessage, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||
};
|
||||
pub use actix_web_files::Files as ActixFiles;
|
||||
pub use actix_web_static_files::ResourceFiles;
|
||||
|
||||
mod banner;
|
||||
|
||||
pub mod locale;
|
||||
|
||||
pub mod application;
|
||||
|
||||
pub mod fatal_error;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
use super::fatal_error::FatalError;
|
||||
|
||||
use crate::core::module::ModuleStaticRef;
|
||||
use crate::core::{module, theme};
|
||||
use crate::html::Markup;
|
||||
use crate::response::FatalError;
|
||||
use crate::response::page::ResultPage;
|
||||
use crate::{config, db, trace, LazyStatic};
|
||||
use crate::{config, db, locale, server, trace, LazyStatic};
|
||||
|
||||
use actix_web::dev::Server;
|
||||
|
||||
|
|
@ -23,7 +22,7 @@ impl Application {
|
|||
LazyStatic::force(&trace::TRACING);
|
||||
|
||||
// Valida el identificador de idioma.
|
||||
LazyStatic::force(&super::locale::LANGID);
|
||||
LazyStatic::force(&locale::LANGID);
|
||||
|
||||
#[cfg(feature = "database")]
|
||||
// Conecta con la base de datos.
|
||||
|
|
@ -46,12 +45,12 @@ impl Application {
|
|||
module::all::run_migrations();
|
||||
|
||||
// Prepara el servidor web.
|
||||
let server = super::HttpServer::new(move || {
|
||||
super::App::new()
|
||||
let server = server::HttpServer::new(move || {
|
||||
server::App::new()
|
||||
.wrap(tracing_actix_web::TracingLogger::default())
|
||||
.configure(module::all::configure_services)
|
||||
.configure(theme::all::configure_services)
|
||||
.default_service(super::web::route().to(service_not_found))
|
||||
.default_service(server::web::route().to(service_not_found))
|
||||
})
|
||||
.bind(format!(
|
||||
"{}:{}",
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
use crate::{config, trace, LazyStatic};
|
||||
|
||||
use unic_langid::LanguageIdentifier;
|
||||
|
||||
/// Almacena el Identificador de Idioma Unicode
|
||||
/// ([Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier))
|
||||
/// para la aplicación, obtenido de `SETTINGS.app.language`.
|
||||
pub static LANGID: LazyStatic<LanguageIdentifier> =
|
||||
LazyStatic::new(|| match config::SETTINGS.app.language.parse() {
|
||||
Ok(language) => language,
|
||||
Err(_) => {
|
||||
trace::warn!(
|
||||
"{}, {} \"{}\"! {}, {}",
|
||||
"Failed to parse language",
|
||||
"unrecognized Unicode Language Identifier",
|
||||
config::SETTINGS.app.language,
|
||||
"Using \"en-US\"",
|
||||
"check the settings file",
|
||||
);
|
||||
"en-US".parse().unwrap()
|
||||
}
|
||||
});
|
||||
|
|
@ -19,8 +19,8 @@ impl ModuleTrait for DefaultHomePage {
|
|||
Some(l("module_description"))
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
cfg.route("/", app::web::get().to(demo));
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
|
||||
cfg.route("/", server::web::get().to(demo));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ impl ThemeTrait for Aliner {
|
|||
THEME_ALINER
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
|
||||
configure_service_for_static_files!(cfg, "/aliner", bundle_aliner);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ impl ThemeTrait for Bootsier {
|
|||
THEME_BOOTSIER
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
|
||||
configure_service_for_static_files!(cfg, "/bootsier", bundle_bootsier);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ impl ThemeTrait for Bulmix {
|
|||
THEME_BULMIX
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
|
||||
configure_service_for_static_files!(cfg, "/bulmix", bundle_bulmix);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use super::ModuleStaticRef;
|
|||
|
||||
use crate::core::hook::add_action;
|
||||
use crate::core::theme;
|
||||
use crate::{app, db, trace, LazyStatic};
|
||||
use crate::{db, server, trace, LazyStatic};
|
||||
|
||||
#[cfg(feature = "database")]
|
||||
use crate::{db::*, run_now};
|
||||
|
|
@ -132,7 +132,7 @@ pub fn run_migrations() {
|
|||
|
||||
// CONFIGURE SERVICES ******************************************************************************
|
||||
|
||||
pub fn configure_services(cfg: &mut app::web::ServiceConfig) {
|
||||
pub fn configure_services(cfg: &mut server::web::ServiceConfig) {
|
||||
for m in ENABLED_MODULES.read().unwrap().iter() {
|
||||
m.configure_service(cfg);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::app;
|
||||
use crate::core::hook::HookAction;
|
||||
use crate::core::theme::ThemeStaticRef;
|
||||
use crate::server;
|
||||
use crate::util::{single_type_name, Handle};
|
||||
|
||||
#[cfg(feature = "database")]
|
||||
|
|
@ -49,7 +49,7 @@ pub trait ModuleTrait: BaseModule + Send + Sync {
|
|||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {}
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {}
|
||||
}
|
||||
|
||||
impl<M: ?Sized + ModuleTrait> BaseModule for M {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use super::ThemeStaticRef;
|
||||
use crate::{app, base, configure_service_for_static_files, trace, LazyStatic};
|
||||
use crate::{base, configure_service_for_static_files, server, trace, LazyStatic};
|
||||
|
||||
use std::sync::RwLock;
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ pub fn theme_by_single_name(single_name: &str) -> Option<ThemeStaticRef> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn configure_services(cfg: &mut app::web::ServiceConfig) {
|
||||
pub fn configure_services(cfg: &mut server::web::ServiceConfig) {
|
||||
configure_service_for_static_files!(cfg, "/theme", bundle_theme);
|
||||
|
||||
for t in THEMES.read().unwrap().iter() {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use crate::core::component::ComponentTrait;
|
|||
use crate::html::{html, Favicon, Markup};
|
||||
use crate::response::page::{Page, PageContext, PageOp};
|
||||
use crate::util::{single_type_name, Handle};
|
||||
use crate::{app, concat_string, config};
|
||||
use crate::{concat_string, config, server};
|
||||
|
||||
pub type ThemeStaticRef = &'static dyn ThemeTrait;
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
|
|||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {}
|
||||
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn before_render_page(&self, page: &mut Page) {
|
||||
|
|
|
|||
|
|
@ -63,12 +63,12 @@ pub mod html;
|
|||
#[cfg(feature = "database")]
|
||||
pub mod db;
|
||||
|
||||
// Prepara y ejecuta la aplicación.
|
||||
pub mod app;
|
||||
|
||||
// APIs esenciales para crear componentes, acciones, módulos y temas.
|
||||
pub mod core;
|
||||
|
||||
// API del servidor web.
|
||||
pub mod server;
|
||||
|
||||
// Tipos de respuestas a peticiones web.
|
||||
pub mod response;
|
||||
// Base de componentes, módulos y temas.
|
||||
|
|
@ -76,6 +76,9 @@ pub mod base;
|
|||
// Macros y funciones útiles.
|
||||
pub mod util;
|
||||
|
||||
// Prepara y ejecuta la aplicación.
|
||||
pub mod app;
|
||||
|
||||
// RE-EXPORTA API ÚNICA.
|
||||
|
||||
pub mod prelude;
|
||||
|
|
|
|||
|
|
@ -94,15 +94,37 @@
|
|||
//! }
|
||||
//! ```
|
||||
|
||||
use crate::{config, trace, LazyStatic};
|
||||
|
||||
use unic_langid::LanguageIdentifier;
|
||||
|
||||
pub use fluent_templates;
|
||||
pub use fluent_templates::fluent_bundle::FluentValue;
|
||||
pub use fluent_templates::{static_loader as static_locale, Loader as Locale};
|
||||
|
||||
/// Almacena el Identificador de Idioma Unicode
|
||||
/// ([Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier))
|
||||
/// para la aplicación, obtenido de `SETTINGS.app.language`.
|
||||
pub static LANGID: LazyStatic<LanguageIdentifier> =
|
||||
LazyStatic::new(|| match config::SETTINGS.app.language.parse() {
|
||||
Ok(language) => language,
|
||||
Err(_) => {
|
||||
trace::warn!(
|
||||
"{}, {} \"{}\"! {}, {}",
|
||||
"Failed to parse language",
|
||||
"unrecognized Unicode Language Identifier",
|
||||
config::SETTINGS.app.language,
|
||||
"Using \"en-US\"",
|
||||
"check the settings file",
|
||||
);
|
||||
"en-US".parse().unwrap()
|
||||
}
|
||||
});
|
||||
|
||||
#[macro_export]
|
||||
/// Permite integrar fácilmente localización en temas, módulos y componentes.
|
||||
macro_rules! pub_locale {
|
||||
( $dir_locales:literal $(, $core_locales:literal)? ) => {
|
||||
use $crate::app;
|
||||
use $crate::locale::*;
|
||||
|
||||
static_locale! {
|
||||
|
|
@ -118,7 +140,7 @@ macro_rules! pub_locale {
|
|||
|
||||
#[allow(dead_code)]
|
||||
fn l(key: &str) -> String {
|
||||
LOCALES.lookup(&app::locale::LANGID, key).unwrap_or(key.to_string())
|
||||
LOCALES.lookup(&LANGID, key).unwrap_or(key.to_string())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -126,7 +148,7 @@ macro_rules! pub_locale {
|
|||
key: &str,
|
||||
args: &std::collections::HashMap<String, FluentValue>
|
||||
) -> String {
|
||||
LOCALES.lookup_with_args(&app::locale::LANGID, key, args).unwrap_or(key.to_string())
|
||||
LOCALES.lookup_with_args(&LANGID, key, args).unwrap_or(key.to_string())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -135,7 +157,7 @@ macro_rules! pub_locale {
|
|||
args: &std::collections::HashMap<String, FluentValue>
|
||||
) -> $crate::html::PreEscaped<String> {
|
||||
$crate::html::PreEscaped(
|
||||
LOCALES.lookup_with_args(&app::locale::LANGID, key, args).unwrap_or(key.to_string())
|
||||
LOCALES.lookup_with_args(&LANGID, key, args).unwrap_or(key.to_string())
|
||||
)
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -19,16 +19,18 @@ pub use crate::html::*;
|
|||
#[cfg(feature = "database")]
|
||||
pub use crate::{db, db::*, migration_item, pub_migration};
|
||||
|
||||
pub use crate::app;
|
||||
pub use crate::app::application::Application;
|
||||
pub use crate::app::fatal_error::FatalError;
|
||||
pub use crate::app::HttpMessage;
|
||||
|
||||
pub use crate::core::{component::*, hook::*, module::*, theme::*};
|
||||
|
||||
pub use crate::{hook_action, hook_before_render_component};
|
||||
|
||||
pub use crate::server;
|
||||
pub use crate::server::HttpMessage;
|
||||
|
||||
pub use crate::response::page::*;
|
||||
pub use crate::response::FatalError;
|
||||
pub use crate::response::ResponseError;
|
||||
|
||||
pub use crate::base::component::*;
|
||||
|
||||
pub use crate::app;
|
||||
pub use crate::app::application::Application;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
pub use actix_web::ResponseError;
|
||||
|
||||
pub mod page;
|
||||
|
||||
mod fatal_error;
|
||||
pub use fatal_error::FatalError;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::app::http::{header::ContentType, StatusCode};
|
||||
use crate::app::HttpResponse;
|
||||
use crate::server::http::{header::ContentType, StatusCode};
|
||||
use crate::server::HttpResponse;
|
||||
use crate::response::{page::Page, ResponseError};
|
||||
|
||||
use std::fmt;
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
use super::{BeforeRenderPageHook, PageContext, PageOp, ResultPage, HOOK_BEFORE_RENDER_PAGE};
|
||||
|
||||
use crate::app::fatal_error::FatalError;
|
||||
use crate::core::component::*;
|
||||
use crate::core::hook::{action_ref, run_actions};
|
||||
use crate::html::{html, AttributeValue, Classes, ClassesOp, Markup, DOCTYPE};
|
||||
use crate::response::FatalError;
|
||||
use crate::{config, trace, LazyStatic};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
|
|
|||
5
pagetop/src/server.rs
Normal file
5
pagetop/src/server.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
pub use actix_web::{
|
||||
http, web, App, HttpMessage, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||
};
|
||||
pub use actix_web_files::Files as ActixFiles;
|
||||
pub use actix_web_static_files::ResourceFiles;
|
||||
|
|
@ -171,10 +171,10 @@ macro_rules! configure_service_for_static_files {
|
|||
( $cfg:ident, $dir:expr, $embed:ident ) => {{
|
||||
let static_files = &$crate::config::SETTINGS.dev.static_files;
|
||||
if static_files.is_empty() {
|
||||
$cfg.service($crate::app::ResourceFiles::new($dir, $embed()));
|
||||
$cfg.service($crate::server::ResourceFiles::new($dir, $embed()));
|
||||
} else {
|
||||
$cfg.service(
|
||||
$crate::app::ActixFiles::new($dir, $crate::concat_string!(static_files, $dir))
|
||||
$crate::server::ActixFiles::new($dir, $crate::concat_string!(static_files, $dir))
|
||||
.show_files_listing(),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue