Skip to content

Instantly share code, notes, and snippets.

@ibraheemdev
Last active February 24, 2024 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibraheemdev/e97ecbefbcbe15692744111606fa5ba7 to your computer and use it in GitHub Desktop.
Save ibraheemdev/e97ecbefbcbe15692744111606fa5ba7 to your computer and use it in GitHub Desktop.
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use std::convert::Infallible;
use std::sync::Arc;
use tokio_postgres::{Client, NoTls};
#[tokio::main]
async fn main() {
let (client, connection) = tokio_postgres::connect("host=localhost user=postgres", NoTls)
.await
.unwrap();
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
let commands = vec![
"DROP TABLE IF EXISTS posts",
"CREATE TABLE posts ( id serial PRIMARY KEY, body text)",
];
for command in commands {
client.query(command, &[]).await.unwrap();
}
for i in 0..1000 {
client
.query(
"INSERT INTO posts (id, body) VALUES ($1, $2)",
&[&i, &LOREM_IPSUM],
)
.await
.unwrap();
}
let db = Arc::new(client);
let make_svc = make_service_fn(|_| {
let db = db.clone();
async move { Ok::<_, Infallible>(service_fn(move |req| handle(db.clone(), req))) }
});
let addr = ([127, 0, 0, 1], 3000).into();
Server::bind(&addr).serve(make_svc).await.unwrap();
}
async fn handle(db: Arc<Client>, _: Request<Body>) -> Result<Response<Body>, Infallible> {
let posts = db
.query("SELECT id, body FROM posts", &[])
.await
.unwrap()
.iter()
.map(|row| Post {
id: row.get(0),
body: row.get(1),
})
.collect::<Vec<_>>();
Ok(Response::new(Body::from(
FortunesTemplate { posts }.to_string(),
)))
}
pub struct Post {
pub id: i32,
pub body: String,
}
markup::define! {
FortunesTemplate(posts: Vec<Post>) {
{markup::doctype()}
html {
head {
title { "Fortunes" }
}
body {
table {
tr { th { "id" } th { "message" } }
@for post in posts {
tr {
td { {post.id} }
td { {post.body} }
}
}
}
}
}
}
}
const LOREM_IPSUM: &'static str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer sed risus fermentum, commodo orci vitae, ultrices diam. In a rutrum nisi. Maecenas tristique convallis magna, id porttitor enim bibendum vel. Sed condimentum lacus quis vehicula mollis. Nam suscipit tempus nisl, volutpat efficitur quam ultricies id. Vestibulum ut nibh volutpat, molestie nunc ac, tristique odio. Maecenas interdum nisl et nibh fermentum, ut aliquet metus pellentesque. Nam dictum dignissim consequat. Vestibulum dapibus arcu ut rhoncus facilisis. Vestibulum sed dignissim diam. Morbi ut leo eu nisi gravida hendrerit.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment