Skip to main content

Router

How works the routing system in Graphul?​

Graphul for its router system uses the trait Method that implements a function for every HTTP Method.

We can access these methods using an instance of Graphul or an instance of a Group.

Besides every handler have access to the Context.

List of HTTP Methods implemented​

As we said, Graphul tries to give an implementation of every HTTP Method, here is the list:

  • get
  • post
  • put
  • patch
  • delete
  • options
  • trace
  • head

All of these are available. If you try to access these methods directly from a Graphul instance or a Group instance maybe you don't see the autocompletion or maybe if you try to run it you could get an error like this:

Output trying to run itOutput trying to run it

This error as said in the image is because you need to import the trait Methods. So you can add this line to your file and try again:

use graphul::http::Methods;

Example​

use graphul::{http::Methods, Graphul};

#[tokio::main]
async fn main() {
let mut app = Graphul::new();
app.get("/", || async { "hello world!" });
app.get("/hello", your_handler);
app.post("/hello", your_handler);
app.put("/hello", your_handler);
app.patch("/hello", your_handler);
app.delete("/hello", your_handler);
app.run("127.0.0.1:8000").await;
}

async fn your_handler() {
// ... your code
}

Getting Path Params​

The path params are accessible with a definition of the path, for example:

  app.get("/:name", |name: Path<String>| async move {
format!("Hi {}!", name.0)
});
info

Graphul maintains the extractors of Axum

We declare the parameter using the : to define a parameter and then you can access the parameter of type Context to get the query parameters inside of the handler.

Getting Query Params​

The Query Params are too similar to the Path Params. In this case, we just call the function query to access the Query Params.

  app.get("/", |name: Query<String>| async move {
format!("Hi {}!", name.0)
});
info

Graphul maintains the extractors of Axum