Skip to content

Commit

Permalink
<configure> .flaskenv integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kirianguiller committed Oct 16, 2020
1 parent 42d860f commit 3c4b34a
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ celerybeat-schedule
*.sage.py

# Environments
.flaskenv
.env
.venv
env/
Expand Down
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ A sample project showing how to build a scalable, maintainable, modular Flask AP

_This is an example project using the structure proposed in [this blog post](http://alanpryorjr.com/2019-05-20-flask-api-example/)._

## Setting the environment config
Create a .flaskenv file with the following config
```
FLASK_ENV=env|prod|test
```

## Running the app

## Setting the python virtual environment

Preferably, first create a virtualenv and activate it, perhaps with the following command:

Expand All @@ -24,14 +30,10 @@ pip install -r requirements.txt

to get the dependencies.

### Manage the database

Explicitely set the environment (```dev``` ,```prod``` and ```test```)
```
export FLASK_CONFIG=dev
```
### Manage the database

Next, initialize the database
If first time, initialize the database

```
python manage.py seed_db
Expand All @@ -42,11 +44,15 @@ Type "Y" to accept the message (which is just there to prevent you accidentally

### Run the app for local development

The following command is a shorthand for running the development server
In .flaskenv, set the `FLASK_ENV` to dev
```
python dev.py
FLASK_ENV=dev
```

Then, you can run the app
```
python wsgi.py
```

## Running tests

Expand All @@ -60,7 +66,7 @@ pytest

### Deploy the app in production

##### Folder settup
##### Set the environment

On the server, create the project folder

Expand All @@ -78,6 +84,11 @@ Install the python packages requirements
pip install -r requirements.txt
```

In .flaskenv, set `FLASK_ENV` to prod
```
FLASK_ENV=prod
```

Initialize the DB (or paste an existant one)
```
python migrate.py seed_db
Expand All @@ -90,4 +101,8 @@ If needed to, allow the folder access to the server user that will have control
sudo chown -Rf <admin>:<group> /var/www/flask_api_example/
```



source : https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

Navigate to the posted URL in your terminal to be greeted with Swagger, where you can test out the API.
2 changes: 1 addition & 1 deletion app/fizz/fizzbar/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class FizzbarResource(Resource):
"""Fizzbars"""

@responds(schema=FizzbarSchema, many=True)
@responds(schema=FizzbarSchema(many=True) )
def get(self) -> List[Fizzbar]:
"""Get all Fizzbars"""

Expand Down
2 changes: 1 addition & 1 deletion app/fizz/fizzbar/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mypy_extensions import TypedDict
from typing import TypedDict


class FizzbarInterface(TypedDict, total=False):
Expand Down
2 changes: 1 addition & 1 deletion app/fizz/fizzbaz/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class FizzbazResource(Resource):
"""Fizzbaz"""

@responds(schema=FizzbazSchema, many=True)
@responds(schema=FizzbazSchema(many=True))
def get(self) -> List[Fizzbaz]:
"""Get all Fizzbaz"""

Expand Down
2 changes: 1 addition & 1 deletion app/fizz/fizzbaz/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mypy_extensions import TypedDict
from typing import TypedDict


class FizzbazInterface(TypedDict, total=False):
Expand Down
2 changes: 1 addition & 1 deletion app/other_api/doodad/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class DoodadResource(Resource):
"""Doodads"""

@responds(schema=DoodadSchema, many=True)
@responds(schema=DoodadSchema(many=True))
def get(self) -> List[Doodad]:
"""Get all Doodads"""

Expand Down
2 changes: 1 addition & 1 deletion app/other_api/doodad/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mypy_extensions import TypedDict
from typing import TypedDict


class DoodadInterface(TypedDict, total=False):
Expand Down
2 changes: 1 addition & 1 deletion app/other_api/whatsit/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class WhatsitResource(Resource):
'''Whatsits'''

@responds(schema=WhatsitSchema, many=True)
@responds(schema=WhatsitSchema(many=True))
def get(self) -> List[Whatsit]:
'''Get all Whatsits'''

Expand Down
2 changes: 1 addition & 1 deletion app/other_api/whatsit/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mypy_extensions import TypedDict
from typing import TypedDict


class WhatsitInterface(TypedDict, total=False):
Expand Down
2 changes: 1 addition & 1 deletion app/widget/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class WidgetResource(Resource):
"""Widgets"""

@responds(schema=WidgetSchema, many=True)
@responds(schema=WidgetSchema(many=True))
def get(self) -> List[Widget]:
"""Get all Widgets"""

Expand Down
2 changes: 1 addition & 1 deletion app/widget/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mypy_extensions import TypedDict
from typing import TypedDict


class WidgetInterface(TypedDict, total=False):
Expand Down
4 changes: 3 additions & 1 deletion config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ socket = flask_api_example.sock
chmod-socket = 660
vacuum = true

die-on-term = true
die-on-term = true

/var/www/flask_api_example
5 changes: 0 additions & 5 deletions dev.py

This file was deleted.

5 changes: 4 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from app import create_app, db
from commands.seed_command import SeedCommand

env = os.getenv("FLASK_CONFIG") or "test"
from dotenv import load_dotenv
load_dotenv(dotenv_path=".flaskenv", verbose=True)

env = os.getenv("FLASK_ENV") or "test"
print(f"Active environment: * {env} *")
app = create_app(env)

Expand Down
39 changes: 20 additions & 19 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
aniso8601==7.0.0
attrs==19.1.0
aniso8601==8.0.0
attrs==20.2.0
cffi==1.14.3
Click==7.0
click==7.1.2
cryptography==3.1.1
Flask==1.1.1
flask-accepts==0.10.0
Flask-RESTful==0.3.7
flask-restplus==0.12.1
flask-restx==0.1.0
Flask==1.1.2
flask-accepts==0.17.4
Flask-RESTful==0.3.8
flask-restplus==0.13.0
flask-restx==0.2.0
Flask-Script==2.0.6
Flask-SQLAlchemy==2.4.0
Flask-SQLAlchemy==2.4.4
iniconfig==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.1
jsonschema==3.0.2
Jinja2==2.11.2
jsonschema==3.2.0
MarkupSafe==1.1.1
marshmallow==3.2.0
mypy-extensions==0.4.1
marshmallow==3.8.0
numpy==1.19.2
packaging==20.4
pandas==1.1.3
pluggy==0.13.1
py==1.9.0
pycparser==2.20
pyparsing==2.4.7
pyrsistent==0.15.4
pyrsistent==0.17.3
pytest==6.1.1
python-dateutil==2.8.0
pytz==2019.2
six==1.12.0
SQLAlchemy==1.3.6
python-dateutil==2.8.1
python-dotenv==0.14.0
pytz==2020.1
six==1.15.0
SQLAlchemy==1.3.20
toml==0.10.1
Werkzeug==0.15.5
uWSGI==2.0.19.1
Werkzeug==1.0.1
2 changes: 1 addition & 1 deletion wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from app import create_app

app = create_app(os.getenv("FLASK_CONFIG") or "test")
app = create_app(os.getenv("FLASK_ENV") or "test")
if __name__ == "__main__":
app.run(debug=True)

0 comments on commit 3c4b34a

Please sign in to comment.