Code Monkey home page Code Monkey logo

wbl-backend's Introduction

FastAPI

Using FastAPI to perform the backend operations and establishing the connection between the UI and Database

Acknowledgements

Prerequisites

  • Python 3.7+
  • MySQL database

Installation

VS code (1.89.1)
MySQL Workbench
Postman

Create a virtual environment and activate it:

python -m venv YOUR_FOLDER_NAME

for mac/linux platforms:

source YOUR_FOLDER_NAME/bin/activate

for Windows platforms

cd YOUR_FOLDER_NAME\Scripts
activate

Clone the repository

git clone https://github.com/WhiteboxHub/wbl-backend.git 
cd wbl-backend

Install the dependencies in terminal:

pip install fastapi
pip install uvicorn
pip install uvicorn[standard]
pip install mysql-connector-python
pip install python-jose
pip install passlib 

Set up the MySQL database:

  • Install MySQL and login credentials

Environment Variables

To run this project, you will need to add the following environment variables to your .env file

DB_HOST=00.000.00.00

DB_USER=UserName

DB_PASSWORD=Password

DB_NAME=DbName

DB_PORT=Port

SECRET_KEY=SecretKey

API Reference

Get all items

  GET /api/items
Parameter Type Description
api_key string Required. Your API key

Get item

  GET /api/items/${id}
Parameter Type Description
id string Required. Id of item to fetch

add(num1, num2)

Takes two numbers and returns the sum.

๐Ÿ”— Links

https://github.com/WhiteboxHub/wbl-backend

Features

  • Light/dark mode toggle
  • Live previews
  • Fullscreen mode
  • Cross platform

License

MIT

Running the Application

  1. Start the FastAPI application:

    bash uvicorn main:app --reload

  2. Access the API documentation at http://127.0.0.1:8000/docs.

User Registration

  • Endpoint: POST /users/

  • Request Body:

    json { "username": "yourusername", "email": "[email protected]", "password": "yourpassword" }

  • Response:

    json { "id": 1, "username": "yourusername", "email": "[email protected]" }

Token Generation

  • Endpoint: POST /token

  • Request Body: (as x-www-form-urlencoded)

    text username=yourusername&password=yourpassword

  • Response:

    json { "access_token": "your_access_token", "token_type": "bearer" }

Protected End point

  • Endpoint: GET /recordings

  • Headers:

    text Authorization: Bearer your_access_token

  • Response: (Example)

    json [ { "id": 1, "name": "Recording 1", "url": "http://example.com/recording1" } ]

Using Postman

Step 1: Acquire the Token

  1. Create a new request in Postman.
  2. Set the request type to POST.
  3. Enter the URL: http://127.0.0.1:8000/token.
  4. Navigate to the Body tab and select x-www-form-urlencoded.
  5. Add the following key-value pairs:
    • username: yourusername
    • password: yourpassword
  6. Send the request.
  7. Copy the access_token from the response.

Step 2: Use the Token to Access Protected Endpoints

  1. Create a new request in Postman.
  2. Set the request type to GET.
  3. Enter the URL: http://127.0.0.1:8000/recordings.
  4. Navigate to the Authorization tab.
  5. Select Bearer Token from the Type dropdown.
  6. Paste the access_token into the Token field.
  7. Send the request.
  8. Inspect the response to ensure you can access the protected resource.

Troubleshooting

  • Ensure that the SECRET_KEY in auth.py is kept secure and is not shared publicly.
  • Verify that your MySQL database is running and accessible with the provided credentials.
  • Check the FastAPI logs for any errors or warnings during requests.
  • Make sure the token has not expired and is correctly formatted in the Authorization header.

wbl-backend's People

Contributors

whiteboxhub avatar hkinnovapath 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.