Type-safe, async Rust web framework with intuitive API.
#[macro_use] extern crate rocket;
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![index, users])
.mount("/api", routes![api_routes])
.attach(DbConn::fairing())
.manage(AppState::new())
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
// Path parameters
#[get("/users/<id>")]
fn get_user(id: i64) -> Json<User> {
Json(find_user(id))
}
// Optional parameters
#[get("/users?<page>&<limit>")]
fn list_users(page: Option<u32>, limit: Option<u32>) -> Json<Vec<User>> {
let page = page.unwrap_or(1);
let limit = limit.unwrap_or(10);
Json(fetch_users(page, limit))
}
// Multiple segments
#[get("/files/<path..>")]
fn get_file(path: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join(path)).ok()
}
use rocket::request::{FromRequest, Outcome};
struct AuthUser {
id: i64,
role: String,
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for AuthUser {
type Error = AuthError;
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
match req.headers().get_one("Authorization") {
Some(token) => match validate_token(token).await {
Ok(user) => Outcome::Success(user),
Err(e) => Outcome::Error((Status::Unauthorized, e)),
},
None => Outcome::Forward(Status::Unauthorized),
}
}
}
#[get("/protected")]
fn protected(user: AuthUser) -> String {
format!("Hello, {}", user.id)
}
use rocket::serde::json::Json;
#[derive(Serialize, Deserialize)]
struct CreateUser {
name: String,
email: String,
}
#[post("/users", data = "<user>")]
fn create_user(user: Json<CreateUser>) -> Result<Json<User>, Status> {
let user = user.into_inner();
match insert_user(&user) {
Ok(created) => Ok(Json(created)),
Err(_) => Err(Status::InternalServerError),
}
}
struct AppState {
config: Config,
cache: Cache,
}
#[launch]
fn rocket() -> _ {
rocket::build()
.manage(AppState::new())
.mount("/", routes![handler])
}
#[get("/config")]
fn handler(state: &State<AppState>) -> String {
state.config.name.clone()
}
use rocket::fairing::{Fairing, Info, Kind};
struct RequestLogger;
#[rocket::async_trait]
impl Fairing for RequestLogger {
fn info(&self) -> Info {
Info {
name: "Request Logger",
kind: Kind::Request | Kind::Response,
}
}
async fn on_request(&self, req: &mut Request<'_>, _: &mut Data<'_>) {
println!("Request: {} {}", req.method(), req.uri());
}
async fn on_response<'r>(&self, _: &'r Request<'_>, res: &mut Response<'r>) {
println!("Response: {}", res.status());
}
}
#[catch(404)]
fn not_found() -> Json<ErrorResponse> {
Json(ErrorResponse { error: "Not found".into() })
}
#[catch(500)]
fn internal_error() -> Json<ErrorResponse> {
Json(ErrorResponse { error: "Internal server error".into() })
}
#[launch]
fn rocket() -> _ {
rocket::build()
.register("/", catchers![not_found, internal_error])
}
&State<T> over global stateRocket.toml for environment-specific configrocket::local::blocking::Client for testsSearch for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer