It's not inconceivable that people may want to disable any particularly built in middleware. Particularly the logger middleware, but it's not inconceivable that someone may want to disable even the cookies one for some reason.
These would still be on by default.
It seems like this could be achieved without compiler feature flags, in code:
let mut app = tide::configure().skip_logger().with_state(state).build()
since it doesn't seem terribly unlikely tide will need some general way to optionally configure other aspects of the Server in code at some point
I would prefer an explicit way to specify that no middleware should be added (versus having to discover any middleware added in every release and finding the flags/code to disable). The defaults can have appropriate middleware, but the idea is that it is easier to upgrade applications with new Tide versions if I can say I always want a Server without any default middleware and then I add any middleware which "works for me".
Another relevant thing to this conversation: Would anyone actually want to disable cookies? When we talk about "disable all middleware," that currently would include breaking cookie parsing behavior
If I have an API server which does not use cookies, I may not want to have any parsing done for cookies.
Also, if I have a custom session middleware, my concern would be with other middleware potentially interfering with the cookies set. For instance, I want to have private/signed cookies and while there's a PR for that today (which is awesome), there may be requirements which are not met by the API. Then, I would have to always be careful about the cookies or other middleware changing and conflicting with my middleware.
I think having cookie functionality and other default behavior like logging by default is great. Still, as much as I dislike having to choose middleware features (like Express) and just want all the features known to work together (like Rails), I think there are valid use cases where optional functionality is not desired, especially as the framework matures and apps know what they really want.
I'd also prefer having none of the middleware applied by default, especially in the nested app scenario, where default middleware would be applied multiple times. Tide could provide some convenience methods to apply middleware that is used often, or let users apply what they need using Server::middleware.
That's a good point, mounting apps within apps having default middleware issues...
I think the middlewares-running-multiple-times issue could be solved by allowing a middleware to specify that it should only be run once per request and tracking that info on the request. That would also allow for re-entrant routing
I think we could remove the build-in middleware,
...
server.middleware(cookies::CookiesMiddleware::new());
server.middleware(log::LogMiddleware::new());
...
we can add the middleware with custom
I think we can offer some helper methods like .with_logger for built-in middlewares.
let mut app = tide::new().with_cookie().with_logger();
It will be more explicit but still easy and friendly.
I think we can offer some helper methods like
.with_loggerfor built-in middlewares.let mut app = tide::new().with_cookie().with_logger();
Yes! That's in here https://github.com/http-rs/tide/pull/468/files :)
The "hello world" example pulls 167 crates.
Here's a full list, maybe can it help pointing out other crates that can be feature gated:
Expand list of deps
$ cargo clean && cargo build
Updating crates.io index
Compiling proc-macro2 v1.0.18
Compiling cfg-if v0.1.10
Compiling unicode-xid v0.2.1
Compiling syn v1.0.34
Compiling typenum v1.12.0
Compiling libc v0.2.72
Compiling serde_derive v1.0.114
Compiling version_check v0.9.2
Compiling log v0.4.8
Compiling serde v1.0.114
Compiling lazy_static v1.4.0
Compiling wasm-bindgen-shared v0.2.65
Compiling ryu v1.0.5
Compiling bumpalo v3.4.0
Compiling byteorder v1.3.4
Compiling itoa v0.4.6
Compiling serde_json v1.0.56
Compiling futures-core v0.3.5
Compiling futures-sink v0.3.5
Compiling wasm-bindgen v0.2.65
Compiling pin-project-internal v0.4.22
Compiling getrandom v0.1.14
Compiling memchr v2.3.3
Compiling autocfg v1.0.0
Compiling opaque-debug v0.2.3
Compiling once_cell v1.4.0
Compiling subtle v2.2.3
Compiling pin-utils v0.1.0
Compiling futures-io v0.3.5
Compiling proc-macro-hack v0.5.16
Compiling percent-encoding v2.1.0
Compiling byte-tools v0.3.1
Compiling adler v0.2.3
Compiling tinyvec v0.3.3
Compiling slab v0.4.2
Compiling gimli v0.22.0
Compiling matches v0.1.8
Compiling subtle v1.0.0
Compiling cache-padded v1.1.1
Compiling object v0.20.0
Compiling parking v1.0.5
Compiling rustc-demangle v0.1.16
Compiling waker-fn v1.0.0
Compiling ppv-lite86 v0.2.8
Compiling fake-simd v0.1.2
Compiling anyhow v1.0.31
Compiling fastrand v1.3.3
Compiling maybe-uninit v2.0.0
Compiling scoped-tls v1.0.0
Compiling send_wrapper v0.4.0
Compiling zeroize v1.1.0
Compiling async-task v3.0.0
Compiling dtoa v0.4.6
Compiling data-encoding v2.2.1
Compiling pin-project-lite v0.1.7
Compiling base64 v0.12.3
Compiling infer v0.1.7
Compiling httparse v1.3.4
Compiling stable_deref_trait v1.2.0
Compiling route-recognizer v0.1.13
Compiling standback v0.2.9
Compiling error-chain v0.12.2
Compiling time v0.2.16
Compiling futures-channel v0.3.5
Compiling futures-task v0.3.5
Compiling block-padding v0.1.5
Compiling miniz_oxide v0.4.0
Compiling unicode-bidi v0.3.4
Compiling concurrent-queue v1.1.1
Compiling unicode-normalization v0.1.13
Compiling crossbeam-utils v0.7.2
Compiling kv-log-macro v1.0.7
Compiling idna v0.2.0
Compiling socket2 v0.3.12
Compiling num_cpus v1.13.0
Compiling addr2line v0.13.0
Compiling quote v1.0.7
Compiling rand_core v0.5.1
Compiling generic-array v0.12.3
Compiling url v2.1.1
Compiling backtrace v0.3.50
Compiling rand_chacha v0.2.2
Compiling crossbeam-queue v0.2.3
Compiling universal-hash v0.3.0
Compiling block-cipher-trait v0.6.2
Compiling digest v0.8.1
Compiling crypto-mac v0.7.0
Compiling aead v0.2.0
Compiling block-buffer v0.7.3
Compiling polyval v0.3.3
Compiling byte-pool v0.2.1
Compiling aes-soft v0.3.3
Compiling hmac v0.7.1
Compiling rand v0.7.3
Compiling sha2 v0.8.2
Compiling ghash v0.2.3
Compiling hkdf v0.8.0
Compiling aes v0.3.2
Compiling aes-gcm v0.5.0
Compiling wasm-bindgen-backend v0.2.65 // srsly?
Compiling wasm-bindgen-macro-support v0.2.65
Compiling time-macros-impl v0.1.1
Compiling async-attributes v1.1.1
Compiling wasm-bindgen-macro v0.2.65
Compiling time-macros v0.1.0
Compiling pin-project v0.4.22
Compiling futures-util v0.3.5
Compiling cookie v0.14.1
Compiling blocking v0.4.6
Compiling smol v0.1.18
Compiling serde_urlencoded v0.6.1
Compiling serde_qs v0.6.1
Compiling femme v2.1.0
Compiling js-sys v0.3.42
Compiling web-sys v0.3.42
Compiling gloo-timers v0.2.1
Compiling futures-timer v3.0.2
Compiling async-std v1.6.2
Compiling http-types v2.3.0
Compiling async-sse v3.0.0
Compiling async-h1 v2.1.0
Compiling tide v0.11.0
Compiling tide-bloat v0.1.0 (~/tmp/tide-bloat)
Finished dev [unoptimized + debuginfo] target(s) in 50.52s
Source code
$ cat Cargo.toml src/main.rs -p
// cargo.toml
[dependencies]
tide = "0.11.0"
async-std = { version = "1.6.0", features = ["attributes"] }
// src/main.rs
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/").get(|_| async { Ok("Hello, world!") });
app.listen("127.0.0.1:8080").await?;
Ok(())
}
@apiraino Some of the heaviest ones of those are due to https://github.com/async-rs/async-std/issues/823. Please comment on that issue if this matters to you.
@jbr ah interesting :+1: thanks for pointing me to that issue! I commented here because in my test I had also tried removing tide altogether (basically just compiling asynd-std) and the number of crates went down to 52, so I was tricked into believing that they all were deps related directly by tide :smiley:
Tide needs a way to disable logger at runtime too. If I'm using tide-rustls I can't just disable the logger feature since tide-rustls is including the crate with default features.
Maybe it just needs a Server::set_state() to allow doing Server::new().set_state(the_state).with(other_middleware) ?
this is probably a tide-rustls issue βΒ it should use default-features = false
An enthusiastic +1 for putting all optional deps behind feature flags. I want to embed an http server in an application. The only thing it has to do is serve static files from one folder on localhost (eg. no tls). When I added tide without default features and with h1-server and ran cargo update, this is what I got in cargo tree:
βββ tide v0.15.0
β βββ async-h1 v2.1.4
β β βββ async-std v1.7.0
β β β βββ async-global-executor v1.4.3 (*)
β β β βββ async-io v1.2.0 (*)
β β β βββ async-mutex v1.4.0
β β β β βββ event-listener v2.5.1
β β β βββ blocking v1.0.2
β β β β βββ async-channel v1.5.1
β β β β β βββ concurrent-queue v1.2.2 (*)
β β β β β βββ event-listener v2.5.1
β β β β β βββ futures-core v0.3.8
β β β β βββ async-task v4.0.3
β β β β βββ atomic-waker v1.0.0
β β β β βββ fastrand v1.4.0
β β β β βββ futures-lite v1.11.2 (*)
β β β β βββ once_cell v1.5.2
β β β βββ crossbeam-utils v0.8.0
β β β β βββ cfg-if v1.0.0
β β β β βββ const_fn v0.4.3
β β β β βββ lazy_static v1.4.0
β β β β [build-dependencies]
β β β β βββ autocfg v1.0.1
β β β βββ futures-core v0.3.8
β β β βββ futures-io v0.3.8
β β β βββ futures-lite v1.11.2 (*)
β β β βββ kv-log-macro v1.0.7
β β β β βββ log v0.4.11 (*)
β β β βββ log v0.4.11 (*)
β β β βββ memchr v2.3.4
β β β βββ num_cpus v1.13.0 (*)
β β β βββ once_cell v1.5.2
β β β βββ pin-project-lite v0.1.11
β β β βββ pin-utils v0.1.0
β β β βββ slab v0.4.2
β β βββ byte-pool v0.2.2
β β β βββ crossbeam-queue v0.2.3
β β β β βββ cfg-if v0.1.10
β β β β βββ crossbeam-utils v0.7.2
β β β β β βββ cfg-if v0.1.10
β β β β β βββ lazy_static v1.4.0
β β β β β [build-dependencies]
β β β β β βββ autocfg v1.0.1
β β β β βββ maybe-uninit v2.0.0
β β β βββ stable_deref_trait v1.2.0
β β βββ futures-core v0.3.8
β β βββ http-types v2.8.0
β β β βββ anyhow v1.0.34
β β β βββ async-channel v1.5.1 (*)
β β β βββ async-std v1.7.0 (*)
β β β βββ base64 v0.13.0
β β β βββ cookie v0.14.3
β β β β βββ aes-gcm v0.8.0
β β β β β βββ aead v0.3.2
β β β β β β βββ generic-array v0.14.4 (*)
β β β β β βββ aes v0.6.0
β β β β β β βββ aes-soft v0.6.4
β β β β β β β βββ cipher v0.2.5
β β β β β β β β βββ generic-array v0.14.4 (*)
β β β β β β β βββ opaque-debug v0.3.0
β β β β β β βββ cipher v0.2.5 (*)
β β β β β βββ cipher v0.2.5 (*)
β β β β β βββ ctr v0.6.0
β β β β β β βββ cipher v0.2.5 (*)
β β β β β βββ ghash v0.3.0
β β β β β β βββ polyval v0.4.1
β β β β β β βββ cfg-if v0.1.10
β β β β β β βββ universal-hash v0.4.0
β β β β β β βββ generic-array v0.14.4 (*)
β β β β β β βββ subtle v2.3.0
β β β β β βββ subtle v2.3.0
β β β β βββ base64 v0.12.3
β β β β βββ hkdf v0.10.0
β β β β β βββ digest v0.9.0 (*)
β β β β β βββ hmac v0.10.1
β β β β β βββ crypto-mac v0.10.0
β β β β β β βββ generic-array v0.14.4 (*)
β β β β β β βββ subtle v2.3.0
β β β β β βββ digest v0.9.0 (*)
β β β β βββ hmac v0.10.1 (*)
β β β β βββ percent-encoding v2.1.0
β β β β βββ rand v0.7.3 (*)
β β β β βββ sha2 v0.9.2
β β β β β βββ block-buffer v0.9.0 (*)
β β β β β βββ cfg-if v1.0.0
β β β β β βββ cpuid-bool v0.1.2
β β β β β βββ digest v0.9.0 (*)
β β β β β βββ opaque-debug v0.3.0
β β β β βββ time v0.2.23
β β β β βββ const_fn v0.4.3
β β β β βββ libc v0.2.80
β β β β βββ standback v0.2.13
β β β β β [build-dependencies]
β β β β β βββ version_check v0.9.2
β β β β βββ time-macros v0.1.1
β β β β βββ proc-macro-hack v0.5.19
β β β β βββ time-macros-impl v0.1.1
β β β β βββ proc-macro-hack v0.5.19
β β β β βββ proc-macro2 v1.0.24 (*)
β β β β βββ quote v1.0.7 (*)
β β β β βββ standback v0.2.13 (*)
β β β β βββ syn v1.0.50 (*)
β β β β [build-dependencies]
β β β β βββ version_check v0.9.2
β β β β [build-dependencies]
β β β β βββ version_check v0.9.2
β β β βββ futures-lite v1.11.2 (*)
β β β βββ infer v0.2.3
β β β βββ pin-project-lite v0.1.11
β β β βββ rand v0.7.3 (*)
β β β βββ serde v1.0.117 (*)
β β β βββ serde_json v1.0.59
β β β β βββ itoa v0.4.6
β β β β βββ ryu v1.0.5
β β β β βββ serde v1.0.117 (*)
β β β βββ serde_qs v0.7.0
β β β β βββ data-encoding v2.3.1
β β β β βββ percent-encoding v2.1.0
β β β β βββ serde v1.0.117 (*)
β β β β βββ thiserror v1.0.22 (*)
β β β βββ serde_urlencoded v0.7.0
β β β β βββ form_urlencoded v1.0.0 (*)
β β β β βββ itoa v0.4.6
β β β β βββ ryu v1.0.5
β β β β βββ serde v1.0.117 (*)
β β β βββ url v2.2.0 (*)
β β βββ httparse v1.3.4
β β βββ lazy_static v1.4.0
β β βββ log v0.4.11 (*)
β β βββ pin-project-lite v0.1.11
β βββ async-sse v4.1.0
β β βββ async-channel v1.5.1 (*)
β β βββ async-std v1.7.0 (*)
β β βββ http-types v2.8.0 (*)
β β βββ log v0.4.11 (*)
β β βββ memchr v2.3.4
β β βββ pin-project-lite v0.1.11
β βββ async-std v1.7.0 (*)
β βββ async-trait v0.1.42
β β βββ proc-macro2 v1.0.24 (*)
β β βββ quote v1.0.7 (*)
β β βββ syn v1.0.50 (*)
β βββ futures-util v0.3.8 (*)
β βββ http-client v6.2.0
β β βββ async-trait v0.1.42 (*)
β β βββ http-types v2.8.0 (*)
β β βββ log v0.4.11 (*)
β βββ http-types v2.8.0 (*)
β βββ kv-log-macro v1.0.7 (*)
β βββ log v0.4.11 (*)
β βββ pin-project-lite v0.1.11
β βββ route-recognizer v0.2.0
β βββ serde v1.0.117 (*)
β βββ serde_json v1.0.59 (*)
Most helpful comment
I'd also prefer having none of the middleware applied by default, especially in the nested app scenario, where default middleware would be applied multiple times. Tide could provide some convenience methods to apply middleware that is used often, or let users apply what they need using
Server::middleware.