    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104

    use std::sync::Arc;
    use axum::{
        //extract::{self,Request,Query},
        http::{
    //        StatusCode,
            Uri,
            header::HeaderMap,// HeaderValue},
        },
    //    Json,
        routing::get,
        Extension,
        response::{IntoResponse,Response},
    //    http::Request, handler::HandlerWithoutStateExt, http::StatusCode, routing::get, Router,
        Router,
    };
    use tower_cookies::Cookies;

    use crate::{
        route,
        defs::{
            AppDBs,
            ReqHandler,
            ReqHeaderMap,
        },
        handlers::get_auth_state,
    };

    pub fn pages_router_handlers() -> Router {
        async fn page_handler(
            //req: Request,
            header: HeaderMap,
            uri: Uri,
            Extension(app_dbs): Extension<Arc<AppDBs>>,
            // Extension(random): Extension<Random>,
            Extension(cookies): Extension<Cookies>,
            axum::extract::Path(name): axum::extract::Path<String>,
            //auth_state: AuthState,
        ) -> Response {
            // if let Some(cookie_value) = get_cookie(&req) {
            //     println!("cookie_value: {}",&cookie_value.to_string());
            //     // TODO check value
            // }
            //let  has_cookie: bool;
            // dbg!(&auth_state.session);
            // if auth_state.user.is_none() {
            //     eprintln!("No user found in session");
            // }
            let auth_state = get_auth_state(true, &cookies, &app_dbs).await;
            //    has_cookie=true;
                // TODO check value
                // let sid: String = format!("{}",&auth_state.sid());
                // println!("auth_sid: {}",&sid);
                // println!("id: {}",&auth_state.id());
                // println!("is_expired: {}",&auth_state.ses_expired());
                // println!("is_destroyed: {}",&auth_state.ses_destroyed());
                // println!("validate: {}",&auth_state.ses_validate());
                // let _u = &auth_state.user;
            let mut req_handler = ReqHandler::new(
                ReqHeaderMap::new(header, &format!("{}",&uri.path().to_string())),
                &app_dbs,
                &uri,
                &auth_state,
                "root_handler"
            );
            // if let Some(a) = auth_state {
            //     println!("Auth State from root");
            // }
            // let uri_path = format!("{}",&uri.path().to_string());
            // dbg!("uri: {}",&uri_path);
            req_handler.prepare_response();
            req_handler.context.insert("with_menu", "1");
            let tpl_name = if name.contains(".html") {
                name.replace(".html","")
            } else {
                name
            };
            let result = req_handler.render_template(&format!("pages/{}.html.j2",&tpl_name), &tpl_name);
            // if !has_cookie {
            //     let mut u128_pool = [0u8; 16];
            //     match random.lock() {
            //         Ok(mut r) => r.fill_bytes(&mut u128_pool),
            //         Err(e) => println!("Error random: {}",e),
            //     }
            //     let session_token = u128::from_le_bytes(u128_pool).to_string();
            //     let user_data = format!("{}|{}","jesus" ,"admin,dev");
            //     let result_store = SessionStoreDB::store_session_data(&session_token,&user_data,&app_dbs).await;
            //     println!("Rest store: {}",&result_store);
            //     let cookie = Cookie::build(SESSION_COOKIE_NAME, session_token.to_owned())
            //     //  .domain(domain)
            //         .path("/")
            //         .secure(true)
            //         .http_only(true)
            //         .finish();
            //     cookies.remove(Cookie::new(SESSION_COOKIE_NAME, ""));
            //     cookies.add(cookie);
            // }
            (
                req_handler.req_header.header,
                result.to_owned()
            ).into_response()
        //     "Hello, World!"
        }
        route("/page/:name", get(page_handler))
    }