| Crates.io | bobby |
| lib.rs | bobby |
| version | 0.1.2 |
| created_at | 2025-03-01 17:27:24.509928+00 |
| updated_at | 2025-03-02 14:14:39.275252+00 |
| description | A minimal web framework. |
| homepage | https://github.com/askonomm/bobby |
| repository | https://github.com/askonomm/bobby |
| max_upload_size | |
| id | 1573895 |
| size | 34,144 |
A minimal web framework for Rust.
Note: very much pre-alpha.
Add Bobby to your Cargo.toml:
[dependencies]
bobby = "0.1.2"
Basic usage looks like this:
use bobby::{Bobby, Response};
// Create app
let mut app = Bobby::new();
// Config address
app.with_address([127, 0, 0, 1], 3333);
// Routes
app.get("/", |_| Response::html("Hello, <strong>World</strong>"));
app.get("/some/{thing?}", |_| Response::html("An optional route part."));
app.get("/other/{thing}", |_| Response::html("A non-optional route part."));
// Run
app.run();
Bobby can be configured using with_ methods.
To have Bobby listen on a given address and port, use the with_address method:
app.with_address([127, 0, 0, 1], 3333);
If you don't configure this then Bobby will listen on address 127.0.0.1 and port 8080 by default.
Bobby has built-in support for logging with the log interface, so you could use any logging library that supports it to generate logs.
An example with using the env_logger library:
let mut logger = env_logger::Builder::from_default_env();
logger.target(env_logger::Target::Stdout);
logger.init();
let mut app = Bobby::new();
// and so forth ...
Would then generate the following output in your stdout:
[2025-03-02T14:05:51Z INFO bobby::bobby] Listening on 127.0.0.1:3112 ...
[2025-03-02T14:05:56Z INFO bobby::bobby] HTTP/1.1 GET /
[2025-03-02T14:05:57Z INFO bobby::bobby] HTTP/1.1 GET /
[2025-03-02T14:05:57Z INFO bobby::bobby] HTTP/1.1 GET /
[2025-03-02T14:06:01Z INFO bobby::bobby] HTTP/1.1 GET /asd
[2025-03-02T14:06:01Z WARN bobby::bobby] HTTP/1.1 GET /asd - Not found
Routes are added to the instance of Bobby by calling route related methods. An example route looks like this:
app.get("/", |req| {
Response::html("Hello, World.")
});
Supported methods are:
getpostputdeletepatchoptionsheadEach route function gets a Request instance passed to it as its single argument.
You can see the incoming request' method:
app.get("/", |req| {
let method = req.method();
});
You can see the incoming request' URI:
app.get("/", |req| {
let uri = req.uri();
});
You can get the route parameters:
app.get("/hello/{who}", |req| {
let who = req.param("who");
});
Each route must return an instance of Response.
HTMLYou can return a HTML response:
app.get("/", |req| {
Response::html("Hello, World.")
});
JSONYou can return a JSON response:
use serde_json::json;
app.get("/", |req| {
Response::json(json!({
"name": "John"
}))
});
You can set the response headers:
app.get("/", |req| {
Response::html("Hello, World.")
.with_header("Content-Type", "text/html")
});
You can set the response status:
app.get("/", |req| {
Response::html("Not found.")
.with_header("Content-Type", "text/html")
.with_status(404)
});