thruster-rs/thruster

An fast and intuitive rust web framework

built with async/await in mind

Thruster

✅ Runs in stable ✅ Runs fast ✅ Doesn't use unsafe

Documentation

Features

  • hyper compatible
  • ssl ready
  • testable
  • static file serving
  • socketio
  • gRPC, and more experimental non-tonic based gRPC

Motivation

Thruster is a web framework that aims for developers to be productive and consistent across projects and teams. Its goals are to be:

  • Performant
  • Simple
  • Intuitive

Thruster also

  • Does not use unsafe
  • Works in stable rust

Fast

Thruster can be run with different server backends and represents a nicely packaged layer over them. This means that it can keep up with the latest and greatest changes from the likes of Hyper, Actix, or even ThrusterServer, a home-grown http engine.

Intuitive

Based on frameworks like Koa, and Express, thruster aims to be a pleasure to develop with.

Example

To run the example cargo run --example <example-name>. For example, cargo run --example hello_world and open http://localhost:4321/

Middleware Based

The core parts that make the new async await code work is designating middleware functions with the #[middleware_fn] attribute (which marks the middleware so that it's compatible with the stable futures version that thruster is built on,) and then the async_middleware! macro in the actual routes.

A simple example for using async await is:

Error handling

It's recommended to use the map_try! macro from the main package. This has the same function as try!, but with the ability to properly map the error in a way that the compiler knows that execution ends (so there's no movement issues with context.)

This ends up looking like:

Quick setup without a DB

The easiest way to get started is to just clone the starter kit

The example provides a simple plaintext route, a route with JSON serialization, and the preferred way to organize sub routes using sub apps.

Quick setup with postgres

The easiest way to get started with postgres is to install thruster-cli,

And then to run

Which will generate everything you need to get started! Note that this requires a running postgres connection and assumes the following connection string is valid:

This is all configurable and none of it is hidden from the developer. It's like seeing the magic trick and learning how it's done! Check out the docs for thruster-cli here.

Testing

Thruster provides an easy test suite to test your endpoints, simply include the testing module as below:

Make your own middleware modules

Middleware is super easy to make! Simply create a function and export it at a module level. Below, you'll see a piece of middleware that allows profiling of requests:

You might find that you want to allow for more specific data stored on the context, for example, perhaps you want to be able to hydrate query parameters into a hashmap for later use by other middlewares. In order to do this, you can create an additional trait for the context that middlewares downstream must adhere to. Check out the provided query_params middleware for an example.

Other, or Custom Backends

Thruster is capable of just providing the routing layer on top of a server of some sort, for example, in the Hyper snippet above. This can be applied broadly to any backend, as long as the server implements ThrusterServer.

There needs to be:

  • An easy way to create a server.
  • A function to build the server into a future that could be loaded into an async runtime.

Within the build function, the server implementation should:

  • Start up some sort of listener for connections
  • Call let matched = app.resolve_from_method_and_path(<some method>, <some path>); (This is providing the actual routing.)
  • Call app.resolve(<incoming request>, matched) (This runs the chained middleware.)

Using cargo generate

Note: This hasn't yet been updated for the latest version of thruster

If you have cargo generate installed, you can simply run the cargo generator

Issues

Collection of the latest Issues

ynuwenhof

ynuwenhof

Comment Icon1

Hey,

Here's a small collection of alias ideas that I find to be fitting.

Ordered from best to worst, IMO.

use_sub_app

  • nest
  • app
  • sub

use_middleware

  • middleware
  • with
  • mid

I might come up with some more alias ideas soon 👍

Versions

Find the latest versions by id

v1.1.14 - Mar 15, 2022

What's Changed

New Contributors

Full Changelog: https://github.com/thruster-rs/Thruster/compare/v1.1.11...v1.1.14

v1.1.11 - Dec 04, 2021

A small release, simply adds a mitigation for slowloris attacks (via connection timeouts) and adds information about Jab dependency injection library.

v1.1.10 - Sep 18, 2021

  • Adds strict mode, i.e. the ability to strictly distinguish between routes like /a and /a/. This behavior is disabled by default.
  • Adds the actix-web backend server
  • Update the readme a bit ;)

v1.1.9 - Jun 23, 2021

Main changes in this were to remove Sync requirements from futures. Also fixes many of the servers that were broken but hidden behind feature flags.

v1.1.4 - Jun 07, 2021

Quick release, realized that multiple pieces of middleware weren't working, e.g. [profile, plaintext]. Now we have the fix in!

v1.1.3 - Jun 07, 2021

chore: Update version and make release notes feat: add json and set_status methods for basic context (#185) chore: Add middleware how-to to readme chore: Update readme location (#184)

v1.1.2 - Jun 05, 2021

  • Rebuilt entire parser from scratch
  • Rebuilt middleware system from scratch (should be 8% faster now!)
  • Added m! macro to soon replace async_middleware!. They do basically the same thing, except m! doesn't need the context type passed in
  • Upgraded library dependencies

v1.0.1 - Oct 14, 2020

General bug fixes including:

  • Fixing route parameters, which were broken -- eek!
  • Fixing the cookie system to not be so insane
  • Fixing query parameters

Information - Updated Aug 01, 2022

Stars: 876
Forks: 37
Issues: 2

Example application and boiler plate code for Rust, Actix and Heroku

Actix is a popular Rust web framework and these examples get your feet wet

Example application and boiler plate code for Rust, Actix and Heroku

Rust web application

Diesel (ORM) is used to work with the database

Rust web application
Http

799

Rouille, a Rust web micro-framework

Rouille is a micro-web-framework library

Rouille, a Rust web micro-framework

Create a ALB in AWS

Make it point to a lambda called rust-web (needed by deploy

Create a ALB in AWS

Base web app for Rust web projects

Started with the static_index example here:

Base web app for Rust web projects

Simple Rust Web Service

This is the readme file for a Medium article I published on creating a

Simple Rust Web Service

Rust Web assembly game 1024

The game logic has been developed by Rust Programming Language

Rust Web assembly game 1024

Swagger-ui is a crate to use in rust web-servers to render

OpenAPI specification, using swagger-ui-dist into your binary, using rocket

Swagger-ui is a crate to use in rust web-servers to render

rust-actix-react-web-starter

A bare Rust web app that uses Diesel and License

rust-actix-react-web-starter

Example application: Rust, Actix &amp; Heroku

Actix is a popular Rust web framework

Example application: Rust, Actix &amp; Heroku

rust-web-fullstack

Toy projects while learning rust web development

rust-web-fullstack
Facebook Instagram Twitter GitHub Dribbble
Privacy