Skip to content

Commit

Permalink
Release version 0.1.3 📣
Browse files Browse the repository at this point in the history
Release version 0.1.3
  • Loading branch information
ycd authored Aug 13, 2020
2 parents 4695df5 + 063de67 commit c39b81e
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 90 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ newproject/

### Latest Changes

### 0.1.3

* Make database optional
* Now Manage FastAPI has support for MongoDB, PostgreSQL, SQLite, MySQL, Tortoise ORM

### 0.1.2

* Add tests
Expand Down
26 changes: 13 additions & 13 deletions docs/managing_projects/startproject.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ class Settings(BaseSettings):
raise ValueError(v)

# Database Settings
DATABASE_SERVER: Optional[str] = ""
DATABASE_USER: Optional[str] = ""
DATABASE_PASSWORD: Optional[str] = ""
DATABASE_PORT: Optional[str] = ""
DATABASE_NAME: Optional[str] = ""
DATABASE_HOST: Optional[str] = ""

# SQLALCHEMY_DATABASE_URL: Optional[
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"{DATABASE_SERVER}://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}"
# ] = f"{DB_SERVER}://{DB_USER}:{DB_PASSWORD}@{DB_PORT}:{DB_PORT}/{DB_NAME}"


# SQLALCHEMY_DATABASE_URL = "postgresql://user:passowrd@localhost:5432/database_name"
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
# DATABASE_URL = "postgresql://user:passowrd@localhost:5432/DB_NAME"
DATABASE_URL = "sqlite:///./test.db"



Expand All @@ -116,7 +116,7 @@ import sqlalchemy
from newproject.settings import settings
import databases

database = databases.Database(settings.SQLALCHEMY_DATABASE_URL)
database = databases.Database(settings.DATABASE_URL)
metadata = sqlalchemy.MetaData()

# Put your database models here | Below
Expand All @@ -126,7 +126,7 @@ metadata = sqlalchemy.MetaData()
# Put your database models here | Above


engine = sqlalchemy.create_engine(settings.SQLALCHEMY_DATABASE_URL)
engine = sqlalchemy.create_engine(settings.DATABASE_URL)
metadata.create_all(engine)
```

Expand Down
6 changes: 6 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

### Latest Changes

### 0.1.3

* Make database optional
* Now Manage FastAPI has support for MongoDB, PostgreSQL, SQLite, MySQL, Tortoise ORM


### 0.1.2

* Add tests
Expand Down
19 changes: 16 additions & 3 deletions manage_fastapi/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import typer
from .utils import start_app, start_project
from .utils import start_app, start_project, select_database

app = typer.Typer(add_completion=False, help="Managing FastAPI projects made easy!")

app = typer.Typer(
add_completion=False,
help="Managing FastAPI projects made easy!",
name="Manage FastAPI",
)


@app.command(help="Creates a project with the given name.")
def startproject(projectname: str = typer.Argument(...)):
start_project(projectname)
database_option = select_database()
start_project(projectname, database_option=database_option)


@app.command(help="Creates a app with the given name.")
def startapp(appname: str = typer.Argument(...)):
start_app(appname)


# TODO
# @app.command(help="Runs server")
# def runserver(server: str = typer.Argument("uvicorn")):
# run_server()

247 changes: 193 additions & 54 deletions manage_fastapi/templates.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
main_template = """from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from {project_name}.settings import settings
from {project_name}.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()
database_options_template = """PostgreSQL - SQLite - MySQL [0]
Tortoise [1]
MongoDB [2]
Create without Database [9]
@app.on_event("shutdown")
async def disconnect_database():
await database.disconnect()
"""

schema_template = """# For more information check Pydantic documentation = https://pydantic-docs.helpmanual.io/usage/models/
Expand All @@ -41,23 +21,6 @@ class Model(BaseModel):
pass
"""

database_template = """import sqlalchemy
from {project_name}.settings import settings
import databases
database = databases.Database(settings.SQLALCHEMY_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.SQLALCHEMY_DATABASE_URL)
metadata.create_all(engine)
"""

settings_template = """from typing import Any, Dict, List, Optional, Union
Expand All @@ -82,20 +45,16 @@ def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str
raise ValueError(v)
# Database Settings
DATABASE_SERVER: Optional[str] = ""
DATABASE_USER: Optional[str] = ""
DATABASE_PASSWORD: Optional[str] = ""
DATABASE_PORT: Optional[str] = ""
DATABASE_NAME: Optional[str] = ""
DATABASE_HOST: Optional[str] = ""
# SQLALCHEMY_DATABASE_URL: Optional[
# str
# ] = f"{DATABASE_SERVER}://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}"
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: str = f"{DB_SERVER}://{DB_USER}:{DB_PASSWORD}@{DB_PORT}:{DB_PORT}/{DB_NAME}"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:passowrd@localhost:5432/database_name"
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
DATABASE_URL = "sqlite:///./test.db"
Expand Down Expand Up @@ -161,3 +120,183 @@ def test_read_items():
assert response.status_code == 200
assert response.json() == {"name": "Fighters"}
"""

requirements_template = """fastapi==0.61.0
uvicorn==0.11.8
"""


# TORTOISE Main | Database template

tortoise_main_template = """from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from {project_name}.{project_name}.models.database import Example
from tortoise.contrib.fastapi import HTTPNotFoundError, register_tortoise
from {project_name}.{project_name}.settings import settings
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=["*"],
)
register_tortoise(
app,
db_url=settings.DATABASE_URL,
modules={"models": ["{project_name}.{project_name}.models"]},
generate_schemas=True,
add_exception_handlers=True,
)
"""

tortoise_database_template = """from tortoise import fields, models
from tortoise.contrib.pydantic import pydantic_model_creator
class Example(models.Model):
id = fields.IntField(pk=True)
created_at = fields.DatetimeField(auto_now_add=True)
modified_at = fields.DatetimeField(auto_now=True)
class PydanticMeta:
pass
Example = pydantic_model_creator(Example, name="Example")
"""


# Postgresql, SQLite, MySQL Main | Database

empty_main_template = """from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from {project_name}.settings import settings
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_methods=["*"],
allow_headers=["*"],
)
"""


async_sql_main_template = """from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from {project_name}.{project_name}.settings import settings
from {project_name}.{project_name}.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()
"""

async_sql_database_template = """import sqlalchemy
from {project_name}.{project_name}.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)
"""


### MONGODB
mongo_main_template = """from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from {project_name}.{project_name}.settings import settings
from {project_name}.{project_name}.models.utils import connect_to_mongo, close_mongo_connection
app = FastAPI()
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.add_event_handler("startup", connect_to_mongo)
app.add_event_handler("shutdown", close_mongo_connection)
"""


mongo_database_template = """from motor.motor_asyncio import AsyncIOMotorClient
class DataBase:
client: AsyncIOMotorClient = None
db = DataBase()
async def get_database() -> AsyncIOMotorClient:
return db.client
"""

mongo_utils_template = """import logging
from motor.motor_asyncio import AsyncIOMotorClient
from {project_name}.{project_name}.models.database import db
logger = logging.getLogger(__name__)
async def connect_to_mongo():
db.client = AsyncIOMotorClient(str("localhost:27017"),
maxPoolSize=10,
minPoolSize=10)
logger.info(f"Connecting to mongoDB")
async def close_mongo_connection():
db.client.close()
logger.info(f"Closing to mongoDB")
"""
2 changes: 1 addition & 1 deletion manage_fastapi/tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "0.1.2"
assert __version__ == "0.1.3"
Loading

0 comments on commit c39b81e

Please sign in to comment.