mirror of
https://github.com/ycd/manage-fastapi.git
synced 2021-11-08 01:34:39 +03:00
📝 Add documentation for 1.0.0
This commit is contained in:
82
README.md
82
README.md
@@ -1,18 +1,16 @@
|
||||
|
||||
|
||||
<h3 align="center">
|
||||
<strong>Manage FastAPI projects easily</strong>
|
||||
</h3>
|
||||
<p align="center">
|
||||
<a href="https://github.com/ycd/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/github/last-commit/ycd/manage-fastapi?style=for-the-badge" alt="Latest Commit">
|
||||
</a>
|
||||
<img src="https://img.shields.io/github/workflow/status/ycd/manage-fastapi/Test?style=for-the-badge">
|
||||
<img src="https://img.shields.io/codecov/c/github/ycd/manage-fastapi?style=for-the-badge">
|
||||
<br />
|
||||
<a href="https://pypi.org/project/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/pypi/v/manage-fastapi?style=for-the-badge" alt="Package version">
|
||||
</a>
|
||||
<a href="https://github.com/ycd/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/github/last-commit/ycd/manage-fastapi?style=for-the-badge" alt="Latest Commit">
|
||||
</a>
|
||||
<img src="https://img.shields.io/github/workflow/status/ycd/manage-fastapi/Test?style=for-the-badge">
|
||||
<img src="https://img.shields.io/codecov/c/github/ycd/manage-fastapi?style=for-the-badge">
|
||||
<br />
|
||||
<a href="https://pypi.org/project/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/pypi/v/manage-fastapi?style=for-the-badge" alt="Package version">
|
||||
</a>
|
||||
<img src="https://img.shields.io/pypi/pyversions/manage-fastapi?style=for-the-badge">
|
||||
<img src="https://img.shields.io/github/license/ycd/manage-fastapi?style=for-the-badge">
|
||||
</p>
|
||||
@@ -25,50 +23,54 @@
|
||||
|
||||
**Source Code**: View it on [Github](https://github.com/ycd/manage-fastapi/)
|
||||
|
||||
**Installation**: `pip install manage-fastapi`
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
## Features 🚀
|
||||
|
||||
* #### Creates customizable **project boilerplate.**
|
||||
* #### Creates customizable **app boilerplate.**
|
||||
* #### Handles the project structuring for you.
|
||||
|
||||
## Installation
|
||||
|
||||
Manage FastAPI can be installed by running `pip install manage-fastapi`. It requires Python 3.6+ to run.
|
||||
|
||||
## Example folder structure 📦
|
||||
```
|
||||
fastproject/
|
||||
├── __init__.py
|
||||
├── main.py
|
||||
├── core
|
||||
│ ├── models
|
||||
│ │ ├── database.py
|
||||
│ │ └── __init__.py
|
||||
│ ├── schemas
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── schema.py
|
||||
│ └── settings.py
|
||||
├── tests
|
||||
│ ├── __init__.py
|
||||
│ └── v1
|
||||
│ ├── __init__.py
|
||||
│ └── test_v1.py
|
||||
└── v1
|
||||
├── api.py
|
||||
├── endpoints
|
||||
│ ├── endpoint.py
|
||||
│ └── __init__.py
|
||||
└── __init__.py
|
||||
## Usage
|
||||
|
||||
To get started right away with sensible defaults:
|
||||
|
||||
```bash
|
||||
fastapi startproject {name}
|
||||
```
|
||||
|
||||
## Installation 📌
|
||||
You can run _Manage FastAPI_ as a package if running it as a script doesn’t work:
|
||||
|
||||
`pip install manage-fastapi`
|
||||
```bash
|
||||
python -m fastapi startproject {name}
|
||||
```
|
||||
|
||||
## Command line options
|
||||
|
||||
Manage FastAPI has three commands for now. You can list them by running `fastapi --help`:
|
||||
|
||||
<img src="docs/docs_assets/fastapi-help.png" width=700>
|
||||
|
||||
The idea is to have a highly customizable CLI, but at the same time a simple interface for new users. You can see the available options for `startproject` running `fastapi startproject --help`:
|
||||
|
||||
<img src="docs/docs_assets/startproject-help.png" width=700>
|
||||
|
||||
The other commands are already available but the current implementation is too shallow. More details about `startapp` and `run` commands will be provided once they have more functionalities, at the moment you can run `startapp` by just:
|
||||
|
||||
```bash
|
||||
fastapi startapp {name}
|
||||
```
|
||||
|
||||
On the other hand, the `run` command expects you to have a `startproject` structure:
|
||||
|
||||
```bash
|
||||
fastapi run
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
|
||||
|
||||
<h3 align="center">
|
||||
<strong>Manage FastAPI projects easily</strong>
|
||||
</h3>
|
||||
<p align="center">
|
||||
<a href="https://github.com/ycd/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/github/last-commit/ycd/manage-fastapi?style=for-the-badge" alt="Latest Commit">
|
||||
</a>
|
||||
<img src="https://img.shields.io/github/workflow/status/ycd/manage-fastapi/Test?style=for-the-badge">
|
||||
<img src="https://img.shields.io/codecov/c/github/ycd/manage-fastapi?style=for-the-badge">
|
||||
<br />
|
||||
<a href="https://pypi.org/project/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/pypi/v/manage-fastapi?style=for-the-badge" alt="Package version">
|
||||
</a>
|
||||
<a href="https://github.com/ycd/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/github/last-commit/ycd/manage-fastapi?style=for-the-badge" alt="Latest Commit">
|
||||
</a>
|
||||
<img src="https://img.shields.io/github/workflow/status/ycd/manage-fastapi/Test?style=for-the-badge">
|
||||
<img src="https://img.shields.io/codecov/c/github/ycd/manage-fastapi?style=for-the-badge">
|
||||
<br />
|
||||
<a href="https://pypi.org/project/manage-fastapi" target="_blank">
|
||||
<img src="https://img.shields.io/pypi/v/manage-fastapi?style=for-the-badge" alt="Package version">
|
||||
</a>
|
||||
<img src="https://img.shields.io/pypi/pyversions/manage-fastapi?style=for-the-badge">
|
||||
<img src="https://img.shields.io/github/license/ycd/manage-fastapi?style=for-the-badge">
|
||||
</p>
|
||||
@@ -39,36 +37,9 @@
|
||||
|
||||
|
||||
|
||||
## Example folder structure 📦
|
||||
```
|
||||
fastproject/
|
||||
├── __init__.py
|
||||
├── main.py
|
||||
├── core
|
||||
│ ├── models
|
||||
│ │ ├── database.py
|
||||
│ │ └── __init__.py
|
||||
│ ├── schemas
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── schema.py
|
||||
│ └── settings.py
|
||||
├── tests
|
||||
│ ├── __init__.py
|
||||
│ └── v1
|
||||
│ ├── __init__.py
|
||||
│ └── test_v1.py
|
||||
└── v1
|
||||
├── api.py
|
||||
├── endpoints
|
||||
│ ├── endpoint.py
|
||||
│ └── __init__.py
|
||||
└── __init__.py
|
||||
```
|
||||
|
||||
## Installation 📌
|
||||
|
||||
`pip install manage-fastapi`
|
||||
## Usage
|
||||
|
||||
<img src="../docs_assets/fastapi-help.png" width=700>
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
## Creating a new project with Manage FastAPI
|
||||
|
||||
Starting new apps works just like `startproject`, since you are familiar with that command feel free to skip this part.
|
||||
|
||||
* `manage-fastapi startapp [app-name]` - Creates a new app.
|
||||
|
||||
Let's keep working on the project that we created in Managing Projects.
|
||||
|
||||
#### Let's create a new app called `v1`
|
||||
|
||||
```shell
|
||||
manage-fastapi startapp v1
|
||||
|
||||
Application v1 created successfully!
|
||||
```
|
||||
|
||||
Let's see what it created. Now we have a new folder called **v1** and another folder called **v1** under our **tests** folder. Let's see what they have.
|
||||
|
||||
```python
|
||||
fastproject/
|
||||
├── __init__.py
|
||||
├── main.py
|
||||
├── core
|
||||
│ ├── models
|
||||
│ │ ├── database.py
|
||||
│ │ └── __init__.py
|
||||
│ ├── schemas
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── schema.py
|
||||
│ └── settings.py
|
||||
├── tests
|
||||
│ ├── __init__.py
|
||||
│ └── v1
|
||||
│ ├── __init__.py
|
||||
│ └── test_v1.py
|
||||
└── v1
|
||||
├── api.py
|
||||
├── endpoints
|
||||
│ ├── endpoint.py
|
||||
│ └── __init__.py
|
||||
└── __init__.py
|
||||
```
|
||||
|
||||
In our **`fastproject/v1`** we have new **1 directory and 4 files**, let's see what they have.
|
||||
|
||||
In our `endpoints` folder we are going create all the endpoints for this app, also `endpoints.py` comes with a basic Hello world router,
|
||||
|
||||
```python
|
||||
from fastapi import APIRouter, Body, Depends
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def hello_fastapi():
|
||||
return {"Hello":"FastAPI"}
|
||||
```
|
||||
|
||||
In our `api.py` we are getting all endpoints together to use from our **`main.py`**,
|
||||
|
||||
```python
|
||||
# This is an example of how you can route and you are free to change
|
||||
# this will not affect your be included unless you add
|
||||
# from myapp.api import api_router
|
||||
# to your main.py on your project folder
|
||||
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from myapp.endpoints import endpoint
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(endpoint.router, prefix="/hello", tags=["myapp"])
|
||||
```
|
||||
@@ -1,154 +0,0 @@
|
||||
|
||||
To start a new project with Manage FastAPI, you can use this:
|
||||
|
||||
* `manage-fastapi startproject [project-name]` - Create a new project.
|
||||
|
||||
This will create create **4 directories and 8 files** for you. Let's see what it includes, for instance i'm creating a new project called **fastproject**
|
||||
|
||||
```shell
|
||||
manage-fastapi startproject fastproject
|
||||
|
||||
Project fastproject created successfully!
|
||||
```
|
||||
|
||||
The command we ran above, created a `main.py` that will include all our external app's. A folder called **models** for our database stuff, another folder called **schemas** for our Pydantic models etc and a `settings.py` file.
|
||||
|
||||
```shell
|
||||
fastproject/
|
||||
├── __init__.py
|
||||
├── main.py
|
||||
├── core
|
||||
│ ├── models
|
||||
│ │ ├── database.py
|
||||
│ │ └── __init__.py
|
||||
│ ├── schemas
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── schema.py
|
||||
│ └── settings.py
|
||||
└── tests
|
||||
└── __init__.py
|
||||
```
|
||||
|
||||
Our **`main.py`** gonna be our controller. It will include all the routers other settings stuff to make our project more clean and easy to maintain.
|
||||
|
||||
```python
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from fastproject.settings import settings
|
||||
from fastproject.models.database import database
|
||||
|
||||
app = FastAPI(title=settings.PROJECT_NAME)
|
||||
|
||||
if settings.BACKEND_CORS_ORIGINS:
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def connect_database():
|
||||
await database.connect()
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def disconnect_database():
|
||||
await database.disconnect()
|
||||
```
|
||||
|
||||
In **`settings.py`** we have the settings for all of our project, it comes with just a database settings but you can use the **`Settings`** class to include all your OAuth tokens secrets etc.
|
||||
|
||||
```python
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from pydantic import BaseSettings, AnyHttpUrl, HttpUrl, validator
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
PROJECT_NAME: str = "fastproject"
|
||||
|
||||
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [
|
||||
"http://localhost",
|
||||
"http://localhost:80",
|
||||
"http://localhost:8000",
|
||||
]
|
||||
|
||||
@validator("BACKEND_CORS_ORIGINS", pre=True)
|
||||
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
|
||||
if isinstance(v, str) and not v.startswith("["):
|
||||
return [i.strip() for i in v.split(",")]
|
||||
elif isinstance(v, (list, str)):
|
||||
return v
|
||||
raise ValueError(v)
|
||||
|
||||
# Database Settings
|
||||
DB_SERVER: Optional[str] = ""
|
||||
DB_USER: Optional[str] = ""
|
||||
DB_PASSWORD: Optional[str] = ""
|
||||
DB_PORT: Optional[str] = ""
|
||||
DB_NAME: Optional[str] = ""
|
||||
DB_PORT: Optional[str] = ""
|
||||
|
||||
# DATABASE_URL: Optional[
|
||||
# str
|
||||
# ] = f"{DB_SERVER}://{DB_USER}:{DB_PASSWORD}@{DB_PORT}:{DB_PORT}/{DB_NAME}"
|
||||
|
||||
|
||||
# DATABASE_URL = "postgresql://user:passowrd@localhost:5432/DB_NAME"
|
||||
DATABASE_URL = "sqlite:///./test.db"
|
||||
|
||||
|
||||
|
||||
class Config:
|
||||
case_sensitive = True
|
||||
|
||||
|
||||
settings = Settings()
|
||||
```
|
||||
|
||||
In **`models/database.py`** we create all our database stuff, If you don't need database you might want to skip this. **`database.py`** uses [**Databases**](https://github.com/encode/databases) library which is a great library to provide **Async** database support for Python.
|
||||
|
||||
```python
|
||||
import sqlalchemy
|
||||
from fastproject.settings import settings
|
||||
import databases
|
||||
|
||||
database = databases.Database(settings.DATABASE_URL)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
# Put your database models here | Below
|
||||
|
||||
# FastAPI documentation for databases: https://fastapi.tiangolo.com/advanced/async-sql-databases/
|
||||
|
||||
# Put your database models here | Above
|
||||
|
||||
|
||||
engine = sqlalchemy.create_engine(settings.DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
```
|
||||
|
||||
Under **`schemas`** we will declare all our models. **`schemas/schema.py`** comes with a example class. Feel free to delete it if you are experienced with **FastAPI**.
|
||||
|
||||
```python
|
||||
# For more information check Pydantic documentation = https://pydantic-docs.helpmanual.io/usage/models/
|
||||
#
|
||||
# Creating your custom classes
|
||||
#
|
||||
# class ClassName(BaseModel):
|
||||
# name: str
|
||||
# value: int
|
||||
#
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Model(BaseModel):
|
||||
pass
|
||||
```
|
||||
|
||||
---
|
||||
### Let's skip to the next documentation to see how we can create new apps.
|
||||
---
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
### Latest Changes
|
||||
|
||||
### 1.0.0
|
||||
|
||||
* Add back `run` command, previously known as `run-server`
|
||||
|
||||
### 0.1.60
|
||||
|
||||
* Delete run-server command
|
||||
@@ -9,17 +13,14 @@
|
||||
* Create new template for settings without database
|
||||
* Small fix for project utils
|
||||
|
||||
|
||||
### 0.1.52
|
||||
|
||||
* Temporary fix for Path issue when running with uvicorn
|
||||
|
||||
|
||||
### 0.1.51
|
||||
|
||||
* Little update on API template
|
||||
|
||||
|
||||
### 0.1.5
|
||||
|
||||
* Added showmodels
|
||||
@@ -27,24 +28,20 @@
|
||||
* Fix little bugs
|
||||
* Update docs
|
||||
|
||||
|
||||
### 0.1.41
|
||||
|
||||
* Quick fix for a little bug
|
||||
|
||||
|
||||
### 0.1.4
|
||||
|
||||
* Changed project architecture
|
||||
* Increased travis tests
|
||||
|
||||
|
||||
### 0.1.3
|
||||
|
||||
* Make database optional
|
||||
* Now Manage FastAPI has support for MongoDB, PostgreSQL, SQLite, MySQL, Tortoise ORM
|
||||
|
||||
|
||||
### 0.1.2
|
||||
|
||||
* Add tests
|
||||
|
||||
BIN
docs/docs_assets/fastapi-help.png
Normal file
BIN
docs/docs_assets/fastapi-help.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 191 KiB |
BIN
docs/docs_assets/fastapi-startproject-usage.png
Normal file
BIN
docs/docs_assets/fastapi-startproject-usage.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 115 KiB |
BIN
docs/docs_assets/startproject-help.png
Normal file
BIN
docs/docs_assets/startproject-help.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 178 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 71 KiB |
3
manage_fastapi/__main__.py
Normal file
3
manage_fastapi/__main__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .main import app
|
||||
|
||||
app(prog_name="fastapi")
|
||||
Reference in New Issue
Block a user