|
| 1 | +use diesel::{ConnectionError, ConnectionResult}; |
| 2 | +use futures_util::future::BoxFuture; |
| 3 | +use futures_util::FutureExt; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +use axum::{ |
| 7 | + extract::{Path, State}, |
| 8 | + response::Json, |
| 9 | + routing::get, |
| 10 | + Router, |
| 11 | +}; |
| 12 | +use bb8::Pool; |
| 13 | +use diesel::prelude::*; |
| 14 | +use diesel_async::{pooled_connection::AsyncDieselConnectionManager, AsyncPgConnection, RunQueryDsl}; |
| 15 | +use lambda_http::{http::StatusCode, run, Error}; |
| 16 | +use serde::{Deserialize, Serialize}; |
| 17 | + |
| 18 | +table! { |
| 19 | + posts (id) { |
| 20 | + id -> Integer, |
| 21 | + title -> Text, |
| 22 | + content -> Text, |
| 23 | + published -> Bool, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Default, Queryable, Selectable, Serialize)] |
| 28 | +struct Post { |
| 29 | + id: i32, |
| 30 | + title: String, |
| 31 | + content: String, |
| 32 | + published: bool, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Deserialize, Insertable)] |
| 36 | +#[diesel(table_name = posts)] |
| 37 | +struct NewPost { |
| 38 | + title: String, |
| 39 | + content: String, |
| 40 | + published: bool, |
| 41 | +} |
| 42 | + |
| 43 | +type AsyncPool = Pool<AsyncDieselConnectionManager<AsyncPgConnection>>; |
| 44 | +type ServerError = (StatusCode, String); |
| 45 | + |
| 46 | +async fn create_post(State(pool): State<AsyncPool>, Json(post): Json<NewPost>) -> Result<Json<Post>, ServerError> { |
| 47 | + let mut conn = pool.get().await.map_err(internal_server_error)?; |
| 48 | + |
| 49 | + let post = diesel::insert_into(posts::table) |
| 50 | + .values(post) |
| 51 | + .returning(Post::as_returning()) |
| 52 | + .get_result(&mut conn) |
| 53 | + .await |
| 54 | + .map_err(internal_server_error)?; |
| 55 | + |
| 56 | + Ok(Json(post)) |
| 57 | +} |
| 58 | + |
| 59 | +async fn list_posts(State(pool): State<AsyncPool>) -> Result<Json<Vec<Post>>, ServerError> { |
| 60 | + let mut conn = pool.get().await.map_err(internal_server_error)?; |
| 61 | + |
| 62 | + let posts = posts::table |
| 63 | + .filter(posts::dsl::published.eq(true)) |
| 64 | + .load(&mut conn) |
| 65 | + .await |
| 66 | + .map_err(internal_server_error)?; |
| 67 | + |
| 68 | + Ok(Json(posts)) |
| 69 | +} |
| 70 | + |
| 71 | +async fn get_post(State(pool): State<AsyncPool>, Path(post_id): Path<i32>) -> Result<Json<Post>, ServerError> { |
| 72 | + let mut conn = pool.get().await.map_err(internal_server_error)?; |
| 73 | + |
| 74 | + let post = posts::table |
| 75 | + .find(post_id) |
| 76 | + .first(&mut conn) |
| 77 | + .await |
| 78 | + .map_err(internal_server_error)?; |
| 79 | + |
| 80 | + Ok(Json(post)) |
| 81 | +} |
| 82 | + |
| 83 | +async fn delete_post(State(pool): State<AsyncPool>, Path(post_id): Path<i32>) -> Result<(), ServerError> { |
| 84 | + let mut conn = pool.get().await.map_err(internal_server_error)?; |
| 85 | + |
| 86 | + diesel::delete(posts::table.find(post_id)) |
| 87 | + .execute(&mut conn) |
| 88 | + .await |
| 89 | + .map_err(internal_server_error)?; |
| 90 | + |
| 91 | + Ok(()) |
| 92 | +} |
| 93 | + |
| 94 | +fn internal_server_error<E: std::error::Error>(err: E) -> ServerError { |
| 95 | + (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()) |
| 96 | +} |
| 97 | + |
| 98 | +#[tokio::main] |
| 99 | +async fn main() -> Result<(), Error> { |
| 100 | + // required to enable CloudWatch error logging by the runtime |
| 101 | + tracing_subscriber::fmt() |
| 102 | + .with_max_level(tracing::Level::INFO) |
| 103 | + // disable printing the name of the module in every log line. |
| 104 | + .with_target(false) |
| 105 | + // disabling time is handy because CloudWatch will add the ingestion time. |
| 106 | + .without_time() |
| 107 | + .init(); |
| 108 | + |
| 109 | + // Set up the database connection |
| 110 | + // Format for DATABASE_URL=postgres://your_username:your_password@your_host:5432/your_db?sslmode=require |
| 111 | + let db_url = std::env::var("DATABASE_URL").expect("Env var `DATABASE_URL` not set"); |
| 112 | + |
| 113 | + let mgr = AsyncDieselConnectionManager::<AsyncPgConnection>::new_with_setup( |
| 114 | + db_url, |
| 115 | + establish_connection, |
| 116 | + ); |
| 117 | + |
| 118 | + let pool = Pool::builder() |
| 119 | + .max_size(10) |
| 120 | + .min_idle(Some(5)) |
| 121 | + .max_lifetime(Some(Duration::from_secs(60 * 60 * 24))) |
| 122 | + .idle_timeout(Some(Duration::from_secs(60 * 2))) |
| 123 | + .build(mgr) |
| 124 | + .await?; |
| 125 | + |
| 126 | + // Set up the API routes |
| 127 | + let posts_api = Router::new() |
| 128 | + .route("/", get(list_posts).post(create_post)) |
| 129 | + .route("/:id", get(get_post).delete(delete_post)) |
| 130 | + .route("/get", get(list_posts)) |
| 131 | + .route("/get/:id", get(get_post)); |
| 132 | + let app = Router::new().nest("/posts", posts_api).with_state(pool); |
| 133 | + |
| 134 | + run(app).await |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConnection>> { |
| 139 | + let fut = async { |
| 140 | + // We first set up the way we want rustls to work. |
| 141 | + let rustls_config = rustls::ClientConfig::builder() |
| 142 | + .with_safe_defaults() |
| 143 | + .with_root_certificates(root_certs()) |
| 144 | + .with_no_client_auth(); |
| 145 | + let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config); |
| 146 | + let (client, conn) = tokio_postgres::connect(config, tls) |
| 147 | + .await |
| 148 | + .map_err(|e| ConnectionError::BadConnection(e.to_string()))?; |
| 149 | + tokio::spawn(async move { |
| 150 | + if let Err(e) = conn.await { |
| 151 | + eprintln!("Database connection: {e}"); |
| 152 | + } |
| 153 | + }); |
| 154 | + AsyncPgConnection::try_from(client).await |
| 155 | + }; |
| 156 | + fut.boxed() |
| 157 | +} |
| 158 | + |
| 159 | +fn root_certs() -> rustls::RootCertStore { |
| 160 | + let mut roots = rustls::RootCertStore::empty(); |
| 161 | + let certs = rustls_native_certs::load_native_certs().expect("Certs not loadable!"); |
| 162 | + let certs: Vec<_> = certs.into_iter().map(|cert| cert.0).collect(); |
| 163 | + roots.add_parsable_certificates(&certs); |
| 164 | + roots |
| 165 | +} |
0 commit comments