Start work on a login command
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
co-authored by
Nathan Sobo
parent
3a932cc9bf
commit
db3e01a12c
+7
-6
@@ -20,32 +20,33 @@ crossbeam-channel = "0.5.0"
|
||||
ctor = "0.1.20"
|
||||
dirs = "3.0"
|
||||
easy-parallel = "3.1.0"
|
||||
fsevent = {path = "../fsevent"}
|
||||
fsevent = { path = "../fsevent" }
|
||||
futures-core = "0.3"
|
||||
gpui = {path = "../gpui"}
|
||||
gpui = { path = "../gpui" }
|
||||
ignore = "0.4"
|
||||
lazy_static = "1.4.0"
|
||||
libc = "0.2"
|
||||
log = "0.4"
|
||||
num_cpus = "1.13.0"
|
||||
parking_lot = "0.11.1"
|
||||
postage = {version = "0.4.1", features = ["futures-traits"]}
|
||||
postage = { version = "0.4.1", features = ["futures-traits"] }
|
||||
rand = "0.8.3"
|
||||
rust-embed = "5.9.0"
|
||||
seahash = "4.1"
|
||||
serde = {version = "1", features = ["derive"]}
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
similar = "1.3"
|
||||
simplelog = "0.9"
|
||||
smallvec = {version = "1.6", features = ["union"]}
|
||||
smallvec = { version = "1.6", features = ["union"] }
|
||||
smol = "1.2.5"
|
||||
toml = "0.5"
|
||||
tree-sitter = "0.19.5"
|
||||
tree-sitter-rust = "0.19.0"
|
||||
url = "2.2"
|
||||
|
||||
[dev-dependencies]
|
||||
cargo-bundle = "0.5.0"
|
||||
env_logger = "0.8"
|
||||
serde_json = {version = "1.0.64", features = ["preserve_order"]}
|
||||
serde_json = { version = "1.0.64", features = ["preserve_order"] }
|
||||
tempdir = "0.3.7"
|
||||
unindent = "0.1.7"
|
||||
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use gpui::MutableAppContext;
|
||||
use smol::io::{AsyncBufReadExt, AsyncWriteExt};
|
||||
use url::Url;
|
||||
|
||||
pub mod assets;
|
||||
pub mod editor;
|
||||
pub mod file_finder;
|
||||
@@ -18,3 +23,83 @@ pub struct AppState {
|
||||
pub settings: postage::watch::Receiver<settings::Settings>,
|
||||
pub language_registry: std::sync::Arc<language::LanguageRegistry>,
|
||||
}
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_global_action("app:authenticate", authenticate);
|
||||
cx.add_global_action("app:quit", quit);
|
||||
}
|
||||
|
||||
fn authenticate(_: &(), cx: &mut MutableAppContext) {
|
||||
let zed_url = std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev".to_string());
|
||||
let platform = cx.platform().clone();
|
||||
|
||||
dbg!(&zed_url);
|
||||
|
||||
let task = cx.background_executor().spawn(async move {
|
||||
let listener = smol::net::TcpListener::bind("127.0.0.1:0").await?;
|
||||
let port = listener.local_addr()?.port();
|
||||
|
||||
platform.open_url(&format!(
|
||||
"{}/sign_in?native_app_port={}&native_app_public_key=unused-for-now",
|
||||
zed_url, port,
|
||||
));
|
||||
|
||||
let (mut stream, _) = listener.accept().await?;
|
||||
let mut reader = smol::io::BufReader::new(&mut stream);
|
||||
let mut line = String::new();
|
||||
reader.read_line(&mut line).await?;
|
||||
|
||||
let mut parts = line.split(" ");
|
||||
if parts.next() == Some("GET") {
|
||||
if let Some(path) = parts.next() {
|
||||
let url = Url::parse(&format!("http://example.com{}", path))
|
||||
.context("failed to parse login notification url")?;
|
||||
let mut access_token = None;
|
||||
let mut public_key = None;
|
||||
for (key, value) in url.query_pairs() {
|
||||
if key == "access_token" {
|
||||
access_token = Some(value);
|
||||
} else if key == "public_key" {
|
||||
public_key = Some(value);
|
||||
}
|
||||
}
|
||||
stream
|
||||
.write_all(LOGIN_RESPONSE.as_bytes())
|
||||
.await
|
||||
.context("failed to write login response")?;
|
||||
stream.flush().await.context("failed to flush tcp stream")?;
|
||||
|
||||
eprintln!(
|
||||
"logged in. access_token: {:?}, public_key: {:?}",
|
||||
access_token, public_key
|
||||
);
|
||||
|
||||
platform.activate(true);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(anyhow!("failed to parse http request from zed web app"))
|
||||
});
|
||||
|
||||
cx.spawn(|_| async move {
|
||||
if let Err(e) = task.await {
|
||||
log::error!("failed to login {:?}", e)
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn quit(_: &(), cx: &mut MutableAppContext) {
|
||||
cx.platform().quit();
|
||||
}
|
||||
|
||||
const LOGIN_RESPONSE: &'static str = "
|
||||
HTTP/1.1 200 OK\r
|
||||
Content-Length: 64\r
|
||||
Content-Type: text/html\r
|
||||
\r
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<script>window.close();</script>
|
||||
</html>
|
||||
";
|
||||
|
||||
+3
-1
@@ -6,7 +6,7 @@ use log::LevelFilter;
|
||||
use simplelog::SimpleLogger;
|
||||
use std::{fs, path::PathBuf, sync::Arc};
|
||||
use zed::{
|
||||
assets, editor, file_finder, language, menus, settings,
|
||||
self, assets, editor, file_finder, language, menus, settings,
|
||||
workspace::{self, OpenParams},
|
||||
AppState,
|
||||
};
|
||||
@@ -26,6 +26,8 @@ fn main() {
|
||||
|
||||
app.run(move |cx| {
|
||||
cx.set_menus(menus::menus(app_state.clone()));
|
||||
|
||||
zed::init(cx);
|
||||
workspace::init(cx);
|
||||
editor::init(cx);
|
||||
file_finder::init(cx);
|
||||
|
||||
@@ -14,6 +14,12 @@ pub fn menus(state: AppState) -> Vec<Menu<'static>> {
|
||||
arg: None,
|
||||
},
|
||||
MenuItem::Separator,
|
||||
MenuItem::Action {
|
||||
name: "Log In",
|
||||
keystroke: None,
|
||||
action: "app:authenticate",
|
||||
arg: None,
|
||||
},
|
||||
MenuItem::Action {
|
||||
name: "Quit",
|
||||
keystroke: Some("cmd-q"),
|
||||
|
||||
@@ -29,7 +29,6 @@ use std::{
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_global_action("workspace:open", open);
|
||||
cx.add_global_action("workspace:open_paths", open_paths);
|
||||
cx.add_global_action("app:quit", quit);
|
||||
cx.add_action("workspace:save", Workspace::save_active_item);
|
||||
cx.add_action("workspace:debug_elements", Workspace::debug_elements);
|
||||
cx.add_action("workspace:new_file", Workspace::open_new_file);
|
||||
@@ -98,10 +97,6 @@ fn open_paths(params: &OpenParams, cx: &mut MutableAppContext) {
|
||||
});
|
||||
}
|
||||
|
||||
fn quit(_: &(), cx: &mut MutableAppContext) {
|
||||
cx.platform().quit();
|
||||
}
|
||||
|
||||
pub trait Item: Entity + Sized {
|
||||
type View: ItemView;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user