fastapi_template

所属分类:FaaS/Serverless
开发工具:Python
文件大小:0KB
下载次数:0
上传日期:2023-07-31 19:28:15
上 传 者sh-1993
说明:  运行python的FastAPI+无服务器服务器的服务器的模板,
(A template for a server running python s FastAPI + serverless,)

文件列表:
LICENSE (1070, 2023-07-31)
alembic.ini (3373, 2023-07-31)
alembic/ (0, 2023-07-31)
alembic/env.py (2200, 2023-07-31)
alembic/script.py.mako (510, 2023-07-31)
alembic/versions/ (0, 2023-07-31)
alembic/versions/654901b132f4_first_migration.py (872, 2023-07-31)
api/ (0, 2023-07-31)
api/__init__.py (0, 2023-07-31)
api/v1/ (0, 2023-07-31)
api/v1/__init__.py (0, 2023-07-31)
api/v1/foo_routes.py (1007, 2023-07-31)
api/v1/tests/ (0, 2023-07-31)
api/v1/tests/__init__.py (0, 2023-07-31)
api/v1/tests/test_app_routes.py (792, 2023-07-31)
api/v1/tests/test_foo_routes.py (1043, 2023-07-31)
app.py (1943, 2023-07-31)
config/ (0, 2023-07-31)
config/__init__.py (1321, 2023-07-31)
config/default.py (175, 2023-07-31)
config/development.py (553, 2023-07-31)
config/local.py (233, 2023-07-31)
config/production.py (540, 2023-07-31)
config/test.py (236, 2023-07-31)
database.py (648, 2023-07-31)
exceptions.py (384, 2023-07-31)
manage.py (1736, 2023-07-31)
models/ (0, 2023-07-31)
models/__init__.py (265, 2023-07-31)
models/foo_models.py (1322, 2023-07-31)
models/tests/ (0, 2023-07-31)
models/tests/__init__.py (0, 2023-07-31)
models/tests/test_foo_models.py (1712, 2023-07-31)
package-lock.json (195724, 2023-07-31)
package.json (987, 2023-07-31)
poetry.lock (107478, 2023-07-31)
pyproject.toml (772, 2023-07-31)
... ...

# FASTAPI Template App This is a basic template for a [FastAPI](https://fastapi.tiangolo.com/lo/) based app for deployment using the [Serverless](https://www.serverless.com/) framework. ## Libraries used The goal of this template is to provide a sane starting point for larger projects using FastAPI. I make use of the following libraries here: - [Serverless](https://www.serverless.com/) - for handling deployment of the project to AWS Lambda/API Gateway - [serverless-prune-plugin](https://www.serverless.com/plugins/serverless-prune-plugin) - to purge previous deployed versions of functions from AWS lambda - [cross-env](https://www.npmjs.com/package/cross-env) - for running node scripts across platforms - [Poetry](https://python-poetry.org/) - For python packaging and dependency management (an alternative to virtualenv + pip) - [PostgreSQL](https://www.postgresql.org/) - relational database - [SQLAlchemy](https://www.sqlalchemy.org/) - ORM for managing database - [Alembic](https://alembic.sqlalchemy.org/en/latest/) - For database migrations - [Pytest](https://pytest.org) - For running unit tests ## Python version This project uses Python version 3.9. I have not tested it with other versions of Python. ## Requirements If you intend to deploy using the [Serverless](https://www.serverless.com/) framework, first install serverless globally using command: ``` npm install -g serverless ``` If you don't plan to use serverless to deploy your app, you can delete the package.json, package-lock.json and serverless.yml files. You will also need Python 3.9 and PostgreSQL installed locally to run this project. ## Customizing Throughout this project, I use the project name `my-fastapi-app`. You will want to find/replace this with your own project name. # Getting started 1. Add an empty local database + test database (code below assumes you already have the postgres command line tools and a local server setup): ``` createdb my-fastapi-app ceratedb my-fastapi-app-test ``` 2. Run migrate (this doesn't actually modify your database just yet... it just generates a migration script that we will run in the next step) Note: this will setup with the dummy Foo model... if you are just getting started, you should run these commands right away to make sure everything is working. After that, see steps in section "Resetting the database" to start with a fresh DB. ``` python manage.py db migrate # This is for local. For DEV or Prod, run: # env=Development python manage.py db migrate # env=Production python manage.py db migrate ``` 3. Upgrade database (this is the step where the actual DB tables get modified/created) ``` python manage.py db upgrade # This is for local. For DEV or Prod, run: # env=Development python manage.py db upgrade # env=Production python manage.py db upgrade ``` 4. Run unit tests ``` pytest ``` 5. Start the server ``` python manage.py runserver # env=Development python manage.py runserver # env=Production python manage.py runserver ``` Once the server is running, go to http://127.0.0.1:5000 to see the swagger docs and play around. ## Resetting the database The steps above create a table called Foo. I provided this as an example template, but you probably want to define your own tables. To reverse the steps above: 1. Run this command to downgrade your database ``` python manage.py db downgrade ``` 2. Delete the python script that was generate inside of the alembic/versions folder 3. Create your own models/tabels and rerun the steps above!

近期下载者

相关文件


收藏者