|
| 1 | +# FastAPI-SQLModel |
| 2 | + |
| 3 | +FastAPI-SQLModel is a Python API Application with FastAPI, JWT Authentication, |
| 4 | +Postgresql, SQLModel, Docker and Jenkins Pipeline |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +* Full **Docker** integration (Docker based). |
| 9 | +* **Production ready** Python web server using Uvicorn and Gunicorn. |
| 10 | +* Python <a href="https://github.com/tiangolo/fastapi" class="external-link" target="_blank">**FastAPI**</a> backend: |
| 11 | + * **Fast**: Very high performance, on par with **NodeJS** and **Go** (thanks to Starlette and Pydantic). |
| 12 | + * **Intuitive**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging. |
| 13 | + * **Easy**: Designed to be easy to use and learn. Less time reading docs. |
| 14 | + * **Short**: Minimize code duplication. Multiple features from each parameter declaration. |
| 15 | + * **Robust**: Get production-ready code. With automatic interactive documentation. |
| 16 | + * **Standards-based**: Based on (and fully compatible with) the open standards for APIs: <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank">OpenAPI</a> and <a href="http://json-schema.org/" class="external-link" target="_blank">JSON Schema</a>. |
| 17 | + * <a href="https://fastapi.tiangolo.com/features/" class="external-link" target="_blank">**Many other features**</a> including automatic validation, serialization, interactive documentation, authentication with OAuth2 JWT tokens, etc. |
| 18 | +* **Secure password** hashing by default. |
| 19 | +* **JWT token** authentication. |
| 20 | +* **SQLModel** new SQL ORM. |
| 21 | +* Basic starting models for users (modify and remove as you need). |
| 22 | +* **CORS** (Cross Origin Resource Sharing). |
| 23 | +* Load balancing between frontend and backend with **Nginx**, so you can have both under the same domain, separated by path, but served by different containers. |
| 24 | +* Let's Encrypt **HTTPS** certificates automatic generation. |
| 25 | + |
| 26 | + |
| 27 | +## How to use it |
| 28 | + |
| 29 | +**psycopg2** |
| 30 | + |
| 31 | +psycopg2 is postgresql adapter for sqlalchamy this can't be installed directly |
| 32 | +in linux using pip, use below command to install |
| 33 | + |
| 34 | +```bash |
| 35 | +sudo apt-get install python3-psycopg2 |
| 36 | +sudo apt-get install libpq-dev python3-dev |
| 37 | +``` |
| 38 | + |
| 39 | +## JWT token authentication |
| 40 | + |
| 41 | +JWT-Signature using RSA256 algorithm. |
| 42 | + |
| 43 | +You can generate a 2048-bit RSA key pair with the following commands: |
| 44 | + |
| 45 | +```bash |
| 46 | +openssl genpkey -algorithm RSA -out rsa_private.pem -pkeyopt rsa_keygen_bits:2048 |
| 47 | +openssl rsa -in rsa_private.pem -pubout -out rsa_public.pem |
| 48 | +``` |
| 49 | + |
| 50 | +## Create SSL Certificates using Certbot |
| 51 | + |
| 52 | +Generate SSL cerificates from trusted thirdparty or openssl |
| 53 | + |
| 54 | + For Open SSL use Certbot (Let'sencrypt) |
| 55 | +```bash |
| 56 | +sudo apt-get update |
| 57 | +sudo apt-get install software-properties-common |
| 58 | +sudo add-apt-repository universe |
| 59 | +sudo add-apt-repository ppa:certbot/certbot |
| 60 | +sudo apt-get install certbot python3-certbot-nginx |
| 61 | + |
| 62 | +sudo certbot -d example.com certonly |
| 63 | +``` |
| 64 | +certificates will be found in "/etc/letsencrypt/live/example.com/" |
| 65 | + |
| 66 | +Generate a set of 4096-bit diffie-hellman parameters to improve security for some types of ciphers. |
| 67 | +```bash |
| 68 | +sudo mkdir -p /etc/nginx/ssl |
| 69 | +sudo openssl dhparam -out /etc/nginx/ssl/dhp-4096.pem 4096 |
| 70 | +``` |
| 71 | + |
| 72 | +## Deployment |
| 73 | + |
| 74 | +FastAPI Backend can be deployed using docker. Use below docker image for deployment. |
| 75 | + |
| 76 | +tiangolo/uvicorn-gunicorn-fastapi-docker - [Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.6 and above with performance auto-tuning. Optionally with Alpine Linux.](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker) |
| 77 | + |
| 78 | + |
| 79 | +#### References |
| 80 | + |
| 81 | +The fundamental repositories: |
| 82 | +- FastAPI - [FastAPI framework, high performance, easy to learn, fast to code, ready for production](https://fastapi.tiangolo.com/) |
| 83 | +- full-stack-fastapi-postgresql - [Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Docker, automatic HTTPS and more.](https://github.com/tiangolo/full-stack-fastapi-postgresql) |
| 84 | +- blog-posts - [Build a web API from scratch with FastAPI - the workshop](https://github.com/tiangolo/blog-posts/tree/master/pyconby-web-api-from-scratch-with-fastapi) |
| 85 | +- uvicorn - [The lightning-fast ASGI server.](https://www.uvicorn.org/deployment/) |
| 86 | +- tiangolo/uvicorn-gunicorn-fastapi-docker - [Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.6 and above with performance auto-tuning. Optionally with Alpine Linux.](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker) |
| 87 | +- sqlmodel - [SQLModel is a library for interacting with SQL databases from Python code, with Python objects. It is designed to be intuitive, easy to use, highly compatible, and robust.](https://sqlmodel.tiangolo.com/) |
| 88 | +- |
0 commit comments