Fastapi Realworld Example App

Backend logic implementation for https://github.com/gothinkster/realworld with awesome FastAPI
Alternatives To Fastapi Realworld Example App
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Awesome Cheatsheets32,908
5 days ago85mitJavaScript
👩‍💻👨‍💻 Awesome cheatsheets for popular programming languages, frameworks and development tools. They include everything you should know in one single file.
Awesome Compose22,256
3 days ago7April 23, 202199cc0-1.0HTML
Awesome Docker Compose samples
Immudb8,1291518 hours ago130August 25, 2022125apache-2.0Go
immudb - immutable database based on zero trust, SQL and Key-Value, tamperproof, data change history
Coolify7,297
16 hours ago230apache-2.0Svelte
An open-source & self-hostable Heroku / Netlify alternative.
Docker4,596
14 hours ago395agpl-3.0Shell
⛴ Docker image of Nextcloud
Pgloader4,413
14 days ago1February 27, 2018288otherCommon Lisp
Migrate to PostgreSQL in a single command!
Dockertest3,33423272320 hours ago87May 25, 202219apache-2.0Go
Write better integration tests! Dockertest helps you boot up ephermal docker images for your Go tests with minimal work.
Dbmate3,19917 days ago27March 25, 202212mitGo
:rocket: A lightweight, framework-agnostic database migration tool.
Manticoresearch3,041
18 hours ago223gpl-2.0C++
Easy to use open source fast database for search | Good alternative to Elasticsearch now | Drop-in replacement for E in the ELK soon
Phpredisadmin2,968
125 days ago25October 14, 20209PHP
Simple web interface to manage Redis databases.
Alternatives To Fastapi Realworld Example App
Select To Compare


Alternative Project Comparisons
Readme
./.github/assets/logo.png


NOTE: This repository is not actively maintained because this example is quite complete and does its primary goal - passing Conduit testsuite.

More modern and relevant examples can be found in other repositories with fastapi tag on GitHub.

Quickstart

First, run PostgreSQL, set environment variables and create database. For example using docker:

export POSTGRES_DB=rwdb POSTGRES_PORT=5432 POSTGRES_USER=postgres POSTGRES_PASSWORD=postgres
docker run --name pgdb --rm -e POSTGRES_USER="$POSTGRES_USER" -e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" -e POSTGRES_DB="$POSTGRES_DB" postgres
export POSTGRES_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pgdb)
createdb --host=$POSTGRES_HOST --port=$POSTGRES_PORT --username=$POSTGRES_USER $POSTGRES_DB

Then run the following commands to bootstrap your environment with poetry:

git clone https://github.com/nsidnev/fastapi-realworld-example-app
cd fastapi-realworld-example-app
poetry install
poetry shell

Then create .env file (or rename and modify .env.example) in project root and set environment variables for application:

touch .env
echo APP_ENV=dev >> .env
echo DATABASE_URL=postgresql://$POSTGRES_USER:[email protected]$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB >> .env
echo SECRET_KEY=$(openssl rand -hex 32) >> .env

To run the web application in debug use:

alembic upgrade head
uvicorn app.main:app --reload

If you run into the following error in your docker container:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Ensure the DATABASE_URL variable is set correctly in the .env file. It is most likely caused by POSTGRES_HOST not pointing to its localhost.

DATABASE_URL=postgresql://postgres:[email protected]:5432/rwdb

Run tests

Tests for this project are defined in the tests/ folder.

Set up environment variable DATABASE_URL or set up database_url in app/core/settings/test.py

This project uses pytest to define tests because it allows you to use the assert keyword with good formatting for failed assertations.

To run all the tests of a project, simply run the pytest command:

$ pytest
================================================= test session starts ==================================================
platform linux -- Python 3.8.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /home/some-user/user-projects/fastapi-realworld-example-app, inifile: setup.cfg, testpaths: tests
plugins: env-0.6.2, cov-2.9.0, asyncio-0.12.0
collected 90 items

tests/test_api/test_errors/test_422_error.py .                                                                   [  1%]
tests/test_api/test_errors/test_error.py .                                                                       [  2%]
tests/test_api/test_routes/test_articles.py .................................                                    [ 38%]
tests/test_api/test_routes/test_authentication.py ..                                                             [ 41%]
tests/test_api/test_routes/test_comments.py ....                                                                 [ 45%]
tests/test_api/test_routes/test_login.py ...                                                                     [ 48%]
tests/test_api/test_routes/test_profiles.py ............                                                         [ 62%]
tests/test_api/test_routes/test_registration.py ...                                                              [ 65%]
tests/test_api/test_routes/test_tags.py ..                                                                       [ 67%]
tests/test_api/test_routes/test_users.py ....................                                                    [ 90%]
tests/test_db/test_queries/test_tables.py ...                                                                    [ 93%]
tests/test_schemas/test_rw_model.py .                                                                            [ 94%]
tests/test_services/test_jwt.py .....                                                                            [100%]

============================================ 90 passed in 70.50s (0:01:10) =============================================
$

If you want to run a specific test, you can do this with this pytest feature:

$ pytest tests/test_api/test_routes/test_users.py::test_user_can_not_take_already_used_credentials

Deployment with Docker

You must have docker and docker-compose tools installed to work with material in this section. First, create .env file like in Quickstart section or modify .env.example. POSTGRES_HOST must be specified as db or modified in docker-compose.yml also. Then just run:

docker-compose up -d db
docker-compose up -d app

Application will be available on localhost in your browser.

Web routes

All routes are available on /docs or /redoc paths with Swagger or ReDoc.

Project structure

Files related to application are in the app or tests directories. Application parts are:

app
 api              - web related stuff.
  dependencies - dependencies for routes definition.
  errors       - definition of error handlers.
  routes       - web routes.
 core             - application configuration, startup events, logging.
 db               - db related stuff.
  migrations   - manually written alembic migrations.
  repositories - all crud stuff.
 models           - pydantic models for this application.
  domain       - main models that are used almost everywhere.
  schemas      - schemas for using in web routes.
 resources        - strings that are used in web responses.
 services         - logic that is not just crud related.
 main.py          - FastAPI application creation and configuration.
Popular Docker Projects
Popular Database Projects
Popular Virtualization Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Python
Python3
Docker
Database
Postgresql
Tutorial
Docker Compose
Routes
Pytest