initial commit

This commit is contained in:
Greg Gauthier 2021-02-25 10:35:10 +00:00
commit f09a368ec9
6 changed files with 134 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.idea/
.pytest_cache/
*.pyc
*.iml
*.lock
*.log

21
Dockerfile Normal file
View File

@ -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" ]

15
Pipfile Normal file
View File

@ -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"

2
app/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
flask ~= 1.1.2
pytest ~= 6.2.2

64
app/src/simple.py Executable file
View File

@ -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)

26
app/test/test_simple.py Executable file
View File

@ -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