-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sam Bonill
authored and
Sam Bonill
committed
Oct 17, 2022
1 parent
bba30da
commit ee9c3e0
Showing
12 changed files
with
121 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::time::Duration; | ||
|
||
use graphul::http::StatusCode; | ||
use sqlx::{postgres::{PgPoolOptions}, Error, Pool, Postgres}; | ||
|
||
pub async fn db_con() -> Result<Pool<Postgres>, Error> { | ||
let db_uri = std::env::var("DATABASE_URL") | ||
.unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string()); | ||
|
||
PgPoolOptions::new() | ||
.max_connections(5) | ||
.connect_timeout(Duration::from_secs(3)) | ||
.connect(&db_uri) | ||
.await | ||
} | ||
|
||
/// Utility function for mapping any error into a `500 Internal Server Error` | ||
/// response. | ||
pub fn internal_error<E>(err: E) -> (StatusCode, String) | ||
where | ||
E: std::error::Error, | ||
{ | ||
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,29 @@ | ||
//! Example of application using <https://github.com/launchbadge/sqlx> | ||
//! | ||
//! Run with | ||
//! | ||
//! ```not_rust | ||
//! cd examples && cargo run -p example-sqlx-postgres | ||
//! ``` | ||
//! | ||
//! Test with curl: | ||
//! | ||
//! ```not_rust | ||
//! curl 127.0.0.1:3000 | ||
//! curl -X POST 127.0.0.1:3000 | ||
//! ``` | ||
mod db; | ||
|
||
use graphul::{ | ||
Context, | ||
http::{Methods, StatusCode}, | ||
Graphul, | ||
}; | ||
use sqlx::postgres::{PgPool, PgPoolOptions}; | ||
use std::time::Duration; | ||
|
||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
use sqlx::PgPool; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
tracing_subscriber::registry() | ||
.with(tracing_subscriber::EnvFilter::new( | ||
std::env::var("RUST_LOG").unwrap_or_else(|_| "example_tokio_postgres=debug".into()), | ||
)) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
|
||
let db_uri = std::env::var("DATABASE_URL") | ||
.unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string()); | ||
|
||
let pool = PgPoolOptions::new() | ||
.max_connections(5) | ||
.connect_timeout(Duration::from_secs(3)) | ||
.connect(&db_uri) | ||
.await | ||
.expect("can connect to database"); | ||
let pool = db::db_con().await.expect("can connect to database"); | ||
|
||
// build our application | ||
let mut app = Graphul::share_state(pool); | ||
|
||
app.get("/", using_connection_pool_extractor); | ||
app.get("/", using_connection_pool); | ||
|
||
app.run("127.0.0.1:3000").await; | ||
} | ||
|
||
// we can extract the connection pool with `State` or 'Context' | ||
async fn using_connection_pool_extractor(c: Context<PgPool>,) -> Result<String, (StatusCode, String)> { | ||
// we can extract the connection pool with `State` or `Context` | ||
async fn using_connection_pool(c: Context<PgPool>,) -> Result<String, (StatusCode, String)> { | ||
let pool = c.state(); | ||
sqlx::query_scalar("select 'hello world from pg'") | ||
.fetch_one(&*pool) | ||
.fetch_one(pool) | ||
.await | ||
.map_err(internal_error) | ||
} | ||
|
||
/// Utility function for mapping any error into a `500 Internal Server Error` | ||
/// response. | ||
fn internal_error<E>(err: E) -> (StatusCode, String) | ||
where | ||
E: std::error::Error, | ||
{ | ||
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string()) | ||
.map_err(db::internal_error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "example-templates" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
askama = "0.11" | ||
graphul = { path = "../../." } | ||
tokio = { version = "1.0", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use graphul::{ | ||
http::Methods, | ||
Context, Graphul, template::HtmlTemplate, | ||
}; | ||
use askama::Template; | ||
|
||
#[derive(Template)] | ||
#[template(path = "hello.html")] | ||
struct HelloTemplate { | ||
name: String, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let mut app = Graphul::new(); | ||
|
||
app.get("/:name", |c: Context| async move { | ||
let template = HelloTemplate { name: c.params("name") }; | ||
HtmlTemplate(template) | ||
}); | ||
|
||
app.run("127.0.0.1:8000").await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Hello, {{ name }}!</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub const VERSION: &str = "0.2.2"; | ||
pub const VERSION: &str = "0.2.3"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use askama::Template; | ||
use axum::response::{Html, IntoResponse, Response}; | ||
use hyper::StatusCode; | ||
|
||
pub struct HtmlTemplate<T>(pub T); | ||
|
||
impl<T> IntoResponse for HtmlTemplate<T> | ||
where | ||
T: Template, | ||
{ | ||
fn into_response(self) -> Response { | ||
match self.0.render() { | ||
Ok(html) => Html(html).into_response(), | ||
Err(err) => ( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
format!("Failed to render template. Error: {}", err), | ||
) | ||
.into_response(), | ||
} | ||
} | ||
} |