AscendingCreations / rocket_sqlxpostgres

SQLx postgres poller for Rocket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rocket_sqlxpostgres

SQLx postgres pooler for Rocket

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;

use rocket_sqlxpostgres::{SqlxPostgresFairing, SqlxPostgresConfig, SQLxPostgres};
use sqlx::pool::PoolConnection;

fn main() {
    let config = SqlxPostgresConfig::default()
            .with_database("databasename")
            .with_username("username")
            .with_password("password")
            .with_host("localhost")
            .with_port("5432");

    rocket::build()
        .attach(SqlxPostgresFairing::new(config, None))
        .mount("/", routes![index])
        .launch();
}

#[get("/")]
async fn index(sqlxsession: SQLxPostgres) -> Option<String> {
    let mut guard: PoolConnection<sqlx::Postgres> = db.poll.acquire().await.unwrap();

    let (sessions,) =
        sqlx::query_as("SELECT COUNT(*) FROM async_sessions")
            .fetch_one(&mut guard)
            .await.unwrap_or((0i64,));

    format!("{} Sessions in Database", sessions)
}

About

SQLx postgres poller for Rocket

License:MIT License


Languages

Language:Rust 100.0%