- Notifications
You must be signed in to change notification settings - Fork2
Progressive RESTful framework
edezhic/prest
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
ProgressiveRESTful framework that aims tomake application development simple again. Even if you are not familiar with Rust yet you might be interested because it's designed to be as beginner-friendly as possible. Tutorials are available in theblog which is also built with prest. Beware that its still alpha and can only be recommended for pet projects and training because many breaking changes are expected.
It ain't easy to compete with laravel, rails, nextjs and many others, but I always wanted a framework which enables simplicity in common development needs and allowsany customizations/optimizations without switching languages. Rust provides ways to build servers, clients, AIs, blockchains, OS kernels and whatever else you might need, while also being arguably the mostreliable practical language. Thanks to a lot ofamazing libraries in the rust ecosystem prest re-exports a comprehensive development toolkit and adds a bunch of integrations and features on top of them for simplicity:
Create a default rust project, addprest
dependency, bulkuse
everything from it, addinit
macro and make yourmain
async. No massive boilerplates, no custom required CLI tools.
Cargo.toml
[dependencies]prest ="0.5"
src/main.rs
use prest::*;#[init]asyncfnmain(){ ...}
By default it reads the env variables, initializes the runtime, logging, database and other prest subsystems.
High-performance, concurrent, intuitively routed. Based onaxum so it includes powerful middleware api, simple extractors to get information handlers need from requests and flexible return types. But prest also enchances it with a bunch of additional utilities to get started quickly - justrun().await
your router and everything else will be configured automatically.
route("/",get("Hello world")).run().await
You can also add logic after theawait
in case you want to run smth during the shutdown.
For deserialization of incoming data there is a small utility extractorVals<T>
which extracts fields from the query in GET requests and expects json bodies for other methods, for example:
route("/send_data",post(|Vals(data):Vals<Data>|async{/* data will be of type Data */}))
Router is built by composing routes and other routers like this:
let router =route("/path",/* handler */).route("/:path_param",/* handler */).layer(/* middleware */).nest(/* another router */);
For more details about routing, handlers and layers check outaxum's docs. Under the hood it's powered bytokio stack for extreme speed and reliability. Also, if you need to access runtime directly prest exportsRT
static which you can use.Serialize
andDeserialize
macros (check outserde
for details) are re-exported from prest as well.
While axum provides a way to manage shared state it's relatively verbose so prest provides a bit easier solution -state!
macro:
state!(GLOBAL:String ={ sync_function()?});state!(GLOBAL:String =async{ async_function().await?});
It works with both sync and async initialization code, and supports?
operator inside. Under the hood it's using standardLazyLock
,anyhow
-based return type to support?
(but beware that errors in state initialization will panic and stop the app), and tokio's runtime when execution of async code is needed. Variables themselves are statics which can be used anywhere in your codebase, just keep in mind that if you'll need mutable globals you'll have to useRwLock
,Mutex
or smth alike.
html!
macro for rust'y templating forked frommaud, easy styling with built-intailwind classes which are compiled into css inside html, simple client-server interactions withhtmx and it's aliases, unlimited flexibility withhyperscript. Smooth UX without separate front-end stacks:
html!{ nav $"w-full bg-gray-900 rounded-full"{ input $"text-xs lg:text-md" name="query" post="/search" into="#search-results"{}} ... div #search-results{"response will be here!"}}
For more details about these tools I suggest checking out their docs, overall they are pretty simple and intuitive by themselves but powerful enough for the vast majority of apps. Default prest bundle includes tailwind's presets and patched htmx version which sends non-GET request payloads in json format to easily use withVals
and includes a few other tweaks for better compatability.
Embedded DB that works without running separate services based onGlueSQL for compatibility with SQL andsled for high-performance storage. Prest enchances them with theStorage
macro to automatically derive schema based on usual rust structs, and some helper functions to easily interact with the underlying tables without worrying about SQL injections:
#[derive(Storage,Deserialize)]structTodo{id:Uuid,task:String,done:bool,}...Todo::get_all().await?;Todo::select_by_task("Buy milk").await?;Todo::select().filter(col("done").eq(true)).order_by("task").rows().await?;let todo =Todo{id:Uuid::now_v7(),task:"Buy milk".into(),done:false,};todo.save().await?;todo.update_done(true).await?;assert!(todo.check_done(true).await?);todo.remove().await?;
It's aimed to support all the basic types supported by GlueSQL,Option
,Vec
, as well as custom ones which can be serialized/deserialized. As of nowStorage
also requires derivedDeserialize
trait for the DB editor in the...
Monitors host system's resources, collects filtered stats for requests/responses with their timings, high-level info and detailed traces, provides read/write GUI to tables, tracks scheduled tasks, and provide controls over remote host in local builds. While blog intentionally exposes access to it for demo purposes (cog in the menu), by default it is protected by...
Session and user management using passwords and OAuth/openID protocols. Based on the built-in DB,openidconnect-rs,axum-login andpassword-auth. Persisted in the built-in DB, can be initiated by leading users to the predefined routes, and can retrieve current auth/user info using extractors:
html!{// for username/password flow form method="POST" action=(LOGIN_ROUTE){ ...}// for oauth flow a href=(GOOGLE_LOGIN_ROUTE){"Login with Google"}}...route("/authorized-only",get(|user:User|async{"Hello world"}));route("/optional",get(|auth:Auth|async{"auth.user is Option<User>"}));
To enable it you'll need theauth
feature of prest:
prest = {version ="0.5",features = ["auth"] }
If someone requests a route which requires authorization401 Unauthorized
error will be returned.
There is also a rust-based cron alternative for background tasks based ontokio-schedule and enchanced with some utilities and integrations. They can be spawned as easy as:
// can return either `()` or `Result<(), E: Display>` to enable `?`RT.every(5).minutes().spawn(||async{do_smth().await})RT.every(1).day().at(hour, minute, second).spawn(...)
You can also give names to your scheduled tasks and prest will collect additional stats over their timings and execution results:
RT.every(3).hours().schedule("my regular task", ||async{ ...})RT.every(2).days().at(hour, minute, second).schedule(...)
Logging is powered bytracing ecosystem withtrace!
,debug!
,info!
,warn!
anderror!
macros:
info!("My variable value is {}", x);// supports same formatting as `format!` macro
Prest initializes a subscriber which collects these records into several streams: high-levelINFO
+ level logs are written in html format to be observed in the main admin panel page (and the shell in debug builds), and traces of all levels are also written in a non-blocking fashion to files split by days in thejson
format, which can be also explored through special page in the admin panel. By default prest filters low-level logs of its dependencies to avoid spamming your feeds, and you can also add more filters in the argument to theinit
macro:
// like in the `scraping` example#[init(log_filters=[("html5ever","info"),("selectors","info")])]
There is also another prest crateprest-build
. Unlike usual dependencies it goes into build deps section:
[build-dependencies]prest-build ="0.3"
It includes a couple of optional features -sass
andtypescript
which allow transpilation and bundling for typescript/js and sass/scss/css respectfully:
// paths relative to the build scriptbundle_ts("path to main ts/js file");bundle_sass("path to main sass/scss/css file");
And their compiled versions can be embedded withembed_build_output_as!
macro:
embed_build_output_as!(BuiltAssets);...router.embed(BuiltAssets)
They can be requested with the same name as originals butts
/tsx
andscss
/sass
extensions will be replaced withjs
andcss
accordingly.
Also, there is a similar and more flexible macroembed_as!
which can be used with arbitrary folders and files, and this macro is designed to read files from the hard drive as needed in debug builds to avoid slowing down compilation, but in release builds it will embed their contents into the binary and you'll get single-file binary with your whole app in it for convenience and faster file access. These macros generate rust structures which provide access for files' contents and metadata. Here is a snippet from the blogs internals which embeds projects files to render on its pages:
embed_as!(ExamplesDocs from"../" only"*.md");embed_as!(ExamplesCode from"../" except"*.md");
General syntax:
embed_as!(StructName from"path" only"path or wildcard"/*, "..."*/ except"path or wildcard"/*, ...*/)
Such structs can be embedded into the router like this:.embed(StructName)
.
Prest supports 1 click build-upload-start deploy script to linux servers through ssh and sftp, based on docker for cross-platform compilation, and comes with automatically configured TLS based on LetsEncrypt. To make it work you'll need to have docker engine installed, specify the domain in theCargo.toml
and provide credentials:
[package.metadata]domain ="prest.blog"
# add when starting app locally in the shell or in the .env fileSSH_ADDR=123.232.111.222SSH_USER=rootSSH_PASSWORD=verystrongpassword
And just click theDeploy
button in the local admin panel! You can also manage deployments there: stop the current one, start a previous one, or cleanup old builds. Note that without domain it will not configure TLS.
Anyway, even with the fastest framework and server users will still have to face network delays and you may want to provide more native-app-like experience so...
Coreprest-build
function is to build some of your routes into a WASM-based Service Worker and compose a Progressive Web Application so that your users can install it and access these routes offline. To make it work you'll need to addwasm-bindgen
dependency andprest-build
build-dependency, separate host-only from shared host+client code and initialize shared routes in the SW, and add a lil build script:
[dependencies]wasm-bindgen ="0.2"
#[wasm_bindgen(start)]pubfnmain(){shared_routes().handle_fetch_events()}
use prest_build::*;fnmain(){build_pwa(PWAOptions::new()).unwrap();}
To embed the compiled assets into the host you can use the sameembed_build_output_as!
macro. By default it will only run full PWA build in the--release
mode to avoid slowing down usual development, but you can usePWA=debug
env variable to enforce full builds. The general idea is to render some of the templates on the client-side to provide extremely fast responses while also supporting server-side renders as a fallback and indexing mechanism. And get it all with just a few lines of boilerplate code. If PWA experience is not enough for you there is another available option...
Running host functionality with a webview for offline-first apps. Somewhat like Electron but with much smaller and faster binaries. Based on the same libraries asTauri but for rust-first apps. To build for desktops just enable webview feature like this:
prest = {version ="0.5",features = ["webview"] }
This is quite different from server-first or PWA apps and require quite different architecture, especially around auth and similar components. For mobile platforms you'll need to dosome work as of now, but hopefully this will be mostly automated as well.
And the story doesn't end here. Prest host includes a graceful shutdown mechanism which awaits currently processing requests and in-progress scheduled tasks before exiting,Server Sent Events
utils to easily stream data to the clients, a whole bunch of small utils likeVals
extractor andok()
function which can wrap return values of handler closures to provide to allow using?
operator inside of them. If you think that prest is missing some feature which may be useful for you or for modern app development in general - please add an issue inthe repo!
If you aren't familiar with rust yet I strongly recommend to check outThe Rust Book - definitely the best guide with interactive examples available in dozens of languages! Also, I suggest skimming through the first three chapters of theasync book to get an overall understanding how concurrency works in rust.
Prest tutorials are designed to start from basics and then add more and more features on top:
- Todo = basic full-stack todo app in just about 50 lines of code
- PWA = 1 + PWA capabilities and an offline view, ~80 LoC
- Auth = 2 + username+password and Google auth, ~110 LoC
- Sync = 3 + synchronization between clients, ~130 LoC
There are also todo examples with alternative databases - postgres throughseaorm ordiesel, sqlite throughsqlx orturbosql,mongo,redis. Also, there is a couple of examples that showcase how one might use prest with uncommon for web development tech:web scraper,Large Language Model andSolana blockchain program.
To run locally you'll need the latest stablerust toolchain. I also recommend setting up therust-analyzer for your favourite IDE right away. To build & start any example from the cloned prest repo usecargo run -p EXAMPLE-NAME
. Or just copy the selected example's code from the tutorials into local files andcargo run
it. Some examples require additional setup and credentials which are mentioned in their docs.
About
Progressive RESTful framework
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.