35 releases (5 breaking)

0.6.13 Oct 11, 2024
0.5.0 Oct 1, 2024
0.1.5 Jul 31, 2024

#115 in HTTP server

Download history 327/week @ 2024-07-27 207/week @ 2024-08-03 7/week @ 2024-08-10 139/week @ 2024-08-17 107/week @ 2024-08-24 53/week @ 2024-08-31 4/week @ 2024-09-07 645/week @ 2024-09-14 516/week @ 2024-09-21 199/week @ 2024-09-28 1604/week @ 2024-10-05 479/week @ 2024-10-12 8/week @ 2024-10-19

2,116 downloads per month

MIT/Apache

54KB
1.5K SLoC

Velvet

(original repo: https://github.com/raffaeleragni/velvet)

crates.io

A layer of republish and small functions to remove some boilerplate on web stacks.

For a reference/example of a project using it: https://github.com/raffaeleragni/veltes

Other sample projects that use velvet:

Stack used

  • WEB: Axum
  • DB: sqlx(postgres,sqlite,mysql)
  • Templating: Askama (folder templates/)
  • Telemetry: sentry supported
  • Metrics: prometheus under /metrics/prometheus

The askama templates and the static RustEmbed will be compiled in and not required at runtime.

The sqlx migrations are not embedded, and will be needed at runtime.

Proc macros cannot be transferred transitively, so crates need to be added again at project root in order to access them. For example tokio or serde.

Base route setup

use velvet_web::prelude::*;

#[tokio::main]
async fn main() {
    App::new().route("/", get(index)).start().await.unwrap();
}

async fn index() -> impl IntoResponse {
    "Hello World"
}

Logging

Default log level is error. To change the level use the env var RUST_LOG=info|debug|warn.

To get structured logging (json logs) pass env var STRUCTURED_LOGGING=true.

example

Add custom metrics

Metrics available at /metrics/prometheus. The custom metrics will be visible as soon as the first use happens, but only when used after App startup, not before. For example, all the routes will work when used like this.

example

Add a database

Adding a .env file with DATABASE_URL=sqlite::memory:, and enabling the feature sqlite in crate velvet_web.

example

Use an HTTP Client

example

Check JWT token (from bearer or cookies)

Adding a .env file with JWT_SECRET=secret and enabling the feature auth in velvet_web.

JWK urls are also supported with a different enum initialization JWT::JWK.setup().await?.

example

Support for static files

Need to include crate rust_embed as this uses proc macros.

example

A more complete example

Using also Askama templates, and JWT through cookie setting.

example

Testing routes

example

Embedded login(and registration) flow

example

Sending mails

example

example

Default routes already implemented

  • Status (no-op): http GET /status/liveness
  • Metrics: http GET /metrics/prometheus

ENV vars

  • SERVER_BIND: [default] default (0.0.0.0) bind network for which to listen on
  • SERVER_PORT: [number] (default 8080) port for which to listen on
  • DATABASE_URL: postgres://user:pass@host:port/database (if database used) or sqlite::memory:...
  • DATABASE_MAX_CONNECTIONS: [number] (default 1)
  • STRUCTURED_LOGGING: true|false (default false)
  • SENTRY_URL: url inclusive of key for sending telemetry to sentry

To setup TLS use env vars:

  • TLS=true (or any string)
  • TLS_PEM_CERT=cert.pem
  • TLS_PEM_KEY=key.pem

To setup SMTP/MAIL:

Mail setup works only with TLS (port 465 if not set)

  • MAIL_FROM=test@test.com
  • MAIL_HOST=localhost
  • MAIL_PORT=2525
  • MAIL_USERNAME=user
  • MAIL_PASSWORD=password
  • MAIL_ACCEPT_INVALID_CERTS=true if in develop mode

Dependencies

~35–75MB
~1.5M SLoC