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
@@ -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