-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rohul Amin
committed
Jun 20, 2023
1 parent
af17f2d
commit a6ca394
Showing
113 changed files
with
2,956 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docker-compose.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
# y | ||
|
||
## Features | ||
|
||
- **FastAPI** with Python 3.8 | ||
- **React 16** with Typescript, Redux, and react-router | ||
- Postgres | ||
- SqlAlchemy with Alembic for migrations | ||
- Pytest for backend tests | ||
- Jest for frontend tests | ||
- Perttier/Eslint (with Airbnb style guide) | ||
- Docker compose for easier development | ||
- Nginx as a reverse proxy to allow backend and frontend on the same port | ||
|
||
## Development | ||
|
||
The only dependencies for this project should be docker and docker-compose. | ||
|
||
### Quick Start | ||
|
||
Starting the project with hot-reloading enabled | ||
(the first time it will take a while): | ||
|
||
```bash | ||
docker-compose up -d | ||
``` | ||
|
||
To run the alembic migrations (for the users table): | ||
|
||
```bash | ||
docker-compose run --rm backend alembic upgrade head | ||
``` | ||
|
||
And navigate to http://localhost:8000 | ||
|
||
_Note: If you see an Nginx error at first with a `502: Bad Gateway` page, you may have to wait for webpack to build the development server (the nginx container builds much more quickly)._ | ||
|
||
Auto-generated docs will be at | ||
http://localhost:8000/api/docs | ||
|
||
### Rebuilding containers: | ||
|
||
``` | ||
docker-compose build | ||
``` | ||
|
||
### Restarting containers: | ||
|
||
``` | ||
docker-compose restart | ||
``` | ||
|
||
### Bringing containers down: | ||
|
||
``` | ||
docker-compose down | ||
``` | ||
|
||
### Frontend Development | ||
|
||
Alternatively to running inside docker, it can sometimes be easier | ||
to use npm directly for quicker reloading. To run using npm: | ||
|
||
``` | ||
cd frontend | ||
npm install | ||
npm start | ||
``` | ||
|
||
This should redirect you to http://localhost:3000 | ||
|
||
### Frontend Tests | ||
|
||
``` | ||
cd frontend | ||
npm install | ||
npm test | ||
``` | ||
|
||
## Migrations | ||
|
||
Migrations are run using alembic. To run all migrations: | ||
|
||
``` | ||
docker-compose run --rm backend alembic upgrade head | ||
``` | ||
|
||
To create a new migration: | ||
|
||
``` | ||
alembic revision -m "create users table" | ||
``` | ||
|
||
And fill in `upgrade` and `downgrade` methods. For more information see | ||
[Alembic's official documentation](https://alembic.sqlalchemy.org/en/latest/tutorial.html#create-a-migration-script). | ||
|
||
## Testing | ||
|
||
There is a helper script for both frontend and backend tests: | ||
|
||
``` | ||
./scripts/test.sh | ||
``` | ||
|
||
### Backend Tests | ||
|
||
``` | ||
docker-compose run backend pytest | ||
``` | ||
|
||
any arguments to pytest can also be passed after this command | ||
|
||
### Frontend Tests | ||
|
||
``` | ||
docker-compose run frontend test | ||
``` | ||
|
||
This is the same as running npm test from within the frontend directory | ||
|
||
## Logging | ||
|
||
``` | ||
docker-compose logs | ||
``` | ||
|
||
Or for a specific service: | ||
|
||
``` | ||
docker-compose logs -f name_of_service # frontend|backend|db | ||
``` | ||
|
||
## Project Layout | ||
|
||
``` | ||
backend | ||
└── app | ||
├── alembic | ||
│ └── versions # where migrations are located | ||
├── api | ||
│ └── api_v1 | ||
│ └── endpoints | ||
├── core # config | ||
├── db # db models | ||
├── tests # pytest | ||
└── main.py # entrypoint to backend | ||
frontend | ||
└── public | ||
└── src | ||
├── components | ||
│ └── Home.tsx | ||
├── config | ||
│ └── index.tsx # constants | ||
├── __tests__ | ||
│ └── test_home.tsx | ||
├── index.tsx # entrypoint | ||
└── App.tsx # handles routing | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
FROM python:3.8 | ||
|
||
RUN mkdir /app | ||
WORKDIR /app | ||
|
||
RUN apt update && \ | ||
apt install -y postgresql-client | ||
|
||
COPY requirements.txt ./ | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
COPY . . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# A generic, single database configuration. | ||
|
||
[alembic] | ||
# path to migration scripts | ||
script_location = app/alembic | ||
|
||
# template used to generate migration files | ||
# file_template = %%(rev)s_%%(slug)s | ||
|
||
# timezone to use when rendering the date | ||
# within the migration file as well as the filename. | ||
# string value is passed to dateutil.tz.gettz() | ||
# leave blank for localtime | ||
timezone = America/Los_Angeles | ||
|
||
# max length of characters to apply to the | ||
# "slug" field | ||
# truncate_slug_length = 40 | ||
|
||
# set to 'true' to run the environment during | ||
# the 'revision' command, regardless of autogenerate | ||
# revision_environment = false | ||
|
||
# set to 'true' to allow .pyc and .pyo files without | ||
# a source .py file to be detected as revisions in the | ||
# versions/ directory | ||
# sourceless = false | ||
|
||
# version location specification; this defaults | ||
# to alembic/versions. When using multiple version | ||
# directories, initial revisions must be specified with --version-path | ||
# version_locations = %(here)s/bar %(here)s/bat alembic/versions | ||
|
||
# the output encoding used when revision files | ||
# are written from script.py.mako | ||
# output_encoding = utf-8 | ||
|
||
[post_write_hooks] | ||
# post_write_hooks defines scripts or Python functions that are run | ||
# on newly generated revision scripts. See the documentation for further | ||
# detail and examples | ||
|
||
# format using "black" - use the console_scripts runner, against the "black" entrypoint | ||
# hooks=black | ||
# black.type=console_scripts | ||
# black.entrypoint=black | ||
# black.options=-l 79 | ||
|
||
# Logging configuration | ||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
Empty file.
Binary file added
BIN
+111 Bytes
fastapi-react-project/backend/app/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# A generic, single database configuration. | ||
|
||
[alembic] | ||
# path to migration scripts | ||
script_location = alembic | ||
|
||
# template used to generate migration files | ||
# file_template = %%(rev)s_%%(slug)s | ||
|
||
# timezone to use when rendering the date | ||
# within the migration file as well as the filename. | ||
# string value is passed to dateutil.tz.gettz() | ||
# leave blank for localtime | ||
# timezone = | ||
|
||
# max length of characters to apply to the | ||
# "slug" field | ||
# truncate_slug_length = 40 | ||
|
||
# set to 'true' to run the environment during | ||
# the 'revision' command, regardless of autogenerate | ||
# revision_environment = false | ||
|
||
# set to 'true' to allow .pyc and .pyo files without | ||
# a source .py file to be detected as revisions in the | ||
# versions/ directory | ||
# sourceless = false | ||
|
||
# version location specification; this defaults | ||
# to alembic/versions. When using multiple version | ||
# directories, initial revisions must be specified with --version-path | ||
# version_locations = %(here)s/bar %(here)s/bat alembic/versions | ||
|
||
# the output encoding used when revision files | ||
# are written from script.py.mako | ||
# output_encoding = utf-8 | ||
|
||
sqlalchemy.url = driver://user:pass@localhost/dbname | ||
|
||
|
||
[post_write_hooks] | ||
# post_write_hooks defines scripts or Python functions that are run | ||
# on newly generated revision scripts. See the documentation for further | ||
# detail and examples | ||
|
||
# format using "black" - use the console_scripts runner, against the "black" entrypoint | ||
# hooks=black | ||
# black.type=console_scripts | ||
# black.entrypoint=black | ||
# black.options=-l 79 | ||
|
||
# Logging configuration | ||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Generic single-database configuration. |
Empty file.
Binary file added
BIN
+1.76 KB
fastapi-react-project/backend/app/alembic/__pycache__/env.cpython-38.pyc
Binary file not shown.
Oops, something went wrong.
a6ca394
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このプル リクエストでは、FastAPI-React アプリケーションに「Todo 管理」機能を追加しました。ユーザーは Todo を作成、読み取り、更新、削除できます。
エンドポイントの追加: fastapi-react-project/backend/app/api/api_v1/routers/todos.pyに新たにノート管理用のエンドポイントを追加しました。これらのエンドポイントは、ノートの作成、取得、更新、削除を行うためのものです。
CRUD操作の更新: fastapi-react-project/backend/app/db/crud.pyにてノート用のCRUD操作を更新し、新たに追加したエンドポイントに合わせました。
データベースモデルの更新: fastapi-react-project/backend/app/db/models.pyにてデータベーススキーマを拡張し、'notes'モデルを追加しました。このモデルはノート関連のデータを保存します。
スキーマの定義: fastapi-react-project/backend/app/db/schemas.pyにて、ノートの表現に用いる新しいPydanticスキーマを定義しました。
メインアプリの更新: fastapi-react-project/backend/app/main.pyにて新たに作成したノート用のルーターをインポート・含め、エンドポイントを利用可能にしました。
React.jsを使用してtodoを確認、追加、更新、削除するための画面をいくつか追加しました。
1.Todo画面に行く
2.Todoリストを見る
3.Todoを作成する
4.Todoを編集する
5.Todoを削除する