Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add env option to dev command #219

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-rivers-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Add env option to dev command
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ hyper-tls = { version = "0.5.0", features = ["vendored"] }
flume = "0.10.14"
chrono = "0.4.22"
notify = "5.0.0"
envfile = "0.2.1"
41 changes: 31 additions & 10 deletions packages/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::offset::Local;
use colored::Colorize;
use envfile::EnvFile;
use hyper::body::Bytes;
use hyper::http::response::Builder;
use hyper::service::{make_service_fn, service_fn};
Expand All @@ -21,12 +22,27 @@ use crate::utils::{
bundle_function, info, input, success, validate_code_file, validate_public_dir, FileCursor,
};

fn parse_environment_variables(env: Option<PathBuf>) -> io::Result<HashMap<String, String>> {
let mut environment_variables = HashMap::new();

if let Some(path) = env {
let envfile = EnvFile::new(path)?;

for (key, value) in envfile.store {
environment_variables.insert(key, value);
}
}

Ok(environment_variables)
}

// This function is similar to packages/serverless/src/main.rs,
// expect that we don't have multiple deployments and such multiple
// threads to manage.
async fn handle_request(
req: HyperRequest<Body>,
content: Arc<Mutex<(FileCursor, HashMap<String, FileCursor>)>>,
environment_variables: HashMap<String, String>,
) -> Result<HyperResponse<Body>, Infallible> {
let mut url = req.uri().to_string();

Expand Down Expand Up @@ -73,9 +89,10 @@ async fn handle_request(
} else {
let request = Request::from_hyper(req).await;

let mut isolate = Isolate::new(IsolateOptions::new(
String::from_utf8(index.get_ref().to_vec()).unwrap(),
));
let mut isolate = Isolate::new(
IsolateOptions::new(String::from_utf8(index.get_ref().to_vec()).unwrap())
.with_environment_variables(environment_variables),
);

isolate.run(request, tx).await;
}
Expand Down Expand Up @@ -147,6 +164,7 @@ pub async fn dev(
public_dir: Option<PathBuf>,
port: Option<u16>,
hostname: Option<String>,
env: Option<PathBuf>,
) -> io::Result<()> {
validate_code_file(&file)?;

Expand All @@ -173,15 +191,18 @@ pub async fn dev(
.unwrap();

let server_content = content.clone();
let environment_variables = parse_environment_variables(env)?;

let server =
Server::bind(&addr).serve(make_service_fn(move |_conn| {
let content = server_content.clone();
let server = Server::bind(&addr).serve(make_service_fn(move |_conn| {
let content = server_content.clone();
let environment_variables = environment_variables.clone();

async move {
Ok::<_, Infallible>(service_fn(move |req| handle_request(req, content.clone())))
}
}));
async move {
Ok::<_, Infallible>(service_fn(move |req| {
handle_request(req, content.clone(), environment_variables.clone())
}))
}
}));

let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = RecommendedWatcher::new(
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ enum Commands {
/// Hostname to start dev server on
#[clap(long)]
hostname: Option<String>,
/// Path to a env file to parse
#[clap(short, long, value_parser)]
env: Option<PathBuf>,
},
/// Build a Function without deploying it
Build {
Expand Down Expand Up @@ -105,7 +108,8 @@ async fn main() {
public_dir,
port,
hostname,
} => commands::dev(file, client, public_dir, port, hostname).await,
env,
} => commands::dev(file, client, public_dir, port, hostname, env).await,
Commands::Build {
file,
client,
Expand Down