Skip to content

Commit b6f9c69

Browse files
authored
Intial Commit
1 parent 9e6e4c0 commit b6f9c69

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.6.7-alpine3.8
2+
RUN apk add --update alpine-sdk
3+
COPY . /app
4+
WORKDIR /app
5+
RUN pip install --no-cache-dir -r requirements.txt
6+
EXPOSE 5000
7+
CMD python ./api.py

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
# rest-api
2-
Simple Rest API
1+
## Routes
2+
3+
GET
4+
curl http://localhost:5000/todos
5+
curl http://localhost:5000/todos/todo1
6+
7+
CREATE
8+
curl http://localhost:5000/todos -d "task=something new" -X POST -v
9+
10+
UPDATE
11+
curl http://localhost:5000/todos/todo3 -d "task=something different" -X PUT -v
12+
13+
DELETE
14+
curl http://localhost:5000/todos/todo2 -X DELETE -v
15+
16+
## Docker Build
17+
18+
docker build -t todoapp:latest .
19+
docker run -d --name mytodoapp -p 8080:5000 todoapp:latest

api.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from flask import Flask
2+
from flask_restful import reqparse, abort, Api, Resource
3+
4+
app = Flask(__name__)
5+
api = Api(app)
6+
7+
TODOS = {
8+
'todo1': {'task': 'Learn Docker'},
9+
'todo2': {'task': 'Learn k8s'},
10+
'todo3': {'task': 'Learn AppMesh'},
11+
}
12+
13+
14+
def abort_if_todo_doesnt_exist(todo_id):
15+
if todo_id not in TODOS:
16+
abort(404, message="Todo {} doesn't exist".format(todo_id))
17+
18+
parser = reqparse.RequestParser()
19+
parser.add_argument('task')
20+
21+
22+
# Todo
23+
# shows a single todo item and lets you delete a todo item
24+
class Todo(Resource):
25+
def get(self, todo_id):
26+
abort_if_todo_doesnt_exist(todo_id)
27+
return TODOS[todo_id]
28+
29+
def delete(self, todo_id):
30+
abort_if_todo_doesnt_exist(todo_id)
31+
del TODOS[todo_id]
32+
return '', 204
33+
34+
def put(self, todo_id):
35+
args = parser.parse_args()
36+
task = {'task': args['task']}
37+
TODOS[todo_id] = task
38+
return task, 201
39+
40+
41+
# TodoList
42+
# shows a list of all todos, and lets you POST to add new tasks
43+
class TodoList(Resource):
44+
def get(self):
45+
return TODOS
46+
47+
def post(self):
48+
args = parser.parse_args()
49+
todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1
50+
todo_id = 'todo%i' % todo_id
51+
TODOS[todo_id] = {'task': args['task']}
52+
return TODOS[todo_id], 201
53+
54+
##
55+
## Actually setup the Api resource routing here
56+
##
57+
api.add_resource(TodoList, '/todos')
58+
api.add_resource(Todo, '/todos/<todo_id>')
59+
60+
@app.route("/")
61+
def index():
62+
return "Welcome to TODO App!"
63+
64+
65+
if __name__ == '__main__':
66+
app.run(host ='0.0.0.0', port = 5000, debug=True)

requirements.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
aniso8601==8.0.0
2+
click==7.1.2
3+
Flask==1.1.2
4+
Flask-RESTful==0.3.8
5+
itsdangerous==1.1.0
6+
Jinja2==2.11.2
7+
MarkupSafe==1.1.1
8+
pytz==2020.1
9+
six==1.15.0
10+
Werkzeug==1.0.1

0 commit comments

Comments
 (0)