commit f09a368ec9bc8fe97ab69c8e1990db28a20ec3b0 Author: Greg Gauthier Date: Thu Feb 25 10:35:10 2021 +0000 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd9359e --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/ +.pytest_cache/ +*.pyc +*.iml +*.lock +*.log \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..66b21c5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# set base image (host OS) +FROM python:3.9 + +# set the working directory in the container +WORKDIR /code + +# copy the dependencies file to the working directory +COPY requirements.txt . + +# install dependencies +RUN pip install -r requirements.txt + +# copy the content of the local src directory to the working directory +COPY test/ . +COPY src/ . + +# Run the tests first +CMD [ "pytest", "-v test_*.py"] + +# command to run on container start +CMD [ "python", "./server.py" ] \ No newline at end of file diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..ac83905 --- /dev/null +++ b/Pipfile @@ -0,0 +1,15 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +flask = "~=1.1.2" +pytest = "==6.2.2" + +[dev-packages] +flask = "~=1.1.2" +pytest = "*" + +[requires] +python_version = "~=3.9" diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 0000000..27355fe --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1,2 @@ +flask ~= 1.1.2 +pytest ~= 6.2.2 \ No newline at end of file diff --git a/app/src/simple.py b/app/src/simple.py new file mode 100755 index 0000000..291b8b1 --- /dev/null +++ b/app/src/simple.py @@ -0,0 +1,64 @@ +from random import randint +from secrets import choice +from string import ascii_letters +from flask import Flask, json, request + +app = Flask(__name__) + + +@app.route("/") +def index(): + return "Hello World!" + + +@app.route("/version") +def hello(): + return json_response( + { + "application": "Simple Api", + "version": 0.1 + } + ) + + +@app.route("/randoms") +def randoms(): + return json_response(rnd()) + + +@app.route("/hashname", methods=['POST']) +def hashname(): + content = request.json + name = content['name'] + return json_response(hashit(name)) + + +def json_response(data): + response = app.response_class( + response=json.dumps(data), + status=200, + mimetype='application/json' + ) + return response + + +def rnd(): + rnum = randint(1, 80) + rstr = ''.join(choice(ascii_letters) for _ in range(rnum)) + jsrnum = { + "number": rnum, + "string": rstr + } + return jsrnum + + +def hashit(name): + jshash = { + "name": name, + "hash": hash(name.encode('utf-8')) + } + return jshash + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=80) diff --git a/app/test/test_simple.py b/app/test/test_simple.py new file mode 100755 index 0000000..ea8d0ad --- /dev/null +++ b/app/test/test_simple.py @@ -0,0 +1,26 @@ +import json +from app.src.simple import app + + +class TestSimple: + + def setup(self): + self.app = app.test_client() + + def teardown(self): + pass + + def test_version(self): + resp = self.app.get('/version') + rjson = json.loads(resp.data) + assert rjson['version'] == 0.1 + + def test_random_numbers(self): + resp = self.app.get('/randoms') + rjson = json.loads(resp.data) + assert rjson['number'] <= 80 + + def test_random_string(self): + resp = self.app.get('/randoms') + rjson = json.loads(resp.data) + assert len(rjson['string']) <= 80