Skip to content

Commit 757562c

Browse files
committed
fix: embed envrionment variables
1 parent 921813b commit 757562c

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

src/helpers/database/connection.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
use std::sync::OnceLock;
1+
use sqlx::{
2+
migrate::{MigrateError, Migrator},
3+
postgres::PgPoolOptions,
4+
Error as SqlxError, Pool, Postgres,
5+
};
26
use std::path::Path;
3-
use sqlx::{migrate::{MigrateError, Migrator}, postgres::PgPoolOptions, Error as SqlxError, Pool, Postgres};
7+
use std::sync::OnceLock;
48
use thiserror::Error;
59

610
static CONNECTION: OnceLock<Pool<Postgres>> = OnceLock::new();
@@ -11,7 +15,7 @@ pub enum DbConnectionError {
1115
Connection(#[from] SqlxError),
1216

1317
#[error("{0:#}")]
14-
Migrate(#[from] MigrateError)
18+
Migrate(#[from] MigrateError),
1519
}
1620

1721
pub async fn get_db_connection<'r>() -> Result<&'r Pool<Postgres>, DbConnectionError> {
@@ -21,7 +25,7 @@ pub async fn get_db_connection<'r>() -> Result<&'r Pool<Postgres>, DbConnectionE
2125

2226
let pool = PgPoolOptions::new()
2327
.max_connections(5)
24-
.connect(&lc!("DATABASE_URL"))
28+
.connect(&lc!(env!("DATABASE_URL")))
2529
.await?;
2630

2731
Migrator::new(Path::new("./migrations"))

src/helpers/misc/stripe.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
1+
use lazy_static::lazy_static;
12
use reqwest::{header::AUTHORIZATION, Client, Error as RequestError};
23
use serde::Deserialize;
34
use thiserror::Error;
45

5-
use crate::models::{key::{ApiKey, ApiKeyError}, user::User};
6+
use crate::models::{
7+
key::{ApiKey, ApiKeyError},
8+
user::User,
9+
};
10+
11+
lazy_static! {
12+
static ref STRIPE_SECRET: String = lc!(env!("STRIPE_SECRET"));
13+
}
614

715
#[derive(Error, Debug)]
816
pub enum StripeError {
917
#[error("{0:#}")]
1018
Request(#[from] RequestError),
1119

1220
#[error("{0:#}")]
13-
ApiKey(#[from] ApiKeyError)
21+
ApiKey(#[from] ApiKeyError),
1422
}
1523

1624
#[derive(Deserialize)]
1725
struct StripeSession {
18-
url: String
26+
url: String,
1927
}
2028

21-
pub async fn create_stripe_payment(host: &String, user: &User, amount: f32) -> Result<String, StripeError> {
29+
pub async fn create_stripe_payment(
30+
host: &String,
31+
user: &User,
32+
amount: f32,
33+
) -> Result<String, StripeError> {
2234
let StripeSession { url } = Client::new()
2335
.post("https://api.stripe.com/v1/checkout/sessions")
24-
.header(AUTHORIZATION, format!("Bearer {}", lc!("STRIPE_SECRET")))
36+
.header(AUTHORIZATION, format!("Bearer {}", *STRIPE_SECRET))
2537
.form(&[
2638
("payment_method_types[]", "card"),
2739
("line_items[0][price_data][currency]", "usd"),
28-
("line_items[0][price_data][product_data][name]", "RlARndG API key"),
29-
("line_items[0][price_data][unit_amount]", &(amount * 100f32).round().to_string()),
40+
(
41+
"line_items[0][price_data][product_data][name]",
42+
"RlARndG API key",
43+
),
44+
(
45+
"line_items[0][price_data][unit_amount]",
46+
&(amount * 100f32).round().to_string(),
47+
),
3048
("line_items[0][quantity]", "1"),
3149
("mode", "payment"),
32-
("success_url", &format!(
33-
"{host}/api/keys/checkout/success?i={}&p={}&c={{CHECKOUT_SESSION_ID}}",
34-
user.id,
35-
amount
36-
)),
37-
("cancel_url", &format!("{host}/pricing"))
50+
(
51+
"success_url",
52+
&format!(
53+
"{host}/api/keys/checkout/success?i={}&p={}&c={{CHECKOUT_SESSION_ID}}",
54+
user.id, amount
55+
),
56+
),
57+
("cancel_url", &format!("{host}/pricing")),
3858
])
3959
.send()
4060
.await?
@@ -46,12 +66,12 @@ pub async fn create_stripe_payment(host: &String, user: &User, amount: f32) -> R
4666

4767
#[derive(Deserialize)]
4868
struct StripeSessionResponse {
49-
payment_intent: String
69+
payment_intent: String,
5070
}
5171

5272
#[derive(Deserialize)]
5373
struct PaymentIntentResponse {
54-
status: String
74+
status: String,
5575
}
5676

5777
pub async fn verify_payment(session_id: &String) -> Result<bool, StripeError> {
@@ -60,16 +80,20 @@ pub async fn verify_payment(session_id: &String) -> Result<bool, StripeError> {
6080
}
6181

6282
let StripeSessionResponse { payment_intent } = Client::new()
63-
.get(format!("https://api.stripe.com/v1/checkout/sessions/{session_id}"))
64-
.header(AUTHORIZATION, format!("Bearer {}", lc!("STRIPE_SECRET")))
83+
.get(format!(
84+
"https://api.stripe.com/v1/checkout/sessions/{session_id}"
85+
))
86+
.header(AUTHORIZATION, format!("Bearer {}", *STRIPE_SECRET))
6587
.send()
6688
.await?
6789
.json()
6890
.await?;
6991

7092
let PaymentIntentResponse { status } = Client::new()
71-
.get(format!("https://api.stripe.com/v1/payment_intents/{payment_intent}"))
72-
.header(AUTHORIZATION, format!("Bearer {}", lc!("STRIPE_SECRET")))
93+
.get(format!(
94+
"https://api.stripe.com/v1/payment_intents/{payment_intent}"
95+
))
96+
.header(AUTHORIZATION, format!("Bearer {}", *STRIPE_SECRET))
7397
.send()
7498
.await?
7599
.json()

0 commit comments

Comments
 (0)