Skip to content

Commit

Permalink
Readme examples
Browse files Browse the repository at this point in the history
And higher jwt expiration
  • Loading branch information
janos-r committed Apr 9, 2024
1 parent 6be5c3a commit 4e95a98
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
105 changes: 104 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Run without any prior setup, DB is in memory:
cargo run
```

To use routes other than `/hello`, login with:
To use routes other than `/hello`, login with (5min expiration):

```json
// POST on localhost:8080/api/login
Expand Down Expand Up @@ -46,3 +46,106 @@ To use routes other than `/hello`, login with:
without a working SurrealDb instance

Detailed description linked in article above.

TODO: Future Axum 0.8 should implement async traits. So after it comes out, I
will try to fix the Context extractor.

### Examples

GQL create:

```graphql
# post on `localhost:8080/`
mutation {
tickets {
createTicket(ctInput: { title: "First Ticket" }) {
id
title
creator
}
}
}
```

```json
{
"data": {
"tickets": {
"createTicket": {
"id": "6ki4tip4sx33gz622dc0",
"title": "First Ticket",
"creator": "joe@example.com"
}
}
}
}
```

GQL client error:

```graphql
mutation {
tickets {
deleteTicket(id: "12345"){
id
title
creator
}
}
}
```

Displays only the user-facing error text with the request ID to lookup in logs

```json
{
"data": null,
"errors": [
{
"message": "No result for id 12345",
"locations": [
{
"line": 3,
"column": 5
}
],
"path": [
"tickets",
"deleteTicket"
],
"extensions": {
"req_id": "bc857894-412c-4fc7-8040-ffdc1c03aaec"
}
}
]
}
```

Logs have the real error cause

```bash
->> LOGGER - mw_req_logger:
{"req_method":"POST","req_path":"/","user":"joe@example.com",
"error":"SurrealDbNoResult { source: \"internal\", id: \"12345\" }",
"timestamp":"1712652314952","req_id":"bc857894-412c-4fc7-8040-ffdc1c03aaec"}
```

Same as with REST

`delete on http://localhost:8080/api/tickets/999`

```json
{
"error": {
"error": "No result for id 999",
"req_id": "82afa2ab-01ff-4c93-b004-98f86f68e9d2"
}
}
```

```bash
->> LOGGER - mw_req_logger:
{"req_method":"DELETE","req_path":"/api/tickets/999","user":"joe@example.com",
"error":"SurrealDbNoResult { source: \"internal\", id: \"999\" }",
"timestamp":"1712654372370","req_id":"82afa2ab-01ff-4c93-b004-98f86f68e9d2"}
```
4 changes: 2 additions & 2 deletions src/web/routes_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async fn api_login(
};

// NOTE: set to a reasonable number after testing
// NOTE when testing: the default validation.leeway is 2min
let exp = Utc::now() + Duration::minutes(2);
// NOTE when testing: the default validation.leeway is 5min
let exp = Utc::now() + Duration::minutes(5);
let claims = Claims {
exp: exp.timestamp() as usize,
auth: mock_user.email,
Expand Down

0 comments on commit 4e95a98

Please sign in to comment.