tide icon indicating copy to clipboard operation
tide copied to clipboard

expose Method in tide mod

Open prabirshrestha opened this issue 4 years ago • 11 comments

Currently in order to use Method I need to import use tide::http::Method while for status code I can simply write use tide::StatusCode.

Imports are looking like

use tide::{http::Method, Request, Response, StatusCode};

Would it be possible to also expose Method as tide::Method to align with rest of the apis? Not sure if it was deliberate.

prabirshrestha avatar May 16 '21 20:05 prabirshrestha

I think it was suspected that perhaps Method would not be so commonly used in Tide as opposed to Surf?

Fishrock123 avatar May 17 '21 23:05 Fishrock123

That does make sense. @prabirshrestha when do you find yourself using Method in a tide app? That also might indicate somewhere the api could be better

jbr avatar May 17 '21 23:05 jbr

This is usefull when using .all().

app.at("/webdav/").all(handle_webdav);

async fn handle_webdav(req: Request<()>) -> tide::Result {
   match req.method() => {
       Method.OPTIONS => {},
       Method.GET => {},
       _ => {}
   };
   ...
}

prabirshrestha avatar May 18 '21 01:05 prabirshrestha

Why wouldn't you use the router in that circumstance?

jbr avatar May 18 '21 01:05 jbr

I actually want to ship a webdav handler as a library that others can easily consume it as below.

async fn handle_webdav(req: Request<State>) -> tide::Result {
    let webdav = WebdavHandler::new(vfs);
    Ok(webdav.handle(&req).await?)
}

I had originally started with Webdav middleware but faced similar issue.


app.with(WebdavMiddleware::new(vfs));
app.at("*").all(|| async { Ok(Response::new(StatusCode::NotFound)) };

Either way I still need to match on Method.

prabirshrestha avatar May 18 '21 01:05 prabirshrestha

Why not ship a tide Server that can be nested? Regardless, this seems like very rare usage that probably doesn't require making Method easier to access

jbr avatar May 18 '21 01:05 jbr

I did experiment with it but hit the trailing slash issue. https://github.com/http-rs/tide/issues/205#issuecomment-841729547. Though this is a minor issue that I could wait and update when fixed.

As mentioned in https://github.com/http-rs/tide/issues/830, I'm hoping to have a generic handler that takes Request and returns Result<Response> from http_types so trying to avoid tide specific code if possible. Then it might not matter since now will be use Method from http_types instead.

prabirshrestha avatar May 18 '21 01:05 prabirshrestha

Are there other server frameworks that use http_types that you're hoping to support?

jbr avatar May 18 '21 01:05 jbr

I'm not aware of any but just experimenting with ideas.

Ideally one could write Request -> Result<Response> fn and make it work in other frameworks. Then might be one could just have to_actix_middleware(webdav_handler) or to_tide_middleware(webdav_handler)

prabirshrestha avatar May 18 '21 02:05 prabirshrestha

If you're trying to avoid tide types, wouldn't you just directly depend on http_types::Method?

jbr avatar May 18 '21 02:05 jbr

Yes I would in this case. But did find it weird that I had to use tide::http_types::Method. In other cases where I don't want to convert all to http_types I was hoping to just use tide::Method as it felt more natural.

This was just a minor api difference I saw so not a major issue for me.

prabirshrestha avatar May 18 '21 02:05 prabirshrestha