Bobby
A minimal web framework for Rust.
Note: very much pre-alpha.
Installation
Add Bobby to your Cargo.toml:
[]
= "0.1.2"
Usage
Basic usage looks like this:
use ;
// Create app
let mut app = new;
// Config address
app.with_address;
// Routes
app.get;
app.get;
app.get;
// Run
app.run;
App configuration
Bobby can be configured using with_ methods.
Address and port
To have Bobby listen on a given address and port, use the with_address method:
app.with_address;
If you don't configure this then Bobby will listen on address 127.0.0.1 and port 8080 by default.
Logging
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 = from_default_env;
logger.target;
logger.init;
let mut app = 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
Routing
Routes are added to the instance of Bobby by calling route related methods. An example route looks like this:
app.get;
Supported methods are:
getpostputdeletepatchoptionshead
Requests
Each route function gets a Request instance passed to it as its single argument.
Method
You can see the incoming request' method:
app.get;
URI
You can see the incoming request' URI:
app.get;
Parameters
You can get the route parameters:
app.get;
Responses
Each route must return an instance of Response.
Response: HTML
You can return a HTML response:
app.get;
Response: JSON
You can return a JSON response:
use json;
app.get;
Setting headers
You can set the response headers:
app.get;
Setting status code
You can set the response status:
app.get;