Skip to content

Commit

Permalink
fix(serverless): don't use a .env file on release (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Dec 6, 2022
1 parent c72e2fc commit 2bf63f3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-flowers-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/serverless': patch
---

Don't use a .env file on release builds
5 changes: 4 additions & 1 deletion docker/serverless.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ RUN cargo build --release
FROM rust:1.65

COPY --from=builder /app/target/release/lagon-serverless /usr/local/bin/lagon-serverless
COPY --from=builder /app/.env ./.env

# Serverless
EXPOSE 4000
# Prometheus
EXPOSE 9000

CMD ["lagon-serverless"]
7 changes: 4 additions & 3 deletions packages/serverless/src/deployments/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
env,
sync::Arc,
time::{Duration, Instant},
};
Expand All @@ -13,10 +14,10 @@ const CACHE_TASK_INTERVAL: Duration = Duration::from_secs(60);

pub fn run_cache_clear_task(last_requests: Arc<RwLock<HashMap<String, Instant>>>) {
let isolates_cache_seconds = Duration::from_secs(
dotenv::var("LAGON_ISOLATES_CACHE_SECONDS")
.expect("LAGON_ISOLATES_CACHE_SECONDS must be set")
env::var("LAGON_ISOLATES_CACHE_SECONDS")
.expect("LAGON_ISOLATES_CACHE_SECONDS is not set")
.parse()
.expect("Failed to parse LAGON_ISOLATES_CACHE_SECONDS"),
.expect("LAGON_ISOLATES_CACHE_SECONDS is not a valid number"),
);

tokio::spawn(async move {
Expand Down
6 changes: 3 additions & 3 deletions packages/serverless/src/deployments/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
collections::{HashMap, HashSet},
fs,
env, fs,
path::Path,
sync::Arc,
};
Expand Down Expand Up @@ -43,15 +43,15 @@ impl Deployment {
domains.push(format!(
"{}.{}",
self.id,
dotenv::var("LAGON_ROOT_DOMAIN").expect("LAGON_ROOT_DOMAIN must be set")
env::var("LAGON_ROOT_DOMAIN").expect("LAGON_ROOT_DOMAIN must be set")
));

// Default domain (function's name) and custom domains are only set in production deployments
if self.is_production {
domains.push(format!(
"{}.{}",
self.function_name,
dotenv::var("LAGON_ROOT_DOMAIN").expect("LAGON_ROOT_DOMAIN must be set")
env::var("LAGON_ROOT_DOMAIN").expect("LAGON_ROOT_DOMAIN must be set")
));

domains.extend(self.domains.clone());
Expand Down
4 changes: 2 additions & 2 deletions packages/serverless/src/deployments/pubsub.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, env, sync::Arc};

use anyhow::Result;
use log::{error, warn};
Expand Down Expand Up @@ -26,7 +26,7 @@ pub fn listen_pub_sub(
deployments: Arc<RwLock<HashMap<String, Arc<Deployment>>>>,
) -> JoinHandle<Result<()>> {
tokio::spawn(async move {
let url = dotenv::var("REDIS_URL").expect("REDIS_URL must be set");
let url = env::var("REDIS_URL").expect("REDIS_URL must be set");
let client = redis::Client::open(url)?;
let mut conn = client.get_connection()?;
let mut pub_sub = conn.as_pubsub();
Expand Down
7 changes: 5 additions & 2 deletions packages/serverless/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use axiom_rs::Client;
use chrono::prelude::Local;
use flume::Sender;
use serde_json::{json, Value};
use std::sync::{Arc, RwLock};
use std::{
env,
sync::{Arc, RwLock},
};

use log::{
as_debug, kv::source::as_map, set_boxed_logger, set_max_level, Level, LevelFilter, Log,
Expand Down Expand Up @@ -35,7 +38,7 @@ impl SimpleLogger {

Self {
tx: Arc::new(RwLock::new(Some(tx))),
region: dotenv::var("LAGON_REGION").expect("LAGON_REGION must be set"),
region: env::var("LAGON_REGION").expect("LAGON_REGION must be set"),
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions packages/serverless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use s3::creds::Credentials;
use s3::Bucket;
use std::collections::HashMap;
use std::convert::Infallible;
use std::env;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Instant;
Expand Down Expand Up @@ -262,7 +263,10 @@ async fn handle_request(

#[tokio::main]
async fn main() -> Result<()> {
// Only load a .env file on development
#[cfg(debug_assertions)]
dotenv::dotenv().expect("Failed to load .env file");

let _flush_guard = init_logger().expect("Failed to init logger");

let runtime = Runtime::new(RuntimeOptions::default());
Expand All @@ -271,7 +275,7 @@ async fn main() -> Result<()> {
let builder = PrometheusBuilder::new();
builder.install().expect("Failed to start metrics exporter");

let url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set");
let url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let url = url.as_str();
let opts = Opts::from_url(url).expect("Failed to parse DATABASE_URL");
#[cfg(not(debug_assertions))]
Expand All @@ -281,11 +285,11 @@ async fn main() -> Result<()> {
let pool = Pool::new(opts)?;
let conn = pool.get_conn()?;

let bucket_name = dotenv::var("S3_BUCKET").expect("S3_BUCKET must be set");
let bucket_name = env::var("S3_BUCKET").expect("S3_BUCKET must be set");
let region = "eu-west-3".parse()?;
let credentials = Credentials::new(
Some(&dotenv::var("S3_ACCESS_KEY_ID").expect("S3_ACCESS_KEY_ID must be set")),
Some(&dotenv::var("S3_SECRET_ACCESS_KEY").expect("S3_SECRET_ACCESS_KEY must be set")),
Some(&env::var("S3_ACCESS_KEY_ID").expect("S3_ACCESS_KEY_ID must be set")),
Some(&env::var("S3_SECRET_ACCESS_KEY").expect("S3_SECRET_ACCESS_KEY must be set")),
None,
None,
None,
Expand Down

0 comments on commit 2bf63f3

Please sign in to comment.