Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tide support #79

Closed
prabirshrestha opened this issue Jun 5, 2020 · 6 comments
Closed

Add tide support #79

prabirshrestha opened this issue Jun 5, 2020 · 6 comments

Comments

@prabirshrestha
Copy link
Contributor

https://github.com/http-rs/tide

I'm currently doing something like this.

pub async fn not_found(_ctx: Request<AppState>) -> tide::Result {
    let mut buf = Vec::new();
    templates::statuscode404(&mut buf)?;

    Ok(Response::new(StatusCode::NotFound)
        .body(Body::from(buf))
        .set_mime(mime::TEXT_HTML_UTF_8))
}
@prabirshrestha
Copy link
Contributor Author

Here is a trait so it is now easier to use template with tide.

use renderer::Render;

pub async fn not_found_route(_req: Request<AppState>) -> tide::Result {
    let mut res = Response::new(StatusCode::NotFound);
    res.render_html(|o| Ok(templates::notfound(o)?))?;
    Ok(res)
}
pub trait Render {
    fn render<Call>(&mut self, call: Call) -> std::io::Result<()>
    where
        Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;

    fn render_html<Call>(&mut self, call: Call) -> std::io::Result<()>
    where
        Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;
}

impl Render for tide::Response {
    fn render<Call>(&mut self, call: Call) -> std::io::Result<()>
    where
        Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
    {
        let mut buf = vec![];
        call(&mut buf)?;
        self.set_body(buf);
        Ok(())
    }

    fn render_html<Call>(&mut self, call: Call) -> std::io::Result<()>
    where
        Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
    {
        self.render(call)?;
        self.set_content_type(tide::http::mime::HTML);
        Ok(())
    }
}

@kaj
Copy link
Owner

kaj commented Jul 30, 2020

The tide example is extended in #94

While doing that I discovered that tide uses a Mime type from http-types rather than the mime crate. So I'll do a separate PR providing a feature for that include a feauture for that in #94 (making it easier to test with the example in the same PR).

@kaj
Copy link
Owner

kaj commented Aug 7, 2020

Merged the example in #94 . The rendering traits can still be moved from the example to the feature, but that can wait for another PR.

@prabirshrestha
Copy link
Contributor Author

Awesome. Thanks for improving this.

For now I wouldn't go and add it as a feature since the api for tide is changing quite rapidly. Might be can revisit in the next few months once the api are stable.

@kaj
Copy link
Owner

kaj commented Aug 8, 2020

I already did add a tide013 feature, but that is minimal, it only provides the http-types Mime support through the tide::http reexport.

@kaj
Copy link
Owner

kaj commented Nov 15, 2023

I'll close this. The minimal feature and example exists, but tide itself has not been updated the last two years, so maybe not the most relevant web framework anymore.

@kaj kaj closed this as completed Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants