Skip to content

Database middleware #73

Answered by gsserge
fimbault asked this question in Q&A
Apr 22, 2021 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

With Routerify you can use any db library that supports tokio. Initialize the pool and then pass it as shared data to a router instance.
Here's a simple yet fully functional example using sqlx and Postgres.

Cargo.toml

[dependencies]
tokio = { version = "1", features = ["full"] }
hyper = { version = "0.14", features = ["full"] }
routerify = "2"
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "postgres" ] }

main.rs

use hyper::{Body, Request, Response, Server};
use routerify::prelude::*;
use routerify::{Router, RouterService};
use sqlx::postgres;
use std::{convert::Infallible, net::SocketAddr};

struct Service {
    db_pool: postgres::PgPool,
}

async fn home(req: Request<Body>)

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@fimbault
Comment options

@gsserge
Comment options

@fimbault
Comment options

@rousan
Comment options

Answer selected by gsserge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants