Skip to main content

kalgan/
lib.rs

1//! # Kalgan - A Rust Framework for Web Developers
2//!
3//! Welcome to Kalgan API documentation.
4//!
5//! Please, see the [Official Site](https://www.markdownguide.org) for features and getting started.
6#[macro_use]
7extern crate lazy_static;
8
9pub mod handler {
10    mod asset;
11    pub(crate) mod controller;
12    mod error;
13    pub mod middleware;
14    pub(crate) mod resolver;
15}
16pub mod http {
17    pub mod request;
18    pub mod response;
19}
20pub mod service {
21    #[cfg(feature = "session")]
22    pub mod csrf;
23    #[cfg(feature = "sqlx")]
24    pub mod db;
25    #[cfg(feature = "kalgan_i18n")]
26    pub mod i18n;
27    #[cfg(feature = "mailer")]
28    pub mod mailer;
29    #[cfg(feature = "services")]
30    pub mod password;
31    #[cfg(feature = "services")]
32    pub mod token;
33    pub mod url;
34}
35mod server {
36    pub(crate) mod tcp_listener;
37    pub(crate) mod thread_pool;
38    pub(crate) mod worker;
39}
40pub mod storage {
41    pub mod cookie;
42    #[cfg(feature = "session")]
43    pub mod session;
44}
45pub mod settings;
46#[cfg(feature = "tera")]
47pub mod template;
48use crate::{
49    handler::middleware::Outcome,
50    http::{request::Request, response::Response},
51    server::tcp_listener,
52};
53#[cfg(feature = "cache")]
54pub use kalgan_cache;
55use chrono::{offset::Utc, NaiveTime};
56use log::trace;
57use std::{collections::HashMap, sync::Mutex};
58
59/// Stores the current time used to refresh the config parameters and version static files in templates.
60struct Refresh {
61    time: NaiveTime,
62}
63#[cfg(feature = "tera")]
64/// Stores the custom tera configuration.
65struct TeraConfig {
66    config: Option<fn(&mut tera::Tera) -> &mut tera::Tera>,
67}
68lazy_static! {
69    pub(crate) static ref CONFIG: Mutex<kalgan_config::Config> = Mutex::new(kalgan_config::Config{ collection: HashMap::new() });
70    pub(crate) static ref ROUTES: Mutex<kalgan_router::Router> = Mutex::new(kalgan_router::Router{ collection: Vec::new() });
71    pub(crate) static ref REFRESH: Mutex<Refresh> = Mutex::new(Refresh { time: Utc::now().time() });
72}
73#[cfg(feature = "tera")]
74lazy_static! {
75    pub(crate) static ref TEMPLATES: tera::Tera = {
76        template::get_tera(&settings::get_string("tera.path").unwrap()).unwrap()
77    };
78    pub(crate) static ref TERA_CONFIG: Mutex<TeraConfig> = Mutex::new(TeraConfig { config: None });
79}
80#[cfg(feature = "kalgan_i18n")]
81lazy_static! {
82    pub(crate) static ref MESSAGES: Mutex<kalgan_i18n::Messages> = Mutex::new(kalgan_i18n::Messages{ collection: HashMap::new() });
83}
84/// Parses configuration parameters, routes and translation messages and finally starts the http server.
85pub fn run(
86    settings_file_path: &'static str,
87    controller: fn(&Request, &str) -> Result<Response, String>,
88    middleware: Option<fn(&mut Request, &str) -> Result<Outcome, String>>,
89) {
90    set_config(settings_file_path);
91    set_routes();
92    #[cfg(feature = "kalgan_i18n")]
93    {
94        set_messages();
95    }
96    tcp_listener::start(settings_file_path, controller, middleware);
97}
98#[cfg(feature = "tera")]
99/// Sets custom tera configuration.
100pub fn set_tera_config(tera_config: fn(&mut tera::Tera) -> &mut tera::Tera) {
101    let mut tera_static = TERA_CONFIG.lock().unwrap();
102    tera_static.config = Some(tera_config);
103    std::mem::drop(tera_static);
104}
105/// Parses settings parameter files.
106pub(crate) fn set_config(settings_file_path: &str) {
107    let mut config_static = CONFIG.lock().unwrap();
108    config_static.collection = kalgan_config::Config::new(&settings_file_path).collection;
109    trace!("{:?}", &config_static);
110    std::mem::drop(config_static);
111}
112/// Parses routing files.
113fn set_routes() {
114    let mut route_static = ROUTES.lock().unwrap();
115    route_static.collection =
116        kalgan_router::Router::new(&settings::get_string("router.path").unwrap()).collection;
117    trace!("{:?}", &route_static);
118    std::mem::drop(route_static);
119}
120#[cfg(feature = "kalgan_i18n")]
121/// Parses translation messages files.
122fn set_messages() {
123    let mut messages_static = MESSAGES.lock().unwrap();
124    messages_static.collection =
125        kalgan_i18n::Messages::new(&settings::get_string("i18n.path").unwrap()).collection;
126    trace!("{:?}", &messages_static);
127    std::mem::drop(messages_static);
128}
129#[cfg(feature = "test")]
130/// Parses settings parameter files used in testing.
131pub fn mock_settings(settings_file_path: &str) {
132    set_config(settings_file_path);
133}
134#[cfg(feature = "test")]
135/// Parses routing files used in testing.
136pub fn mock_routes() {
137    set_routes();
138}
139#[cfg(feature = "test")]
140/// Parses translation messages files used in testing.
141pub fn mock_i18n() {
142    set_messages();
143}
144#[cfg(test)]
145pub mod tests {
146    use super::*;
147    pub fn set_config() {
148        mock_settings("tests/mock/settings.yaml");
149    }
150}