-
Notifications
You must be signed in to change notification settings - Fork 72
/
mini_app.py
executable file
·42 lines (31 loc) · 1015 Bytes
/
mini_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# run:
# $ FLASK_APP=mini_app flask run
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from safrs import SAFRSBase, SafrsApi
db = SQLAlchemy()
class User(SAFRSBase, db.Model):
"""
description: My User description
"""
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
email = db.Column(db.String)
def create_api(app, host="127.0.0.1", port=5000, prefix=""):
api = SafrsApi(app, host=host, port=port, prefix=prefix)
api.expose_object(User)
User(name="test", email="[email protected]") # this will automatically commit the user!
print(f"Starting API: http://{host}:{port}/{prefix}")
def create_app(host="127.0.0.1"):
app = Flask("demo_app")
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite:///")
db.init_app(app)
with app.app_context():
db.create_all()
create_api(app, host)
return app
app = create_app()
if __name__ == "__main__":
app.run()