⚡️ Quickstart
use graphul::{Graphul, http::Methods};
#[tokio::main]
async fn main() {
let mut app = Graphul::new();
app.get("/", || async {
"Hello, World 👋!"
});
app.run("127.0.0.1:8000").await;
}
JSON
use graphul::{Json, Graphul, http::Methods};
use serde_json::json;
#[tokio::main]
async fn main() {
let mut app = Graphul::new();
app.get("/", || async {
Json(json!({
"name": "full_name",
"age": 98,
"phones": [
format!("+44 {}", 8)
]
}))
});
app.run("127.0.0.1:8000").await;
}
Resource
use graphul::{Json, Graphul, http::{StatusCode, resource::Resource, response::Response}, Request, IntoResponse};
use async_trait::async_trait;
use serde_json::json;
struct Article;
#[async_trait]
impl Resource for Article {
async fn get(_req: Request) -> Response {
let posts = json!({
"posts": ["Article 1", "Article 2", "Article ..."]
});
(StatusCode::OK, Json(posts)).into_response()
}
async fn post(_req: Request) -> Response {
(StatusCode::CREATED, "post handler").into_response()
}
}
#[tokio::main]
async fn main() {
let mut app = Graphul::new();
app.resource("/article", Article);
app.run("127.0.0.1:8000").await;
}
Groups
use graphul::{
Json,
extract::Path,
Graphul,
http::{ Methods, StatusCode }
};
use serde_json::json;
async fn index() -> &'static str {
"index handler"
}
async fn name(Path(name): Path<String>) -> impl IntoResponse {
let user = json!({
"response": format!("my name is {}", name)
});
(StatusCode::CREATED, Json(user))
}
#[tokio::main]
async fn main() {
let mut app = Graphul::new();
let mut api = app.group("api");
let mut user = api.group("user");
user.resource("/", Article)
user.get("/:name", name);
let mut post = api.group("post");
post.get("/", index);
post.get("/all", || async move {
Json(json!({"message": "hello world!"}))
});
app.run("127.0.0.1:8000").await;
}