Añade configuración y conexión a la base de datos
This commit is contained in:
parent
b6dd473578
commit
76785af4dc
13 changed files with 210 additions and 83 deletions
138
src/core/server/app.rs
Normal file
138
src/core/server/app.rs
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
use crate::{Lazy, base, locale, trace};
|
||||
use crate::config::SETTINGS;
|
||||
use crate::core::{Server, global, server};
|
||||
use crate::core::theme::register_theme;
|
||||
use crate::core::module::register_module;
|
||||
|
||||
use std::io::Error;
|
||||
use actix_web::middleware::normalize::{NormalizePath, TrailingSlash};
|
||||
|
||||
pub struct Application {
|
||||
server: Server,
|
||||
}
|
||||
|
||||
impl Application {
|
||||
pub async fn build(bootstrap: Option<fn()>) -> Result<Self, Error> {
|
||||
// Imprime rótulo (opcional) de bienvenida.
|
||||
if SETTINGS.app.startup_banner.to_lowercase() != "off" {
|
||||
let figfont = figlet_rs::FIGfont::from_content(
|
||||
match SETTINGS.app.startup_banner.to_lowercase().as_str() {
|
||||
"slant" => include_str!("figfonts/slant.flf"),
|
||||
"small" => include_str!("figfonts/small.flf"),
|
||||
"speed" => include_str!("figfonts/speed.flf"),
|
||||
"starwars" => include_str!("figfonts/starwars.flf"),
|
||||
_ => {
|
||||
println!(
|
||||
"FIGfont \"{}\" not found for banner. {}. {}.",
|
||||
SETTINGS.app.startup_banner,
|
||||
"Using \"Small\"",
|
||||
"Check the settings file",
|
||||
);
|
||||
include_str!("figfonts/small.flf")
|
||||
}
|
||||
}
|
||||
).unwrap();
|
||||
println!("\n{} {}\n\n Powered by PageTop {}\n",
|
||||
figfont.convert(&SETTINGS.app.name).unwrap(),
|
||||
&SETTINGS.app.description,
|
||||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
}
|
||||
|
||||
// Inicia registro de trazas y eventos.
|
||||
Lazy::force(&server::tracing::TRACING);
|
||||
|
||||
// Valida el identificador de idioma.
|
||||
Lazy::force(&locale::LANGID);
|
||||
|
||||
// Inicializa la conexión con la base de datos.
|
||||
trace::info!(
|
||||
"Connecting to database \"{}\" with a pool of {} connections.",
|
||||
&SETTINGS.database.db_name,
|
||||
&SETTINGS.database.max_pool_size
|
||||
);
|
||||
|
||||
#[cfg(any(feature = "default", feature = "mysql"))]
|
||||
let db_uri = format!(
|
||||
"mysql://{}/{}",
|
||||
&SETTINGS.database.db_host,
|
||||
&SETTINGS.database.db_name
|
||||
);
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
let db_uri = format!(
|
||||
"postgres://{}/{}",
|
||||
&SETTINGS.database.db_host,
|
||||
&SETTINGS.database.db_name
|
||||
);
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
let db_uri = format!("sqlite://{}", &SETTINGS.database.db_name);
|
||||
|
||||
let mut uri = url::Url::parse(&db_uri).unwrap();
|
||||
|
||||
// https://github.com/launchbadge/sqlx/issues/1624
|
||||
|
||||
#[cfg(not(feature = "sqlite"))]
|
||||
uri.set_username(&SETTINGS.database.db_user.as_str()).unwrap();
|
||||
|
||||
#[cfg(not(feature = "sqlite"))]
|
||||
uri.set_password(Some(&SETTINGS.database.db_pass.as_str())).unwrap();
|
||||
|
||||
#[cfg(not(feature = "sqlite"))]
|
||||
if SETTINGS.database.db_port != 0 {
|
||||
uri.set_port(Some(SETTINGS.database.db_port)).unwrap();
|
||||
}
|
||||
|
||||
let mut db_options = sea_orm::ConnectOptions::new(uri.to_string());
|
||||
db_options.max_connections(SETTINGS.database.max_pool_size);
|
||||
|
||||
let mut db_conn = server::dbconn::DBCONN.write().unwrap();
|
||||
*db_conn = Some(
|
||||
sea_orm::Database::connect::<sea_orm::ConnectOptions>(
|
||||
db_options.into()
|
||||
)
|
||||
.await
|
||||
.expect("Failed to connect to database")
|
||||
);
|
||||
|
||||
// Registra los temas predefinidos.
|
||||
register_theme(&base::theme::aliner::AlinerTheme);
|
||||
register_theme(&base::theme::minimal::MinimalTheme);
|
||||
register_theme(&base::theme::bootsier::BootsierTheme);
|
||||
|
||||
// Registra los módulos predeterminados.
|
||||
register_module(&base::module::admin::AdminModule);
|
||||
register_module(&base::module::user::UserModule);
|
||||
|
||||
// Ejecuta la función de inicio de la aplicación.
|
||||
if bootstrap != None {
|
||||
trace::debug!("Calling application bootstrap");
|
||||
let _ = &(bootstrap.unwrap())();
|
||||
}
|
||||
|
||||
// Registra el módulo para la página de inicio de PageTop.
|
||||
// Al ser el último, puede sobrecargarse con la función de inicio.
|
||||
register_module(&base::module::homepage::HomepageModule);
|
||||
|
||||
// Prepara el servidor web.
|
||||
let server = server::HttpServer::new(|| {
|
||||
server::App::new()
|
||||
.wrap(tracing_actix_web::TracingLogger)
|
||||
.wrap(NormalizePath::new(TrailingSlash::Trim))
|
||||
.configure(&global::themes)
|
||||
.configure(&global::modules)
|
||||
})
|
||||
.bind(format!("{}:{}",
|
||||
&SETTINGS.webserver.bind_address,
|
||||
&SETTINGS.webserver.bind_port
|
||||
))?
|
||||
.run();
|
||||
|
||||
Ok(Self { server })
|
||||
}
|
||||
|
||||
pub fn run(self) -> Result<Server, Error> {
|
||||
Ok(self.server)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue