/API

Primary LanguagePythonMIT LicenseMIT

Project logo

Tech With Tim - API

Status GitHub Issues GitHub Pull Requests Licence Discord Test and deploy

API for the Tech With Tim website using FastAPI.

📝 Table of Contents

🏁 Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See Running with Docker if you want to setup the API faster with Docker.

Discord application

Create a new Discord application here by clicking the New application button and name it whatever you want.

New application

Now that you have an application, go to the OAuth2 tab.

OAuth2 tab

And add http://127.0.0.1:5000/auth/discord/callback to the redirects.

Redirects

Prerequisites

Install Pipenv:

pip install pipenv

Install the required packages and the packages for development with Pipenv:

pipenv install --dev

Environment variables

Required

Start by writing this in a file named .env:

REDIS_URI=
SECRET_KEY=
POSTGRES_URI=
DISCORD_CLIENT_ID=
DISCORD_CLIENT_SECRET=

And fill in the variables with the values below:

  • REDIS_URI is the Redis server URI.
  • SECRET_KEY is the key used for JWT token encoding.
  • POSTGRES_URI is the PostgreSQL database URI.
  • DISCORD_CLIENT_ID is the Discord application ID. Copy it from your Discord application page (see below).
  • DISCORD_CLIENT_SECRET is the Discord application secret. Copy it from your Discord application page (see below).

Client ID and secret

Optional

For testing you need to add these environment variables:

  • TEST_REDIS_URI is the Connection URI for Redis testing server.
  • TEST_POSTGRES_URI is the PostgreSQL database URI for tests.

If you are self hosting the Piston API, you need to set the PISTON_URL environment variable.

Running

Run the API and initialise the database:

Make sure submodules are up to date.

If you have not initialized submodules use this command:
git submodule update --init

To update submodules:
git submodule foreach git pull

pipenv run python launch.py runserver --initdb

The API should run at http://127.0.0.1:5000. For more information about the CLI, check the docs here.

🐳 Running with Docker

Both the API and the frontend can be started using Docker. Using Docker is generally recommended (but not strictly required) because it abstracts away some additional set up work.

  • Setup the discord app like done here.

  • Make a file named .env like done here. You don't need the DB_URI environment variable though.

  • Then make sure you have docker and docker-compose installed, if not read this for docker and this for docker compose.

  • Deploy the API:

    docker-compose up --build api

✅ Linting

We use a pre-commit hook for linting the code before each commit. Set up the pre-commit hook:

pipenv run pre-commit install

If you want to run the pre-commit checks before trying to commit, you can do it with:

pipenv run lint

🚨 Tests

To test the API, we use the pytest framework to make sure that the code we write works.

Run the tests:

pipenv run test

When you contribute, you need to add tests on the features you add.

⛏️ Built Using

✍️ Authors

See also the list of contributors who participated in this project.