From 4c8940569e5729213b7eee6f3d5be39b745b697b Mon Sep 17 00:00:00 2001 From: Melody Horn / boringcactus Date: Sat, 12 Jun 2021 23:24:33 -0600 Subject: actually implement the easy example --- Cargo.toml | 3 +++ examples/tutorial01/main.rs | 15 +++++++++------ examples/tutorial01/polls/urls.rs | 10 ++++++---- examples/tutorial01/polls/views.rs | 6 +++--- src/contrib/admin/site/urls.rs | 8 ++++++-- src/http/mod.rs | 4 +--- src/lib.rs | 5 ++--- src/server/mod.rs | 11 +++++++++++ src/urls/mod.rs | 17 +++-------------- 9 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 src/server/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 52b5213..cc6e98f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +hyper = { version = "0.14", features = ["full"] } +tokio = { version = "1", features = ["full"] } +warp = "0.3" diff --git a/examples/tutorial01/main.rs b/examples/tutorial01/main.rs index 3e8f999..62c32cd 100644 --- a/examples/tutorial01/main.rs +++ b/examples/tutorial01/main.rs @@ -1,13 +1,16 @@ use tosin::contrib::admin; -use tosin::urls::Path; +use tosin::http::Filter; +use tosin::urls::{UrlMap, path}; mod polls; -pub const URL_PATTERNS: &[Path] = &[ - Path::Include { url: "polls/", content: polls::urls::URL_PATTERNS }, - Path::Include { url: "admin/", content: admin::site::urls::URL_PATTERNS }, -]; +pub fn urls() -> UrlMap { + path!("polls" / ..).and(polls::urls::urls()) + .or(path!("admin" / ..).and(admin::site::urls::urls())) + .unify() + .boxed() +} fn main() { - tosin::run_server(URL_PATTERNS); + tosin::run_server(urls()); } diff --git a/examples/tutorial01/polls/urls.rs b/examples/tutorial01/polls/urls.rs index d56b8c9..184c6f8 100644 --- a/examples/tutorial01/polls/urls.rs +++ b/examples/tutorial01/polls/urls.rs @@ -1,7 +1,9 @@ -use tosin::urls::Path; +use tosin::http::Filter; +use tosin::urls::{UrlMap, path}; use super::views; -pub const URL_PATTERNS: &[Path] = &[ - Path::View { url: "", view: views::index, name: "index" }, -]; +pub fn urls() -> UrlMap { + path::end().map(views::index) // TODO name: "index" + .boxed() +} diff --git a/examples/tutorial01/polls/views.rs b/examples/tutorial01/polls/views.rs index 23b740d..4859cec 100644 --- a/examples/tutorial01/polls/views.rs +++ b/examples/tutorial01/polls/views.rs @@ -1,5 +1,5 @@ -use tosin::http::{Request, Response}; +use tosin::http::{Reply, Response}; -pub fn index(request: Request) -> Response { - Response("Hello, world. You're at the polls index.") +pub fn index() -> Response { + "Hello, world. You're at the polls index.".into_response() } diff --git a/src/contrib/admin/site/urls.rs b/src/contrib/admin/site/urls.rs index 5e81c4e..ef23d55 100644 --- a/src/contrib/admin/site/urls.rs +++ b/src/contrib/admin/site/urls.rs @@ -1,3 +1,7 @@ -use crate::urls::Path; +use crate::http::{Filter, Reply}; +use crate::urls::{UrlMap, path}; -pub const URL_PATTERNS: &[Path] = &[]; +pub fn urls() -> UrlMap { + path::end().map(|| Reply::into_response("bruh")) + .boxed() +} diff --git a/src/http/mod.rs b/src/http/mod.rs index 69a0706..f8f1066 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -1,3 +1 @@ -pub struct Request {} - -pub struct Response(pub &'static str); +pub use warp::{Filter, Reply, reply::Response}; diff --git a/src/lib.rs b/src/lib.rs index 28e6ab9..b9cf73d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ pub mod contrib; pub mod http; +pub mod server; pub mod urls; -pub fn run_server(url_patterns: &[urls::Path]) { - todo!(); -} +pub use server::run_server; diff --git a/src/server/mod.rs b/src/server/mod.rs new file mode 100644 index 0000000..b275616 --- /dev/null +++ b/src/server/mod.rs @@ -0,0 +1,11 @@ +use crate::urls::UrlMap; + +pub fn run_server(filter_stack: UrlMap) { + let server_task = warp::serve(filter_stack).run(([127, 0, 0, 1], 3030)); + + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(server_task) +} diff --git a/src/urls/mod.rs b/src/urls/mod.rs index 1acf186..45720b6 100644 --- a/src/urls/mod.rs +++ b/src/urls/mod.rs @@ -1,16 +1,5 @@ -use crate::http::{Request, Response}; +pub use warp::path; -pub type View = fn(Request) -> Response; +use crate::http::Response; -pub enum Path { - View { - url: &'static str, - view: View, - name: &'static str, - }, - - Include { - url: &'static str, - content: &'static [Path], - }, -} +pub type UrlMap = warp::filters::BoxedFilter<(Response,)>; -- cgit v1.2.3