Giter VIP home page Giter VIP logo

actix-web-rest-api-with-jwt's Introduction

Actix-web REST API with JWT

A simple CRUD backend app using Actix-web, Diesel and JWT

Require

Or using Docker

How to run

Manual

  • Rename secret.key.sample to secret.key or create your own key by running head -c16 /dev/urandom > secret.key in command line (Linux/UNIX only) and copy to /src folder
  • Create a database in postgres cli or pgAdmin tool
  • Rename .env.sample to .env and update the database connection string in DATABASE_URL key.
  • Build with release profile: cargo build --release
  • Run release binary in command line/terminal.
    • Windows: target/release/actix-web-rest-api-with-jwt.exe
    • Linux/UNIX: target/release/actix-web-rest-api-with-jwt
  • Enjoy! ๐Ÿ˜„

Docker

  • Enter into project directory and run docker-compose up
  • Enjoy! ๐Ÿ˜„

APIs

Address: localhost:8000

GET /api/ping: Ping

  • Response:
    • 200 OK
    pong!
    

POST /api/auth/signup: Signup

  • Request body:
{
   "username": string,
   "email": string,
   "password": string       // a raw password
}
  • Response
    • 200 OK
    {
       "message": "signup successfully",
       "data": ""
    }
    
    • 400 Bad Request
    {
       "message": "User '{username}' is already registered",
       "data": ""
    }
    

POST /api/auth/login: Login

  • Request body:
{
   "username_or_email": string,
   "password": string       // a raw password
}
  • Response
    • 200 OK
    {
       "message": "login successfully",
       "data": {
         "token": string      // bearer token
       }
    }
    
    • 400 Bad Request
    {
       "message": "wrong username or password, please try again",
       "data": ""
    }
    

GET /api/address-book: Get all people information

  • Header:
    • Authorization: bearer <token>
  • Response
    • 200 OK
    {
       "message": "ok",
       "data": [
          {
            "id": int32,
            "name": string,
            "gender": boolean,      // true for male, false for female
            "age": int32,
            "address": string,
            "phone": string,
            "email": string
          }
       ]
    }
    

GET /api/address-book/{id}: Get person information by id

  • Param path:
    • id: int32
  • Header:
    • Authorization: bearer <token>
  • Response
    • 200 OK
    {
       "message": "ok",
       "data": {
         "id": int32,
         "name": string,
         "gender": boolean,      // true for male, false for female
         "age": int32,
         "address": string,
         "phone": string,
         "email": string
       }
    }
    
    • 404 Not Found
    {
       "message": "person with id {id} not found",
       "data": ""
    }
    

GET /api/address-book/{query}: Search for person information by keyword

  • Param path:
    • query: string
  • Header:
    • Authorization: bearer <token>
  • Response
    • 200 OK
    {
       "message": "ok",
       "data": [
         {
           "id": int32,
           "name": string,
           "gender": boolean,      // true for male, false for female
           "age": int32,
           "address": string,
           "phone": string,
           "email": string
         }
       ]
    }
    

POST /api/address-book: Add person information

  • Header:
    • Authorization: bearer <token>
  • Request body:
    {
      "name": string,
      "gender": boolean,      // true for male, false for female
      "age": int32,
      "address": string,
      "phone": string,
      "email": string
    }
    
  • Response
    • 201 Created
    {
      "message": "ok",
      "data": ""
    }
    
    • 500 Internal Server Error
    {
      "message": "can not insert data",
      "data": ""
    }
    

PUT /api/address-book/{id}: Update person information by id

  • Param path:
    • id: int32
  • Header:
    • Authorization: bearer <token>
  • Request body:
{
  "name": string,
  "gender": boolean,      // true for male, false for female
  "age": int32,
  "address": string,
  "phone": string,
  "email": string
}
  • Response
    • 200 OK
    {
      "message": "ok",
      "data": ""
    }
    
    • 500 Internal Server Error
    {
      "message": "can not update data",
      "data": ""
    }
    

DELETE /api/address-book/{id}: Delete person information by id

  • Param path:
    • id: int32
  • Header:
    • Authorization: bearer <token>
  • Response
    • 200 OK
    {
      "message": "ok",
      "data": ""
    }
    
    • 500 Internal Server Error
    {
      "message": "can not delete data",
      "data": ""
    }
    

Errors:

  • Invalid or missing token
    • Status code: 401 Unauthorized
    • Response:
    {
      "message": "invalid token, please login again",
      "data": ""
    }
    

actix-web-rest-api-with-jwt's People

Contributors

sakadream 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.