Skip to content

caragpe/FlaskHttpServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlaskHttpServer

Setting up the project

Creating a virtual environment

python3 -m venv venv
# Python3 is installed in /usr/local/bin/python3
virtualenv -p /usr/local/bin/python3 venv
# Activate it!
source venv/bin/activate
# To deactivate it, just type `deactivate`

Main dependencies

pip install flask
pip install flask-sqlalchemy
pip install flask-migrate
pip install pytest

If you are using PyCharm, there is a requiremets.txt file that can be set up in Tools > Python Integrated Tools > Packaging > Package requirements file to automatically install these dependencies.

Export app name

export FLASK_APP=server.py

Migrations

flask db migrate -m "Initial migration message"
flask db upgrade

Running the server on port 8081

flask run -p 8081

Testing

To run the tests:

python -m pytest tests/

Also, it can be run from each individual test file using PyCharm (run 'pytest for test_{filename})

Usage

Open two terminals. On the first one, start the server as described before. On the second one, submit GET and POST requests:

# GET request
curl -v -X GET http://127.0.0.1:8081/echo?message={message_here}
# POST request
curl -v -X POST -H "admin-auth: 3344" http://127.0.0.1:8081/set_banner?banner={header_banner_here}

/echo endpoint

The /echo endpoint accepts exclusively GET requests and accepts a single parameter message.

  • If no message is passed or any additional parameter is given, it should return: 406 NOT ACCEPTABLE.
  • If a wrong request type is sent, it should respond with 405 METHOD NOT ALLOWED
  • If the request is fine, the response should contain the message passed in the request.

/set_banner endpoint

The /set_banner endpoint should accept a POST request with a single parameter banner in the POST body.

  • The request should be authenticated with a PIN code set in the headers. The correct pin is 3344 and header name is admin-auth
  • If authentication fails it should return: 403 METHOD NOT ALLOWED
  • If the request is right, response should be empty.
  • After a correct setting, banner should be returned in the header in all further responses to the /echo endpoint.

ToDo's

  • Create a test database
  • Create a test class for the Banner repository
  • Mock database in the command/service test classes
  • Refine correct naming/description of errors
  • Update config file with constants used across the app
  • TBD

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published