mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-07 00:06:24 +03:00
WIP
This commit is contained in:
parent
f7f4aad00f
commit
62f7c858e3
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -896,6 +896,7 @@ dependencies = [
|
||||
"parking_lot",
|
||||
"project",
|
||||
"rand 0.8.3",
|
||||
"routerify",
|
||||
"rpc",
|
||||
"scrypt",
|
||||
"serde",
|
||||
@ -3792,6 +3793,19 @@ dependencies = [
|
||||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "routerify"
|
||||
version = "3.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "496c1d3718081c45ba9c31fbfc07417900aa96f4070ff90dc29961836b7a9945"
|
||||
dependencies = [
|
||||
"http",
|
||||
"hyper",
|
||||
"lazy_static",
|
||||
"percent-encoding",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "roxmltree"
|
||||
version = "0.14.1"
|
||||
|
@ -30,6 +30,7 @@ lipsum = { version = "0.8", optional = true }
|
||||
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||
parking_lot = "0.11.1"
|
||||
rand = "0.8"
|
||||
routerify = "3.0"
|
||||
scrypt = "0.7"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
@ -1,10 +1,24 @@
|
||||
// use crate::{auth, db::UserId, AppState, Request, RequestExt as _};
|
||||
use async_trait::async_trait;
|
||||
use hyper::{
|
||||
header::{CONTENT_LENGTH, CONTENT_TYPE},
|
||||
Body, Request, Response,
|
||||
};
|
||||
use routerify::prelude::*;
|
||||
|
||||
use anyhow::Result;
|
||||
use routerify::RouterBuilder;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{AppState, RequestExt};
|
||||
// use surf::StatusCode;
|
||||
|
||||
pub fn add_routes(router: &mut RouterBuilder<Body, anyhow::Error>) {
|
||||
router.get("/users", get_users);
|
||||
}
|
||||
|
||||
// pub fn add_routes(app: &mut tide::Server<Arc<AppState>>) {
|
||||
// app.at("/users").get(get_users);
|
||||
// app.at("/users").post(create_user);
|
||||
@ -29,15 +43,23 @@ use std::sync::Arc;
|
||||
// .build())
|
||||
// }
|
||||
|
||||
// async fn get_users(request: Request) -> tide::Result {
|
||||
// request.require_token().await?;
|
||||
async fn get_users(request: Request<Body>) -> Result<Response<Body>> {
|
||||
// request.require_token().await?;
|
||||
|
||||
// let users = request.db().get_all_users().await?;
|
||||
let users = request.db().get_all_users().await?;
|
||||
|
||||
// Ok(tide::Response::builder(StatusCode::Ok)
|
||||
// .body(tide::Body::from_json(&users)?)
|
||||
// .build())
|
||||
// }
|
||||
// Body::from
|
||||
|
||||
let body = "Hello World";
|
||||
Ok(Response::builder()
|
||||
.header(CONTENT_LENGTH, body.len() as u64)
|
||||
.header(CONTENT_TYPE, "text/plain")
|
||||
.body(Body::from(body))?)
|
||||
|
||||
// Ok(tide::Response::builder(StatusCode::Ok)
|
||||
// .body(tide::Body::from_json(&users)?)
|
||||
// .build())
|
||||
}
|
||||
|
||||
// async fn create_user(mut request: Request) -> tide::Result {
|
||||
// request.require_token().await?;
|
||||
|
@ -5,16 +5,13 @@ mod env;
|
||||
mod rpc;
|
||||
|
||||
use ::rpc::Peer;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use anyhow::{anyhow, Result};
|
||||
use db::{Db, PostgresDb};
|
||||
use hyper::{
|
||||
server::conn::AddrStream,
|
||||
service::{make_service_fn, service_fn},
|
||||
Body, Request, Response, Server,
|
||||
};
|
||||
use hyper::{Body, Request, Server};
|
||||
use routerify::ext::RequestExt as _;
|
||||
use routerify::{Router, RouterService};
|
||||
use serde::Deserialize;
|
||||
use std::{convert::Infallible, net::TcpListener, sync::Arc};
|
||||
use std::{net::TcpListener, sync::Arc};
|
||||
|
||||
// type Request = tide::Request<Arc<AppState>>;
|
||||
|
||||
@ -42,17 +39,15 @@ impl AppState {
|
||||
}
|
||||
}
|
||||
|
||||
// #[async_trait]
|
||||
// trait RequestExt {
|
||||
// fn db(&self) -> &Arc<dyn Db>;
|
||||
// }
|
||||
trait RequestExt {
|
||||
fn db(&self) -> &Arc<dyn Db>;
|
||||
}
|
||||
|
||||
// #[async_trait]
|
||||
// impl RequestExt for Request {
|
||||
// fn db(&self) -> &Arc<dyn Db> {
|
||||
// &self.state().db
|
||||
// }
|
||||
// }
|
||||
impl RequestExt for Request<Body> {
|
||||
fn db(&self) -> &Arc<dyn Db> {
|
||||
&self.data::<Arc<AppState>>().unwrap().db
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
@ -82,16 +77,19 @@ async fn main() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run_server(state: Arc<AppState>, rpc: Arc<Peer>, listener: TcpListener) -> Result<()> {
|
||||
let make_service = make_service_fn(|_: &AddrStream| async move {
|
||||
Ok::<_, Infallible>(service_fn(|_: Request<Body>| async move {
|
||||
Response::new(Body::from(format!("hello"))
|
||||
}))
|
||||
});
|
||||
fn router(state: Arc<AppState>, peer: Arc<Peer>) -> Result<Router<Body, anyhow::Error>> {
|
||||
let mut router = Router::builder().data(state);
|
||||
api::add_routes(&mut router);
|
||||
router.build().map_err(|error| anyhow!(error))
|
||||
}
|
||||
|
||||
Server::from_tcp(listener)
|
||||
.expect("could not create server")
|
||||
.serve(make_service);
|
||||
pub async fn run_server(
|
||||
state: Arc<AppState>,
|
||||
peer: Arc<Peer>,
|
||||
listener: TcpListener,
|
||||
) -> Result<()> {
|
||||
let service = RouterService::new(router(state, peer)?).map_err(|error| anyhow!(error))?;
|
||||
Server::from_tcp(listener)?.serve(service);
|
||||
|
||||
// let mut app = tide::with_state(state.clone());
|
||||
// rpc::add_routes(&mut app, &rpc);
|
||||
|
Loading…
Reference in New Issue
Block a user