Coder Social home page Coder Social logo

clean-architecture-fastapi's Introduction

概要

クリーンアーキテクチャ(Clean Architecture)で実装した FastAPI ベースのシンプルな ユーザ操作(CRUD)API。

ローカル開発環境構築

前提

  • Python 3.11
  • sqlite3

アプリケーション環境構築

Python ライブラリのインストール。

$ pip install poetry

データベース初期化

DB を初期化

$ export PYTHONPATH="$(pwd):$PYTHONPATH"
$ python app/init_db.py

作成された DB の確認

$ sqlite3 sample_db.sqlite3

テーブルの確認。

> .tables
users

レコードの確認。

> select * from users;
1|squid|5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8|[email protected]|2021-01-11 22:35:50.033582|2021-01-11 22:35:50.033582
2|octopus|5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8|[email protected]|2021-01-11 22:35:50.034306|2021-01-11 22:35:50.034306

アプリケーション起動

$ uvicorn app.main:app --reload

http://127.0.0.1:8000/docs へアクセス。

Docker 環境構築

前提

コンテナ作成

環境変数の設定

.env-app.sample.env-postgresql.sampleをリネームし、適宜編集する。

$ cp -p .env-app.sample .env-app
$ cp -p .env-postgresql.sample .env-postgresql

コンテナ起動

$ docker-compose up -d
$ docker-compose ps                                                                    master ◼
  Name                Command               State            Ports
---------------------------------------------------------------------------
api        poetry run uvicorn app.mai ...   Up      0.0.0.0:8000->8000/tcp
postgres   docker-entrypoint.sh postgres    Up      5432/tcp

データベースの初期化

$ docker exec -it -e PYTHONPATH="/app:$PYTHONPATH" api poetry run python app/init_db.py

作成された DB の確認

$ docker-compose exec db psql -U postgres testdb

テーブルの確認。

# \dt
         List of relations
 Schema | Name  | Type  |  Owner
--------+-------+-------+----------
 public | users | table | postgres
(1 row)

レコードの確認。

# select * from users;
 id |  name   |                             password                             |        email        |         created_at         |         updated_at
----+---------+------------------------------------------------------------------+---------------------+----------------------------+----------------------------
  1 | squid   | 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 | [email protected]   | 2020-12-29 21:03:09.169173 | 2020-12-29 21:03:09.169173
  2 | octopus | 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 | [email protected] | 2020-12-29 21:03:09.173484 | 2020-12-29 21:03:09.173484
(2 rows)

http://127.0.0.1:8000/docs へアクセス。

テスト実行

$ pytest

プロジェクトの構造

app
├── api                - API関連
│   └── v1             - API v1関連
│       └── endpoints  - APIのエンドポイントに関する処理
├── core               - configやロガーなど
├── domains            - Enterprise Business Rules
├── exceptions         - 例外
├── injector           - 依存性注入
├── interfaces         - Interface Adapters
│   └── gateways       - DB等の外部資源とのやりとりを行う
└── usecases           - Application Business Rules
    └── users          - ユーザ操作系処理

参考

clean-architecture-fastapi's People

Contributors

massa423 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.