You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
5 years ago
|
import click
|
||
|
from molten.contrib.sqlalchemy import EngineData
|
||
|
from casbin_api.index import create_app
|
||
|
|
||
|
_, app = create_app()
|
||
|
|
||
|
|
||
|
@click.group()
|
||
|
def cli():
|
||
|
pass
|
||
|
|
||
|
|
||
|
@cli.command()
|
||
|
@click.option("--host", "-h", default="0.0.0.0", help="A hostname or IP address")
|
||
|
@click.option(
|
||
|
"--port", "-p", default=8000, help="Port number to bind to development server"
|
||
|
)
|
||
|
def runserver(host, port):
|
||
|
"""
|
||
|
Runs a Werkzueg development server. Do no use for production.
|
||
|
"""
|
||
|
from werkzeug.serving import run_simple
|
||
|
|
||
|
run_simple(
|
||
|
hostname=host, port=port, application=app, use_debugger=True, use_reloader=True
|
||
|
)
|
||
|
|
||
|
|
||
|
@cli.command()
|
||
|
def shell():
|
||
|
"""
|
||
|
Enters an interactive shell with an app instance and dependency resolver.
|
||
|
"""
|
||
|
import rlcompleter
|
||
|
import readline
|
||
|
from code import InteractiveConsole
|
||
|
|
||
|
helpers = {"app": app, "resolver": app.injector.get_resolver()}
|
||
|
|
||
|
readline.parse_and_bind("tab: complete")
|
||
|
interpreter = InteractiveConsole(helpers)
|
||
|
interpreter.interact(f"Instances in scope: {', '.join(helpers)}.", "")
|
||
|
|
||
|
|
||
|
@cli.command()
|
||
|
def initdb():
|
||
|
"""
|
||
|
Initialize database
|
||
|
"""
|
||
|
click.echo("This feature has been commented out. Please use alembic to manage your database initialization and changes.")
|
||
|
# from casbin_api.db import Base
|
||
|
#
|
||
|
# def _init(engine_data: EngineData):
|
||
|
# Base.metadata.create_all(bind=engine_data.engine)
|
||
|
#
|
||
|
# click.echo("Creating database")
|
||
|
# app.injector.get_resolver().resolve(_init)()
|
||
|
# click.echo("Database created")
|
||
|
|
||
|
|
||
|
@cli.command()
|
||
|
def dropdb():
|
||
|
"""
|
||
|
Drop all tables in database
|
||
|
"""
|
||
|
|
||
|
from casbin_api.db import Base
|
||
|
|
||
|
def _drop(engine_data: EngineData):
|
||
|
Base.metadata.drop_all(bind=engine_data.engine)
|
||
|
|
||
|
click.echo("Are you sure you would like to drop the database?: [Y/N]")
|
||
|
response = input()
|
||
|
if response.lower() == "y":
|
||
|
app.injector.get_resolver().resolve(_drop)()
|
||
|
click.echo("Database dropped")
|
||
|
else:
|
||
|
click.echo("Database drop aborted")
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
cli()
|