https://github.com/rubyhcm/python_fastapi
https://github.com/rubyhcm/python_fastapi
fastapi python
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rubyhcm/python_fastapi
- Owner: rubyhcm
- Created: 2025-02-28T04:06:58.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-17T02:16:46.000Z (about 1 year ago)
- Last Synced: 2025-05-17T03:14:25.046Z (11 months ago)
- Topics: fastapi, python
- Language: Python
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#### Create envirement
`python3 -m venv `
ex: python3 -m venv venv
#### Run virtual envirement
`source venv/bin/activate`
#### Insrall fastapi
`pip install "fastapi[standard]"`
#### Run server
`fastapi dev app/main.py`
or
`uvicorn main:app`
`uvicorn main:app --reload`
#### API docs
http://127.0.0.1:8000/docs
http://127.0.0.1:8000/redoc
#### Install Postgres
`pip install psycopg2`
```python
# app/main.py
try:
conn = psycopg2.connect(
host="localhost",
database="fastapi",
user="postgres",
password="",
cursor_factory=RealDictCursor
)
cursor = conn.cursor()
print("Database connection was successful")
except Exception as error:
print("Connecting to database failed")
print("Error: ", error)
```
#### Steps
1. Create `app/main.py`
2. Create `__init__.py`
# SQL Alchemy
Using ORM
#### Install SQL Alchemy
`pip install sqlalchemy`
#### Steps
1. Create 'app/database.py'
2. Create 'app/models.py'
3. Create 'app/schemas.py'
#### Check packages
`pip freeze`
#### Validate email
`pip install email-validator`
#### Encrypt password
`pip install 'passlib[bcrypt]'`
`pip install --upgrade bcrypt`
`pip uninstall bcrypt`
`pip install bcrypt==4.0.1`
#### JWT
`pip install 'python-jose[cryptography]'`
#### ENV file
`pip install python-dotenv`
#### Using
`user.__dict__ == user.first`
#### Migration
`pip install alembic`
`alembic init alembic` to init
`alembic revision -m 'create notes table'` to create migration
`alembic upgrade head` or `alembic upgrade 3f2cb1cd21cd`to run migration
`alembic downgrade -1` or `alembic downgrade 3f2cb1cd21cd` to downgrade
`alembic current` to check status
#### Requirement
`pip freeze > requirements.txt`
`pip install -r requirements.txt` to install
#### Notes
http://127.0.0.1:8000/docs must be defined api /login with respone key "access_token"
#### Debugger
```python
import pdb
pdb.set_trace()
# using
vars(user)
dir(user)
```
Example in app/routers/auth.py
Or we can use Run & Debug configuration in .vscode/launch.json