about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-02-21 22:47:06 +0300
committerVika <vika@fireburn.ru>2022-02-21 22:47:06 +0300
commit55ba0a507bd310e3b04ac23e0311666968c2e1f1 (patch)
tree1da3a850a0e5da0a0dd4e307236bef14b8520052
parent987ef5045023bcf7b6b843684f62db55f7c142d2 (diff)
downloadkittybox-55ba0a507bd310e3b04ac23e0311666968c2e1f1.tar.zst
Small tweaks, additions and preparations
-rw-r--r--src/indieauth.rs12
-rw-r--r--src/main.rs39
2 files changed, 26 insertions, 25 deletions
diff --git a/src/indieauth.rs b/src/indieauth.rs
index bcc8bbe..4dfe11d 100644
--- a/src/indieauth.rs
+++ b/src/indieauth.rs
@@ -184,7 +184,7 @@ where T: hyper::client::connect::Connect + Clone + Send + Sync + 'static {
                     },
                     _ => Err(IndieAuthError {
                         source: None,
-                        msg: format!("Token endpoint returned {}", res.status()).to_string(),
+                        msg: format!("Token endpoint returned {}", res.status()),
                         kind: ErrorKind::TokenEndpointError
                     }.into())
                 },
@@ -210,15 +210,9 @@ mod tests {
         assert!(!user.check_scope("delete"));
     }
 
+    #[inline]
     fn get_http_client() -> hyper::Client<impl hyper::client::connect::Connect + Clone + Send + Sync + 'static, hyper::Body> {
-        let builder = hyper::Client::builder();
-        let https = hyper_rustls::HttpsConnectorBuilder::new()
-            .with_webpki_roots()
-            .https_or_http()
-            .enable_http1()
-            .enable_http2()
-            .build();
-        builder.build(https)
+        hyper::Client::new()
     }
     
     #[tokio::test]
diff --git a/src/main.rs b/src/main.rs
index 4036d46..695fb83 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,8 @@
 use log::{debug, error, info};
 use std::env;
 use http_types::Url;
+use hyper::client::{HttpConnector,connect::dns::GaiResolver};
+use hyper_rustls::HttpsConnector;
 use warp::{Filter, host::Authority, path::FullPath};
 
 #[tokio::main]
@@ -65,14 +67,13 @@ async fn main() -> Result<(), kittybox::database::StorageError> {
         Some(value) => value,
         None => {
             if let Ok(filename) = env::var("COOKIE_SECRET_FILE") {
-                /*use async_std::io::ReadExt;
+                use tokio::io::AsyncReadExt;
 
-                let mut file = async_std::fs::File::open(filename).await?;
+                let mut file = tokio::fs::File::open(filename).await?;
                 let mut temp_string = String::new();
                 file.read_to_string(&mut temp_string).await?;
 
-                temp_string*/
-                todo!()
+                temp_string
             } else {
                 error!("COOKIE_SECRET or COOKIE_SECRET_FILE is not set, will not be able to log in users securely!");
                 std::process::exit(1);
@@ -91,6 +92,17 @@ async fn main() -> Result<(), kittybox::database::StorageError> {
             }
         };
 
+    let http_client: hyper::Client<HttpsConnector<HttpConnector<GaiResolver>>, hyper::Body> = {
+        let builder = hyper::Client::builder();
+        let https = hyper_rustls::HttpsConnectorBuilder::new()
+            .with_webpki_roots()
+            .https_only()
+            .enable_http1()
+            .enable_http2()
+            .build();
+        builder.build(https)
+    };
+
     if backend_uri.starts_with("redis") {
         println!("The Redis backend is deprecated.");
         std::process::exit(1);
@@ -112,17 +124,11 @@ async fn main() -> Result<(), kittybox::database::StorageError> {
         
         let micropub = warp::path("micropub")
             .and(warp::path::end()
-                 .and(warp::get()
-                      .and(kittybox::micropub::query(database))
-                      .or(warp::post()
-                          .and(kittybox::util::require_host())
-                          .map(|host| "micropub post!"))
-                      .or(warp::options()
-                          .map(|| warp::reply::json::<Option<()>>(&None))
-                          // TODO: why doesn't this work?
-                          // .map(warp::reply::with::header("Allow", "GET, POST"))
-                          .map(|reply| warp::reply::with_header(reply, "Allow", "GET, POST"))
-                      ))
+                 .and(kittybox::micropub::micropub(
+                     database.clone(),
+                     token_endpoint.to_string(),
+                     http_client.clone()
+                 ))
                  .or(warp::get()
                      .and(warp::path("client"))
                      .and(warp::path::end())
@@ -176,7 +182,8 @@ async fn main() -> Result<(), kittybox::database::StorageError> {
 
         let server = warp::serve(app);
 
-        // TODO use warp::Server::bind_with_graceful_shutdown
+        // TODO https://github.com/seanmonstar/warp/issues/854
+        // TODO move to Hyper to wrap the requests in metrics
         info!("Listening on {:?}", host);
         server.bind(host).await;
         Ok(())