DEV Community

heige
heige

Posted on

2

rs-api is based on a web/api application created by axum crate

How do you create an easy-to-use rust web/api project?
Take a few minutes to read the following and you'll be ready to create a project quickly.

rs-api

https://github.com/daheige/rs-api
rust web(api)/job/rpc application

layout

.
├── config Configuration file reading and initialization
├── handlers Function handler for complex routing rules
│ └── mod.rs
├── main.rs 
├── middleware The main middleware is rpc/api middleware
│ └── mod.rs
├── routes Routing rule
│ └── mod.rs
└── services Business logic layer
└── mod.rs
Enter fullscreen mode Exit fullscreen mode

run

cargo run --bin rs-api
Enter fullscreen mode Exit fullscreen mode

Visit home page: http://localhost:1338/

Hello, World!
Enter fullscreen mode Exit fullscreen mode

api handler

POST http://localhost:1338/form

curl --location --request POST 'http://localhost:1338/form' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'name=daheige' \
--data-urlencode 'age=30'
Enter fullscreen mode Exit fullscreen mode

response

{
    "code": 0,
    "message": "ok",
    "data": {
        "name": "daheige",
        "email": "",
        "age": 30
    }
}
Enter fullscreen mode Exit fullscreen mode

POST http://localhost:1338/users

curl --location --request POST 'http://localhost:1338/users' \
--header 'Content-Type: application/json' \
--data-raw '{"username":"daheige"}'
Enter fullscreen mode Exit fullscreen mode

response

{
    "code": 0,
    "message": "success",
    "data": {
        "id": 1337,
        "username": "daheige"
    }
}
Enter fullscreen mode Exit fullscreen mode

GET http://localhost:1338/empty-array

curl --location --request GET 'localhost:1338/empty-array'
Enter fullscreen mode Exit fullscreen mode

response

{
    "code": 0,
    "message": "ok",
    "data": []
}
Enter fullscreen mode Exit fullscreen mode

GET http://localhost:1338/empty-object

curl --location --request GET 'localhost:1338/empty-object'
Enter fullscreen mode Exit fullscreen mode

response

{
    "code": 0,
    "message": "ok",
    "data": {}
}
Enter fullscreen mode Exit fullscreen mode

GET http://localhost:1338/html

curl --location --request GET 'localhost:1338/html'
Enter fullscreen mode Exit fullscreen mode

response

<h1>hello,rs-api</h1>
Enter fullscreen mode Exit fullscreen mode

get header

from axum::http::HeaderMap

let ua = headers
        .get(header::USER_AGENT)
        .and_then(|v| v.to_str().ok())
        .map(|v| v.to_string())
        .unwrap();
println!("user-agent:{}", ua);

// eg: this code
// Content-Type: application/x-www-form-urlencoded
// pub async fn accept_form(Form(input): Form<user::UserForm>) -> impl IntoResponse {
pub async fn accept_form(
    headers: HeaderMap,
    Form(input): Form<user::UserForm>,
) -> impl IntoResponse {
    println!("headers: {:?}", headers);
    let ua = headers
        .get(header::USER_AGENT)
        .and_then(|v| v.to_str().ok())
        .map(|v| v.to_string())
        .unwrap();
    println!("user-agent:{}", ua);

    println!("current input:{:?}", input);
    (
        StatusCode::OK,
        Json(super::Reply {
            code: 0,
            message: "ok".to_string(),
            data: Some(input),
        }),
    )
}
Enter fullscreen mode Exit fullscreen mode

handlers usage

please see handlers/index.rs

access_log middleware

exec begin method:GET uri:/empty-object?id=1 path:/empty-object request body:Body(Empty) query:Some("id=1") ua:PostmanRuntime/7.26.8 request_id:f1d720c8-2eab-408a-bd0a-41c924512d7f
exec end,request_id:f1d720c8-2eab-408a-bd0a-41c924512d7f,exec_time:0ms
Enter fullscreen mode Exit fullscreen mode

License

MIT

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay