https://github.com/gregadc/cookiecutter-flask-all-in-one
https://github.com/gregadc/cookiecutter-flask-all-in-one
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/gregadc/cookiecutter-flask-all-in-one
- Owner: gregadc
- License: mit
- Created: 2020-11-24T17:07:49.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-08-22T11:05:37.000Z (over 1 year ago)
- Last Synced: 2024-08-14T07:08:49.305Z (8 months ago)
- Language: Python
- Size: 299 KB
- Stars: 33
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - gregadc/cookiecutter-flask-all-in-one - (Python)
README
# Cookiecutter Flask All-In-One
A Flask template that uses cookiecutter. (Supports Python >= 3.7)




![]()
## Introduction
Cookiecutter-Flask-All-In-One is an easy tool that allows you to quickly create a flask project.
It allows you to easily launch a REST API using JWT authentication tokens, to use GragphQl or just the user interface with the possibility to authenticate with Google, Facebook etc.## Use it now
First of all you need to install [cookiecutter](https://cookiecutter.readthedocs.io/en/1.7.2/)
Launch `pip install cookiecutter`Once cookiecutter installed, run this command to create the project
```
cookiecutter https://github.com/gregadc/cookiecutter-flask-all-in-one.git
```You have the possibility to change the values of the cookiecutter.json file in order to change the name of the project, the name of the application, or whatever you want want and relaunch the command
```json
{
"project_name": "cookiecutter-flask-all-in-one",
"app_name": "myapp",
"python_version": ["3.8", "3.6", "3.7"],
"full_name": "admin",
"email": "[email protected]",
"password": "admin",
"debug": [0, 1],
"_copy_without_render": [
"{{cookiecutter.project_name}}/app/templates/*.html",
"*.html"
]
}
```Once the project is created
```bashcd cookiecutter-flask-all-in-one
pip install -r requirements.txt
pip install -e .```
The database to use is postgres but you can change the DATABASE_URI of the .env if you want to use another one
If you don't have postgres and you are comfortable with docker run this command it will create a docker postgres container for you```bash
docker run --name db_flask_app -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test -e POSTGRES_DB=test -p 5432:5432 -d postgres
```Run this command to create the database and create a user
`myapp init`You can if you wish to change the `myapp/.env` which contains the default configuration of the project.
```
SECRET_KEY="secret"
FLASK_APP="wsgi.py"
APP_NAME=myapp
EMAIL="[email protected]"
PASSWORD_EMAIL="password"
FLASK_ENV="development"
DATABASE_URI="postgres://test:test@localhost/test"# Facebook credentials
FACEBOOK_OAUTH_CLIENT_ID="id"
FACEBOOK_OAUTH_CLIENT_SECRET="secret-id"# Twitter credentials
TWITTER_OAUTH_API_KEY="id"
TWITTER_OAUTH_API_SECRET="secret-id"# Google credentials
GOOGLE_OAUTH_CLIENT_ID="id"
GOOGLE_OAUTH_CLIENT_SECRET="secret-id"
```## Run project
```bash
(venv-kaCAdD68-py3.8) test@test:~/cookiecutter-flask-all-in-one-test$ myapp run
* Serving Flask app "wsgi.py"
* Environment: development
* Debug mode: off
[2020-11-26 20:18:34,913: INFO | werkzeug] * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
```We have multiple ways to use this project.
### User interface
Open a browser, and enter the url which provides a graphical interface and a basic authentication service, but also the possibility of using the google, facebook services if you have entered the necessary credentials
![]()
If you use Google, Facebook, or Twitter not forget to create a development application with these providers.
Here are the redirect urls you need to provide to your applications
```bash
http://localhost:5000/login/google/authorized
http://localhost:5000/login/twitter/authorized
etc ...
```
If you want to use other providers like github or linkedin look at [flask-dance](https://flask-dance.readthedocs.io/en/latest/) which allowed to implement these services.### REST api
You can use the rest api, and his authentication using Flask-JWT-Extended including access token and refresh token management
A first call to the API will return the tokens to you
```bash
curl -X POST --header "Content-Type: application/json" -d '{"username": "admin", "password": "admin"}' http://localhost:5000/api/login{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MDY0MjQzOTAsIm5iZiI6MTYwNjQyNDM5MCwianRpIjoiYWU3ZjdiZjctMjkzYS00OTcwLThlNGYtNjMwNGY4MGExYjZhIiwiZXhwIjoxNjA2NDI1MjkwLCJpZGVudGl0eSI6InRvdG8iLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MifQ.Dbly2Mln9cD6olEMnoCxvoJ1rUGdjQgP6ekGJY5sOXw"
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MDY0MjQ0NTgsIm5iZiI6MTYwNjQyNDQ1OCwianRpIjoiN2MyZGY2ZjAtZDZhZC00ZTI4LTk0NTktMDMyYzkwMDFkODI3IiwiZXhwIjoxNjA5MDE2NDU4LCJpZGVudGl0eSI6InRvdG8iLCJ0eXBlIjoicmVmcmVzaCJ9.IkQ93fdjH-w3veiVOZf92fTPbkrLXwXp37I5uBCgIYU"
}
```
Once the tokens have been recovered you can now use the API to consult the resources```bash
export ACCESS="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MDY0MjQzOTAsIm5iZiI6MTYwNjQyNDM5MCwianRpIjoiYWU3ZjdiZjctMjkzYS00OTcwLThlNGYtNjMwNGY4MGExYjZhIiwiZXhwIjoxNjA2NDI1MjkwLCJpZGVudGl0eSI6InRvdG8iLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MifQ.Dbly2Mln9cD6olEMnoCxvoJ1rUGdjQgP6ekGJY5sOXw"
curl -X GET --header "Content-Type: application/json" --header "Authorization: Bearer $ACCESS" http://localhost:5000/api/users
```You can also refresh the token
```bash
export REFRESH="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MDY0MjQ0NTgsIm5iZiI6MTYwNjQyNDQ1OCwianRpIjoiN2MyZGY2ZjAtZDZhZC00ZTI4LTk0NTktMDMyYzkwMDFkODI3IiwiZXhwIjoxNjA5MDE2NDU4LCJpZGVudGl0eSI6InRvdG8iLCJ0eXBlIjoicmVmcmVzaCJ9.IkQ93fdjH-w3veiVOZf92fTPbkrLXwXp37I5uBCgIYU"
curl -X GET --header "Content-Type: application/json" --header "Authorization: Bearer $REFRESH" http://localhost:5000/api/refresh{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MDY0MjUzNjUsIm5iZiI6MTYwNjQyNTM2NSwianRpIjoiZmQ3Y2RiMmYtZTZlZC00NDdmLTg3ZWItNzg2ZTc1ZGY0NzYzIiwiZXhwIjoxNjA2NDI2MjY1LCJpZGVudGl0eSI6InRvdG8iLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MifQ.hLrksk8nKrqY4wED1QYOGjIXhR-Rq7Zf_TalhPsJ-sI"
}
```You can list your tokens and revoke them if you want
```bash
curl -X GET --header "Content-Type: application/json" --header "Authorization: Bearer $NEW_ACCESS http://localhost:5000/api/tokens
```
This endpoint will return the tokens with their id so that you can revoke them
```bash
curl -X GET --header "Content-Type: application/json" --header "Authorization: Bearer $NEW_ACCESS http://localhost:5000/api/revoke_token/{
"message": "token revoked"
}
```### GraphQL
You also have the possibility of viewing GraphQl resources if you are connected, launch `http://0.0.0.0:5000/graphql` in your browser
Used packages :
* [Graphene](https://docs.graphene-python.org/en/latest/)
* [Flask-GraphQL](https://github.com/graphql-python/flask-graphql)
* [graphene-sqlalchemy](https://readthedocs.org/projects/graphene-sqlalchemy/)
![]()
The second solution to consult Graphql resources is to use the tokens generated above
```bash
# specific user
curl -X GET --header 'Content-Type: application/graphql' --header "Authorization: Bearer $ACCESS" --data-raw '{ users(userId:1){ edges{ node{ email, username } } } }' http://0.0.0.0:5000/graphql# all users
curl -X GET --header 'Content-Type: application/graphql' --header "Authorization: Bearer $ACCESS" --data-raw '{ users{ edges{ node{ email, username } } } }' http://0.0.0.0:5000/graphql
```## Docker deployment
You have the possibility to use docker by launching the docker-compose, you are free to modify it if you want to use it in production
```bash
docker-compose up -d --build
```
This command will create your containers and the image## Testing
To run the tests the project, we uses [Tox](https://tox.readthedocs.io/en/latest/) which is a command line driven automated test tool for PythonRun the linter with [flake8](https://flake8.pycqa.org/en/latest/)
```bash
tox -e lint
```
Run tests
```bash
tox -e myapp
```