diff options
author | Martin Ashby <martin@ashbysoft.com> | 2022-12-30 00:45:57 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2022-12-30 00:45:57 +0000 |
commit | 671f850c4fb72941c111edb6dff1030839b0dd55 (patch) | |
tree | e2e066891c85aba3eff5bb76c8e0d81a658a2cb9 /comments/src | |
parent | 9be5acc275711d0a6a717ac5564a86185ec1d613 (diff) | |
download | mfashby.net-671f850c4fb72941c111edb6dff1030839b0dd55.tar.gz mfashby.net-671f850c4fb72941c111edb6dff1030839b0dd55.tar.bz2 mfashby.net-671f850c4fb72941c111edb6dff1030839b0dd55.tar.xz mfashby.net-671f850c4fb72941c111edb6dff1030839b0dd55.zip |
Comments are basically functional.
Added example config.
Diffstat (limited to 'comments/src')
-rw-r--r-- | comments/src/main.rs | 45 |
1 files changed, 24 insertions, 21 deletions
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<Ctx>, Query(uq): Query<UrlQuery> -) -> Result<String, (StatusCode, String)> { +) -> Result<Html<String>, (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<Ctx>, - Query(uq): Query<UrlQuery>) -> Result<String, (StatusCode,String)> { - let comments = sqlx::query!("select author,comment,ts from comments where url = $1", uq.url) + Query(uq): Query<UrlQuery>) -> Result<Html<String>, (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)] |