go /

(#3) Extracting the module

Author stanistan
Source https://github.com/stanistan/invoice-proxy/pull/3
Post /stanistan/invoice-proxy/pull/3/625362746/post
Slides /stanistan/invoice-proxy/pull/3/625362746/slides
.md /stanistan/invoice-proxy/pull/3/625362746/md

It’s a PR! to do a thing!


Notable Changes

Cleaning up and refactoring the library.


rustlang/Cargo.toml (thread)

@@ -4,13 +4,6 @@ version = "0.1.0"
 authors = ["Stan Rozenraukh <stan@stanistan.com>"]
 edition = "2018"
 
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
-
 [dependencies]
-num-format = "0.4.0"
-warp = "0.2"
-url = "2.1.1"
-reqwest = { version = "0.10", features = ["json"] }
-tokio = { version = "0.2", features = ["full"] }
+airtable-proxy = { path = "airtable-proxy" }
 serde = { version = "1.0", features = ["derive"] }
-serde_json = "1.0"

We explicitly clean up the dependencies, moving them into the proxy (or whatever happened here).

Changes:
+1/-8 modified

rustlang/airtable-proxy/Cargo.toml (thread)

@@ -0,0 +1,14 @@
+[package]
+name = "airtable-proxy"
+version = "0.1.0"
+authors = ["Stan Rozenraukh <stan@stanistan.com>"]
+edition = "2018"
+
+[dependencies]
+num-format = "0.4.0"

We can see that we moved them all here.

Changes:
+14/-0 added

rustlang/airtable-proxy/src/gen_schema.rs (thread)

@@ -11,23 +12,25 @@ macro_rules! __gen_inner {
     ) => {
         pub mod gen {
             use super::*;
-            use crate::airtable::FetchCtx;
-            use crate::compose;
-            use crate::error::Error;
-            use crate::gen_schema::Table;
-            use crate::network::request::*;
-            use crate::network::response::One;
-            use crate::transform::*;
-            use serde::{Serialize, Deserialize};
+            use $crate::airtable::FetchCtx;
+            use $crate::compose;
+            use $crate::error::Error;
+            use $crate::gen_schema::Table;
+            use $crate::network::request::*;
+            use $crate::network::response::One;
+            use $crate::transform::*;

Using $crate makes this better to resolve when used as a dependency.

Changes:
+35/-28 renamed

rustlang/airtable-proxy/src/lib.rs (thread)

@@ -0,0 +1,52 @@
+pub use warp;
+pub use serde;
+pub use tokio;
+
+pub mod airtable;
+pub mod config;
+pub mod ctx;
+pub mod error;
+pub mod gen_schema;
+pub mod network;
+pub mod transform;
+
+#[macro_export]
+macro_rules! run_proxy {
+    ($namespace:ident) => {
+        #[tokio::main]
+        async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
+            $crate::run_proxy!(@inner $namespace)
+        }
+    };
+    (@inner $namespace:ident) => {{
+        //
+        // make sure we can airtable, yo, that we have the
+        // right env setup/permissions here.
+        let ctx = $crate::airtable::FetchCtx::from_env()?;
+
+        //
+        // get our server port from the environment, default is 3000
+        let address: ::std::net::SocketAddr = ::std::env::var("HOST")
+            .unwrap_or_else(|_| "127.0.0.1:3000".to_string())
+            .parse()?;
+
+        //
+        // make our context async ready
+        let ctx = $crate::ctx::wrap_ctx(ctx);
+
+        //
+        // grab the generated router
+        //
+        // todo: this should print/log the routes, which means
+        // that `route` methods should probably return a tuple
+        // of the `warp::filter` with some `debug` struct that we
+        // can output here.
+        let router = $namespace::gen::route(ctx);
+
+        //TODO: LOGGER
+        println!("will be serving the app on {}", address);
+        $crate::warp::serve(router).run(address).await;
+
+        Ok(())
+    }};
+}

This is a macro so that it’s nice to run.

Changes:
+52/-0 added

Other Files

File +/-
rustlang/Cargo.lock +14/-6 modified
rustlang/airtable-proxy/.gitignore +2/-0 added
rustlang/airtable-proxy/Cargo.lock +1658/-0 added
rustlang/airtable-proxy/src/airtable.rs +0/-0 renamed
rustlang/airtable-proxy/src/config.rs +0/-0 renamed
rustlang/airtable-proxy/src/ctx.rs +4/-3 renamed
rustlang/airtable-proxy/src/error.rs +0/-0 renamed
rustlang/airtable-proxy/src/network/cache.rs +0/-0 renamed
rustlang/airtable-proxy/src/network/mod.rs +3/-0 added
rustlang/airtable-proxy/src/network/request.rs +0/-0 renamed
rustlang/airtable-proxy/src/network/response.rs +0/-0 renamed
rustlang/airtable-proxy/src/transform.rs +0/-0 renamed
rustlang/src/main.rs +2/-39 modified
rustlang/src/network/mod.rs +0/-3 removed
rustlang/src/schema.rs +2/-6 modified