Skip to content

Commit

Permalink
First attempt at one-shot server logic
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Jan 18, 2023
1 parent 7bba2a5 commit 54c8539
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
43 changes: 37 additions & 6 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ tempfile = "3.3.0"
[dependencies]
anyhow = { version = "1.0.65", features = ["backtrace"] }
atty = "0.2"
axum = "0.6.2"
axum-server = "0.4.4"
chrono = "0.4.23"
clap = { version = "4.0.22", features = ["derive"] }
clap_complete = "4.0.6"
Expand All @@ -30,5 +32,6 @@ serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.86"
serde_yaml = "0.8.26"
tiny-gradient = "0.1"
tokio = { version = "1.24.2", features = ["full"] }
turbo-updater = { path = "../turbo-updater" }
webbrowser = "0.8.4"
5 changes: 3 additions & 2 deletions crates/turborepo-lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ pub struct RunArgs {
/// we use it here to modify clap's arguments.
///
/// returns: Result<Payload, Error>
pub fn run(repo_state: Option<RepoState>) -> Result<Payload> {
#[tokio::main]
pub async fn run(repo_state: Option<RepoState>) -> Result<Payload> {
let mut clap_args = Args::new()?;
// If there is no command, we set the command to `Command::Run` with
// `self.parsed_args.run_args` as arguments.
Expand Down Expand Up @@ -401,7 +402,7 @@ pub fn run(repo_state: Option<RepoState>) -> Result<Payload> {
}

let repo_config = RepoConfig::new(&clap_args)?;
login::login(repo_config);
login::login(repo_config).await;

Ok(Payload::Rust(Ok(0)))
}
Expand Down
38 changes: 37 additions & 1 deletion crates/turborepo-lib/src/commands/login.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
use std::{
cell::{Cell, RefCell},
net::SocketAddr,
rc::Rc,
sync::Arc,
};

use axum::{routing::get, Router};
use axum_server::Handle;
use log::{debug, info, warn};
use tokio::sync::Mutex;

use crate::{config::RepoConfig, get_version};

const DEFAULT_HOST_NAME: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 9789;

pub fn login(repo_config: RepoConfig) {
pub async fn login(repo_config: RepoConfig) {
let login_url_base = &repo_config.login_url;
debug!("turbo v{}", get_version());
debug!("api url: {}", repo_config.api_url);
Expand All @@ -16,10 +26,36 @@ pub fn login(repo_config: RepoConfig) {

info!(">>> Opening browser to {login_url}");
direct_user_to_url(&login_url);

let query = Arc::new(Mutex::new(Cell::new(String::new())));
new_one_shot_server(DEFAULT_PORT, query.clone()).await;
println!("{}", query.lock().await.take());
}

fn direct_user_to_url(url: &str) {
if webbrowser::open(url).is_err() {
warn!("Failed to open browser. Please visit {url} in your browser.");
}
}

async fn new_one_shot_server(port: u16, query: Arc<Mutex<Cell<String>>>) {
let handle = axum_server::Handle::new();
let route_handle = handle.clone();
let app = Router::new()
// `GET /` goes to `root`
.route(
"/",
get(|| async move {
let q = query.lock().await;
q.set("hello".to_string());
route_handle.shutdown();
}),
);
let addr = SocketAddr::from(([127, 0, 0, 1], port));
println!("listening on {}", addr);
axum_server::bind(addr)
.handle(handle)
.serve(app.into_make_service())
.await
.unwrap();
}

0 comments on commit 54c8539

Please sign in to comment.