summaryrefslogtreecommitdiff
path: root/server/src/db.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-08-23 18:57:16 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-08-23 18:57:16 +0200
commit2231ab6730552d3b58b00ae42d490743a29d7c8b (patch)
tree4a9612ec08398b8abc2e4ce7c30825f7ef18659c /server/src/db.rs
parentbba5f252f265e6102d2f052e9ca100efe318508f (diff)
Add docs
Diffstat (limited to 'server/src/db.rs')
-rw-r--r--server/src/db.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/server/src/db.rs b/server/src/db.rs
index 3c1467e..43c3bd9 100644
--- a/server/src/db.rs
+++ b/server/src/db.rs
@@ -2,8 +2,12 @@ use crate::errors::AppError;
use sqlx::postgres::PgPool;
+/// Static variable used to manage the database connection. Called with value = None raises a panic
+/// error.
static mut CONNECTION: Option<PgPool> = None;
+/// Setup database connection. Get variable `DATABASE_URL` from the environment. Sqlx crate already
+/// defines an error for environments without DATABASE_URL.
pub async fn setup() -> Result<(), AppError> {
let database_url =
std::env::var("DATABASE_URL").expect("Define `DATABASE_URL` environment variable.");
@@ -15,6 +19,8 @@ pub async fn setup() -> Result<(), AppError> {
Ok(())
}
+/// Get connection. Raises an error if `setup()` has not been called yet.
+/// Managing static `CONNECTION` is an unsafe operation.
pub unsafe fn get_client() -> &'static PgPool {
match &CONNECTION {
Some(client) => client,