Giter Club home page Giter Club logo

axum-template's Introduction


Axum server, Async-GraphQl, SurrealDB template

Run without any prior setup, DB is in memory:

cargo run

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

// POST on localhost:8080/api/login
{ "email": "[email protected]", "password": "123" }

Features implemented

  • Axum
    • Query and Path get examples
    • REST CRUD with in-memory mock model
  • JWT
    • login with expiration
    • saved in cookies HttpOnly
  • Manual Error handling without 3rd party crates
    • Errors respond with request IDs
    • Debug and Display variants for server and client
  • Spellcheck with cspell
  • GraphQl with async-graphql
  • Request logs
    • For every request one log
    • Include req_id, error, logged in user
    • For both REST and GraphQL
  • CI with github actions
  • SurrealDb
    • in memory, no setup
    • service structure callable from both rest and GraphQl
    • no-db workaround in both rest routes and GraphQl for testing and debugging 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:

# post on `localhost:8080/`
mutation {
  tickets {
    createTicket(ctInput: { title: "First Ticket" }) {
      id
      title
      creator
    }
  }
}
{
  "data": {
    "tickets": {
      "createTicket": {
        "id": "6ki4tip4sx33gz622dc0",
        "title": "First Ticket",
        "creator": "[email protected]"
      }
    }
  }
}

GQL client error:

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

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

{
  "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

->> LOGGER       - mw_req_logger:
    {"req_method":"POST","req_path":"/","user":"[email protected]",
    "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

{
  "error": {
    "error": "No result for id 999",
    "req_id": "82afa2ab-01ff-4c93-b004-98f86f68e9d2"
  }
}
->> LOGGER       - mw_req_logger:
    {"req_method":"DELETE","req_path":"/api/tickets/999","user":"[email protected]",
    "error":"SurrealDbNoResult { source: \"internal\", id: \"999\" }",
    "timestamp":"1712654372370","req_id":"82afa2ab-01ff-4c93-b004-98f86f68e9d2"}

axum-template's People

Contributors

janos-r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.