postgrpc/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(unreachable_pub, missing_docs)]
3//!
4//! [`postrpc`](https://github.com/boilerplatter/postgrpc) is a [gRPC](https://grpc.io/) wrapper around Postgres
5//! databases that provides a dynamically-typed, JSON-compatible query interface.
6//!
7//! While most users will use [`postgrpc`](https://github.com/boilerplatter/postgrpc) in its standalone,
8//! executable form, this library provides all of the building blocks needed to build your own gRPC server.
9//! Customization can be done through feature-based conditional compilation and by implementing
10//! [`pools::Pool`] and [`pools::Connection`] traits over customized connection management systems.
11//!
12//! ## How to use `postgrpc`
13//!
14//! By default, this crate ships with all of the features required by its executable. If you're
15//! using `postgrpc` as a library, you will probably only want to enable a subset of those
16//! features. A few of those uses are discussed below.
17//!
18//! #### gRPC handling as a `tonic::server::NamedService`
19//!
20//! By default, `postgrpc` ships a connection-pool-agnostic, [`tonic`](https://docs.rs/tonic/)-compatible gRPC Postgres service in
21//! [`services::postgres`]. To use this service as a part of a [`tonic`](https://docs.rs/tonic)
22//! app without needing to implement your own pool, be sure to enable the `deadpool` feature in
23//! your `Cargo.toml`.
24//!
25//! ##### [`tonic`](https://docs.rs/tonic/latest/tonic/) Example
26//! ```rust
27//! use postgrpc::{pools::deadpool, services::postgres};
28//! use std::sync::Arc;
29//! use tonic::transport::Server;
30//! // deadpool::Configuration implements serde::Deserialize,
31//! // so envy can be used to deserialize it from environment variables
32//! use envy::from_env;
33//!
34//! #[tokio::main]
35//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
36//!   // create the connection pool from environment variables
37//!   let pool = from_env::<deadpool::Configuration>()?
38//!     .create_pool()
39//!     .map(Arc::new)?;
40//!
41//!   // configure and run the tonic server on port 8675
42//!   Server::builder()
43//!     .add_service(services::postgres::new(pool))
44//!     .serve(([127, 0, 0, 1], 8675).into())
45//!     .await?;
46//!
47//!   Ok(())
48//! }
49//! ```
50//!
51//! #### Custom connection pooling
52//! See the documentation of [`pools::Pool`] and [`pools::Connection`] for how to implement your
53//! own connection pools for [`services`]. Custom pools can be implemented without any default
54//! features from this library.
55
56/// Pool implementations and pooling traits for custom connection pools.
57pub mod pools;
58
59/// [`tonic`](https://docs.rs/tonic/latest/tonic/)-compatible gRPC Service implementations for Postgres features.
60pub mod services;
61
62/// [Interceptors](https://docs.rs/tonic/latest/tonic/service/trait.Interceptor.html) for adding
63/// [`tonic::Extensions`] to gRPC requests.
64pub mod extensions;
65
66/// Re-export of the [`async_trait`](https://docs.rs/async-trait) macro for implementing
67/// [`pools::Pool`] and [`pools::Connection`]
68pub use tonic::async_trait;
69
70/// Compiled file descriptors for implementing [gRPC
71/// reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) with e.g.
72/// [`tonic_reflection`](https://docs.rs/tonic-reflection).
73#[cfg_attr(doc, doc(cfg(feature = "reflection")))]
74#[cfg(any(doc, feature = "reflection"))]
75pub const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("routes");