Add personal page

O01eg
May 5, 2023, 9:29 AM
MCF5COULF5S6UFLIN2LN652D2ROAJGEBZN4Y3TYAQDV4OI26Y7FQC

Dependencies

  • [2] LTQCLSBU Split database usage in pages
  • [3] FUCFD4UV Add log in and log out support
  • [4] H6GGDVHW Show auth info in reset password page
  • [5] WW3KRXX6 Add page for reset game password
  • [6] WEVOADLS Require to accept cookies policy for log in
  • [7] WVHXYKCV Add postgresql pools
  • [8] HZDCKIXQ Use constants for templates
  • [9] 6CFNBL5L Add headers for better security
  • [10] YDWTHWAI Show form to revoke delegation
  • [11] AAUEQMCP Add common header block
  • [*] 65A3LIWU Use handlebars to render index
  • [*] EVP2FSBH Split index page
  • [*] 4MZ4VIR7 Initial commit
  • [*] WLWTNO4Y Create form to request game password change link
  • [*] DNFB7TNI Add new pages and links to slow games and reset game password

Change contents

  • replacement in src/templates/sub/header.html at line 6
    [3.34][3.34:77]()
    Hello, {{ common_auth_info.user }}
    [3.34]
    [3.77]
    Hello, <a href="my.html">{{ common_auth_info.user }}</a>
  • file addition: my.html (----------)
    [13.12]
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <link rel="alternate" type="application/rss+xml" href="static/rss.xml" title="Multiplayer FreeOrion server news" />
    <link rel="stylesheet" type="text/css" href="static/style.css" />
    <title>Welcome to public multiplayer FreeOrion server!</title>
    </head>
    <body>
    {{> header}}
    <div class="content">
    <p>Your games:
    <ul>
    {{#each games as |game|}}
    <li class=
    {{#if game.status}}
    {{#if (eq game.status "started")}}
    "game-status-started"
    {{else}}
    "game-status-finished"
    {{/if}}
    {{else}}
    "game-status-prepared"
    {{/if}}
    ><a href="slow-game-{{ game.gameuidenc }}.html">{{ game.gameuid }}</a></li>
    {{/each}}
    </ul>
    </p>
    </div>
    {{> footer}}
    </body>
    </html>
  • edit in src/pages/reset_game_pwd.rs at line 4
    [5.664][4.64:141]()
    use crate::pages::{insert_security_headers, request_to_jar, CommonAuthInfo};
  • edit in src/pages/reset_game_pwd.rs at line 5
    [5.483]
    [2.429]
    use crate::pages::{insert_security_headers, request_to_jar, CommonAuthInfo};
  • file addition: my.rs (----------)
    [14.17]
    use actix_web::http::header;
    use actix_web::{web, HttpRequest, HttpResponse};
    use crate::pages::templates::MY;
    use crate::pages::{insert_security_headers, request_to_jar, CommonAuthInfo};
    use crate::{DataBaseRo, WebData};
    #[derive(serde_derive::Serialize)]
    struct PageData {
    common_auth_info: CommonAuthInfo,
    games: Vec<GameData>,
    }
    #[derive(serde_derive::Serialize)]
    struct GameData {
    gameuid: String,
    gameuidenc: String,
    status: Option<String>,
    }
    pub async fn my(
    request: HttpRequest,
    data: web::Data<WebData<'_>>,
    data_ro: web::Data<DataBaseRo>,
    ) -> HttpResponse {
    let jar = request_to_jar(request);
    let u = jar
    .private(&data.cookies_key)
    .get("auth")
    .map(|x| x.value().to_string());
    if let Some(user) = u {
    let dbclient = match data_ro.0.get().await {
    Ok(client) => client,
    Err(e) => {
    log::error!("{}", e);
    return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
    }
    };
    let stmt = match dbclient
    .prepare("select g.game_uid, g.status::text from games.games g inner join games.players p on g.game_uid = p.game_uid where p.player_name = $1 order by g.game_uid;")
    .await
    {
    Ok(stmt) => stmt,
    Err(e) => {
    log::error!("{}", e);
    return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
    }
    };
    let rows = match dbclient.query(&stmt, &[&user]).await {
    Ok(rows) => rows,
    Err(e) => {
    log::error!("{}", e);
    return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
    }
    };
    let mut games = Vec::with_capacity(rows.len());
    for row in rows {
    let gameuid = row.get::<_, &str>(0);
    let status = row.get::<_, Option<&str>>(1);
    games.push(GameData {
    gameuid: gameuid.to_string(),
    gameuidenc: pct_str::PctString::encode(gameuid.chars(), pct_str::URIReserved)
    .into_string(),
    status: status.map(str::to_string),
    });
    }
    let body = match data.handlebars.render(
    MY,
    &PageData {
    common_auth_info: CommonAuthInfo { user: Some(user) },
    games,
    },
    ) {
    Ok(b) => b,
    Err(e) => {
    log::error!("Render index error: {}", e);
    return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
    }
    };
    insert_security_headers(HttpResponse::Ok()).body(body)
    } else {
    HttpResponse::Found()
    .append_header((header::LOCATION, "login.html"))
    .finish()
    }
    }
  • edit in src/pages/mod.rs at line 12
    [3.1362]
    [5.1060]
    pub mod my;
  • edit in src/pages/mod.rs at line 21
    [5.1148]
    [5.1148]
    pub const MY: &str = "my";
  • replacement in src/pages/log_in.rs at line 128
    [3.5782][3.5782:5839]()
    .append_header((header::LOCATION, "index.html"))
    [3.5782]
    [3.5839]
    .append_header((header::LOCATION, "my.html"))
  • edit in src/main.rs at line 18
    [3.6813]
    [16.1800]
    use pages::my::my;
  • edit in src/main.rs at line 157
    [17.2294]
    [17.2294]
    .register_template_string(templates::MY, include_str!("templates/my.html"))
    .expect("my template");
    handlebars
  • edit in src/main.rs at line 278
    [5.1429]
    [17.2470]
    .route("/my.html", web::get().to(my))