diff options
author | Melody Horn / boringcactus <melody@boringcactus.com> | 2021-06-12 23:24:33 -0600 |
---|---|---|
committer | Melody Horn / boringcactus <melody@boringcactus.com> | 2021-06-12 23:24:33 -0600 |
commit | 4c8940569e5729213b7eee6f3d5be39b745b697b (patch) | |
tree | 20935676f7a14ad12752ec98b65db9f067fba239 | |
parent | 582124d59afa438e2cb88cc024d6b19232236e1e (diff) | |
download | tosin-4c8940569e5729213b7eee6f3d5be39b745b697b.tar.gz tosin-4c8940569e5729213b7eee6f3d5be39b745b697b.zip |
actually implement the easy example
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | examples/tutorial01/main.rs | 15 | ||||
-rw-r--r-- | examples/tutorial01/polls/urls.rs | 10 | ||||
-rw-r--r-- | examples/tutorial01/polls/views.rs | 6 | ||||
-rw-r--r-- | src/contrib/admin/site/urls.rs | 8 | ||||
-rw-r--r-- | src/http/mod.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/server/mod.rs | 11 | ||||
-rw-r--r-- | src/urls/mod.rs | 17 |
9 files changed, 44 insertions, 35 deletions
@@ -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}; @@ -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,)>; |