| Crates.io | starberry |
| lib.rs | starberry |
| version | 0.6.5 |
| created_at | 2025-02-25 04:38:51.273603+00 |
| updated_at | 2025-07-24 02:48:11.790199+00 |
| description | Small, sweet, easy framework for full-stack web application |
| homepage | https://fds.rs |
| repository | https://github.com/Redstone-D/starberry |
| max_upload_size | |
| id | 1568728 |
| size | 75,353 |
Small, sweet, easy framework for full-stack Rust web applications
Starberry is a lightweight, intuitive web framework focused on simplicity and productivity. It supports regex-based routing, tree-structured URLs, and integrates seamlessly with the Akari templating system.
MSRV: 1.86
flowchart TD
RX1[Get Connection from TcpListener]
RX2{Multiple Protocol Mode?}
RX3["Test each protocol with Rx::test()"]
RX4[Select first protocol returning true]
RX5[Use that protocol type]
RX6[Use the single protocol directly]
RX7[Process request: read/write as needed]
TX1[Build Tx and Connection]
TX2["Tx::send(): send Response, get Request"]
TX3{Is connection needed?}
TX4[Close connection]
TX5[Keep connection open]
RX1 --> RX2
RX2 -- Yes --> RX3
RX3 --> RX4
RX4 --> RX5
RX5 --> RX7
RX2 -- No --> RX6
RX6 --> RX7
RX7 --////--> TX1
TX1 --> TX2
TX2 --> TX3
TX3 -- No --> TX4
TX3 -- Yes --> TX5
As the diagram shown above, Rx (Receive) and Tx (Transmit) are the 2 most important traits introduced in Starberry 0.6. All of the protocols are wrapped with them.
This means that you will be able to define your own protocol in App, and very soon you will be able to use socket and ftp in starberry. Currently Http and Sql has been implemented
For Http, a simple method is provided for sending a request to a server
let response = HttpResCtx::send_request(
"https://api.pmine.org", // Host, protocol, port
get_request("/num/change/lhsduifhsjdbczfjgszjdhfgxyjey/36/2"), // Request content
HttpSafety::new().with_max_body_size(25565), // Safety configuration
)
.await
.unwrap();
There are also other protocols that does not provide a shortcut or you maybe want to use more advanced settings for http request, you may use the following way
(1) Building a connection with the server
let builder = ConnectionBuilder::new("example.com", 443)
.protocol(Protocol::HTTP)
.tls(true);
let connection = builder.connect().await.unwrap();
(2) Create a new Tx (HttpResCtx is the struct implemented Tx in Http)
let mut request = HttpResCtx::new(
connection,
HttpSafety::new().with_max_body_size(25565),
"example.com"
);
(3) Insert the request by using the Tx::prcess() function
let _ = request.process(request_templates::get_request("/")).await;
Because we need to borrow the reader from the HttpResCtx so we didn't use the &mut Response from the process() function, we directly use HttpReqCtx.response to further process
request.response.parse_body(
&mut request.reader,
1024 * 1024,
).await;
println!("{:?}, {:?}", request.response.meta, request.response.body);
Protocol registry now manages middleware chains and handler registration through a unified API
The App type no longer owns a global Url tree or middleware list.
URLs and middleware must now be configured on the protocol level during protocol registration.
You
/// APP only have one inbound protocol
pub static APP: SAPP = Lazy::new(|| {
App::new()
.single_protocol(ProtocolBuilder::<HttpReqCtx>::new())
.build()
});
/// APP need multiple inbound protocols
pub static APP_MULTI: SApp = Lazy::new(|| {
App::new()
.protocol(HandlerBuilder::new()
.protocol(ProtocolBuilder::<HttpReqCtx>::new())
// .protocol(ProtocolBuilder:: /* .. /* ) <- Add another protocol
)
.build()
});
Starberry now includes first-class PostgreSQL support via the starberry_sql crate. This async Rust library provides:
SqlQuery/sql! macro)FromRowSqlPool)Basic Example:
use starberry_sql::{DbConnectionBuilder, sql, FromRow, SslMode};
#[derive(FromRow)]
struct User { id: i32, name: String }
#[lit_url(APP, "/users")]
async fn get_users(mut ctx: HttpReqCtx) -> HttpResponse {
let builder = DbConnectionBuilder::new("localhost", 5432)
.database("my_db")
.ssl_mode(SslMode::Disable);
let mut conn = builder.connect().await.unwrap();
let users: Vec<User> = sql!("SELECT id, name FROM users")
.fetch_all_as(&mut conn)
.await
.unwrap();
json_response(&users)
}
We are now able to add config to both APP and urls. For APP, we are able to set the config/static by using methods
pub static APP: SApp = Lazy::new(|| {
App::new()
.single_protocol(ProtocolBuilder::<HttpReqCtx>::new()
.append_middleware::<CookieSession>() // Append the cors middleware. The cors middleware's setting is a AppCorsSettings data
)
.set_config( // Add a config data, which is a TypeID, Any HashMap
prelude::cors_settings::AppCorsSettings::new() // Store a data with the type of AppCorsSettings into config. The middleware will be able to figure this stored data out by using its TypeID
)
.set_local( // Set a static data, which is a String, Any HashMap. When getting data you need to specify both String and Type
"key": "MySuperSecuredAdminKeyWhichIsSoLongThatCanProtectTheServerFromAnyAttack"
)
.build()
});
While for the Url, we are able to store params by using the #[url] macro
#[url(APP.reg_from(&[TEST_URL.clone(), LitUrl("get")]), config=[HttpSafety::new().with_allowed_method(HttpMethod::GET)])]
async fn get_only() -> HttpResponse {
text_response("Get only")
}
#[url(APP.reg_from(&[TEST_URL.clone(), LitUrl("post")]), config=[HttpSafety::new().with_allowed_methods(vec![HttpMethod::POST])])]
async fn post_only() -> HttpResponse {
text_response("Post only")
}
You may see in the new Http mod, request and responses are in the same structure of
pub struct HttpRequest {
pub meta: HttpMeta,
pub body: HttpBody
}
pub struct HttpResponse {
pub meta: HttpMeta,
pub body: HttpBody
}
Where HttpMeta and HttpBody both implemented different methods for sending/parsing request/response
Install starberry bu using
cargo install starberry
After installing starberry, use
starberry new <Proj_name>
To create a new project
use starberry::prelude::*;
#[tokio::main]
async fn main() {
APP.clone().run().await;
}
pub static APP: SApp = Lazy::new(|| {
App::new().build()
});
#[lit_url(APP, "/")]
async fn home_route() -> HttpResponse {
text_response("Hello, world!")
}
Visit your server at http://localhost:3003
project/
├── src/
│ ├── main.rs
│ ├── lib.rs
│ └── ...
├── programfiles/
│ ├── config.json
│ └── ...
└── templates/
│ ├── base.html
| ├── index.html
| └── ...
└── build.rs
Program file folder is used to store the config of the program data generated during the process of running the program. The files are automatically copied to the dist directory when you run starberry build.
Templates are automatically copied to the dist directory when you run starberry build.
You may visit our webpage to go through the quick tutorial for starberry
https://fds.rs/starberry/tutorial/0.6.4/ <- New version
https://fds.rs/starberry/tutorial/0.4.7/
cargo install sbmstd
https://crates.io/crates/sbmstd
| Version | Download | Notes |
|---|---|---|
| ^0.6.4 | cargo install starberry@0.6.5 |
Multi Protocol Support |
| 0.4.7 | cargo install starberry@0.4.7 |
Async + Request Context |
| 0.3.3 | cargo install starberry@0.3.3 |
Sync Starberry |
starberry build/run/release now behaves the same as cargo build/runAll planned updates for 0.6 is already finished
Learn more about Akari template: https://crates.io/crates/akari/
Go to our homepage: https://fds.rs
MIT License