| Crates.io | faithea |
| lib.rs | faithea |
| version | 0.1.6 |
| created_at | 2025-12-24 04:56:52.259141+00 |
| updated_at | 2026-01-10 09:22:38.474882+00 |
| description | A HTTP framework |
| homepage | |
| repository | https://github.com/bigoldcat123/graduation |
| max_upload_size | |
| id | 2002747 |
| size | 173,928 |
#[get("/")]
async fn hello_world() {
"Hello,World"
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
HttpServer::builder()
.mount("/", handlers!(hello_world))
.build()
.start()
.await;
}
#[get("/**")]
async fn static_file_map() {
static_map(&_req, "/path/to/directory").await
}
#[get("/file")]
async fn file() {
StaticFile("/path/to/file")
}
/searchParam?name=hello&age=100#[get("/searchParam")]
async fn search_param(
#[search_param] name: usize,
#[search_param] age: String,
) {
println!("name: {}, age:{}, }",name,age,);
"good"
}
#[get("/pathParam/{name}/{age}")]
async fn path_params(
name: String,
age: usize,
) {
println!("name: {}, age:{}",name,age);
res_modifiers!("")
}
// Serialize for request body
// Deserialize for response body
#[derive(Serialize, Deserialize)]
struct Stu {
name: String,
age: i32,
}
#[post("/json")]
async fn search_params_and_path_params_and_json(
stu: Json<Stu>,
) {
stu
}
derive from MultipartData
the type of field shoule impl
TryFrom<Part>
#[derive(MultipartData, Debug)]
struct StuInfo {
pub name: String,
pub age: i32,
pub merried: Option<bool>,
pub profile: MultiPartFile,
}
#[post("/multipart")]
async fn multipart(data: Multipart<StuInfo>) {
let data = data.into_inner();
println!(
"{:?} {:?} {:?} {:?}",
data.age, data.name, data.profile, data.merried
);
"ok"
}
async fn guard_ok(req:HttpRequest) -> Result<HttpRequest,HttpResponse> {
Ok(req)
}
async fn guard_err(req:HttpRequest) -> Result<HttpRequest,HttpResponse> {
Err(HttpResponse::not_found())
}
HttpServer::builder()
.mount("/", handlers!(hello_world))
.guard("/**", async |e:HttpRequest| {
println!("new req -> ");
Ok(e)
})
.guard("/a",guard_ok)
.guard("/b",guard_err)
.build()
.start()
.await;
#[derive(Debug, Serialize, Deserialize)]
struct Stu {
name: String,
age: i32,
}
impl TryFrom<&mut HttpRequest> for Stu {
type Error = String;
fn try_from(value: &mut HttpRequest) -> Result<Self, Self::Error> {
Ok(Stu {
name: "from req".into(),
age: 111,
})
}
}
#[post("/fromRequest")]
async fn fromRequest(stu:FromRequest<Stu>) {
serde_json::to_string(&stu.into_inner()).unwrap()
}
impl TryConvertFrom<Option<&String>> for String {
fn try_convert_from(value: Option<&String>) -> Result<Self, FuError> {
if let Some(value) = value {
Ok(value.to_string())
} else {
Err("missing value".into())
}
}
}
HttpResponseModifier for your structpub trait HttpResponseModifier {
fn modify<'a>(
&'a self,
res: &'a mut HttpResponse,
) -> std::pin::Pin<Box<dyn Future<Output = Result<(), FuError>> + 'a + Send + Sync>>;
}
impl HttpResponseModifier for MyStruct {
fn modify<'a>(
&'a self,
res: &'a mut HttpResponse,
) -> std::pin::Pin<Box<dyn Future<Output = Result<(), FuError>> + 'a + Send + Sync>> {
Box::pin(async move {
/// your code to modify response
Ok(())
})
}
}
using modifiers!() to return multiple modifier
you can have an access to HttpRequest in any handler through _req
make your struct be the field in a multipartData
just impl the std TryFrom
#[derive(Debug)]
struct A{
}
impl TryFrom<Part> for A {
type Error = FuError;
fn try_from(value: Part) -> Result<Self, Self::Error> {
Ok(Self{})
}
}
#[derive(MultipartData, Debug)]
struct StuInfo {
pub name: Vec<A>,
pub age: i32,
pub merried: Option<bool>,
pub profile: Vec<MultiPartFile>,
}