From 671f850c4fb72941c111edb6dff1030839b0dd55 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Fri, 30 Dec 2022 00:45:57 +0000 Subject: Comments are basically functional. Added example config. --- comments/src/main.rs | 45 +++++++++++++++++++++------------------- comments/templates/comments.html | 10 ++++----- comments/templates/form.html | 6 +++--- 3 files changed, 32 insertions(+), 29 deletions(-) (limited to 'comments') diff --git a/comments/src/main.rs b/comments/src/main.rs index 95cd9ae..2bc11fa 100644 --- a/comments/src/main.rs +++ b/comments/src/main.rs @@ -10,7 +10,7 @@ use askama::Template; use axum::{ extract::{Query, Form, State}, - response::Redirect, + response::{Redirect,Html}, http::StatusCode, routing::get, Router, @@ -29,14 +29,17 @@ use std::{net::SocketAddr, time::Duration}; #[derive(Clone)] struct Ctx { pool: PgPool, + base_path: String, } #[tokio::main] async fn main() { + let base_path = std::env::var("BASE_PATH") + .unwrap_or_else(|_| "/api".to_string()); + let db_connection_str = std::env::var("DATABASE_URL") .unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string()); - // setup connection pool let pool = PgPoolOptions::new() .max_connections(5) .connect_timeout(Duration::from_secs(3)) @@ -48,22 +51,21 @@ async fn main() { .await .expect("failed to run migrations"); - let ctx = Ctx {pool}; + let ctx = Ctx {pool, base_path: base_path.clone()}; - // build our application with some routes let app = Router::new() - .route( - "/form", - get(get_form), - ) - .route( - "/comment", - get(get_comments).post(post_comments), - ) - .with_state(ctx); + .nest(&base_path, Router::new() + .route( + "/form", + get(get_form), + ) + .route( + "/comment", + get(get_comments).post(post_comments), + ) + .with_state(ctx)); - // run it with hyper - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let addr = SocketAddr::from(([127, 0, 0, 1], 5678)); axum::Server::bind(&addr) .serve(app.into_make_service()) .await @@ -81,19 +83,20 @@ struct CommentForm { url: String, capcha_question: String, capcha_id: Uuid, + base_path: String, } async fn get_form( State(ctx): State, Query(uq): Query -) -> Result { +) -> Result, (StatusCode, String)> { let capcha = sqlx::query!("select id, question from capchas order by random() limit 1") .fetch_one(&ctx.pool) .await .map_err(internal_error)?; - let c = CommentForm{url: uq.url, capcha_question: capcha.question, capcha_id: capcha.id}; + let c = CommentForm{url: uq.url, capcha_question: capcha.question, capcha_id: capcha.id, base_path: ctx.base_path}; let res = c.render().map_err(internal_error)?; - Ok(res) + Ok(Html(res)) } #[derive(Template)] @@ -109,8 +112,8 @@ struct Comment { async fn get_comments( State(ctx): State, - Query(uq): Query) -> Result { - let comments = sqlx::query!("select author,comment,ts from comments where url = $1", uq.url) + Query(uq): Query) -> Result, (StatusCode,String)> { + let comments = sqlx::query!("select author,comment,ts from comments where url = $1 order by ts", uq.url) .fetch_all(&ctx.pool) .await .map_err(internal_error)?; @@ -123,7 +126,7 @@ async fn get_comments( }).collect(); let c = Comments{comments: render_comments}; let res = c.render().map_err(internal_error)?; - Ok(res) + Ok(Html(res)) } #[derive(Deserialize)] diff --git a/comments/templates/comments.html b/comments/templates/comments.html index 72c4a46..e64ba1c 100644 --- a/comments/templates/comments.html +++ b/comments/templates/comments.html @@ -1,9 +1,9 @@ -
    +
      {% for comment in comments -%} -
    • - {{ comment.author }} -

      {{ comment.comment }}

      - {{ comment.ts }} +
    • + {{ comment.author }} +

      {{ comment.comment }}

      + {{ comment.ts }}
    • {%- endfor %}
    diff --git a/comments/templates/form.html b/comments/templates/form.html index 09fc81f..1f1692c 100644 --- a/comments/templates/form.html +++ b/comments/templates/form.html @@ -1,11 +1,11 @@ -
    +




    - - +
    +

    -- cgit v1.2.3-ZIG