thruster-rs / Thruster

A fast, middleware based, web framework written in Rust
MIT License
1.07k stars 47 forks source link

A few usage questions #141

Closed agersant closed 4 years ago

agersant commented 4 years ago

Hello and thanks for working on Thruster! I have been experimenting with various Rust web frameworks over the past few days to potentially replace my usage of Rocket. So far Thruster has been one of the more promising candidates. I love the simplicity of the API and how easy it is to create, manipulate and pass around the App type.

I have a few questions on how to use the framework correctly:

Many thanks in advance!

trezm commented 4 years ago

Hello and thanks for using Thruster! :)

I'm glad you decided to try it out, it's awesome to have you here.

Let's see about those questions

agersant commented 4 years ago

Thanks for the fast reply!

trezm commented 4 years ago

Hmm for point number 2 I see what you mean. I think the solution here is to make the route /swagger/* and then make the call for route() on BasicHyperContext. You can then use that route to resolve to the actual file -- beware though! Make sure you use the proper security practices to avoid allowing malicious users entering routes like /swagger/../../../super-secret-password or something like that.

For the last point, I think I have a solution that I've been thinking about over the weekend. I'll post on #130 once I've verified that what I think will work actually will work :)

agersant commented 4 years ago

You can then use that route to resolve to the actual file

How would the code in the handler know which part of route()'s output is the route (eg. /swagger) versus the path that can be mapped to the drive (eg /img/logo.png)?

Good point on watching out for directory traversal attacks, I would have missed that!

trezm commented 4 years ago

Sorry about the delayed response, have been taking care of a few personal things on the outside!

How would the code in the handler know which part of route()'s output is the route (eg. /swagger) versus the path that can be mapped to the drive (eg /img/logo.png)?

Let me restate to make sure I understand the question here:

Given a few routes like this: /swagger/data/doc, /swagger/img/logo.png, /swagger/img/cute-puppies.png, how would you make it so that each route wouldn't have to be individually defined, yet you get the correct behavior of /swagger/data/... hitting a swagger middleware and /swagger/img/... hitting an appropriate path?

I would think something like this would work?

app = App::create(generate_context);

app.get("/data/*", async_middleware!(Ctx, [data_handlers]));
app.get("/*", async_middleware!(Ctx, [general_file_handlers]));

Or I'm misunderstanding the question!

trezm commented 4 years ago

I should also mention that I have a PR coming up soon that should handle async file cacheing and serving as a stream as well!

trezm commented 4 years ago

Closing this for lack of activity