-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·128 lines (103 loc) · 3.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
import logging
import logmatic
import os
import sys
from flask import Flask, request, jsonify, make_response
from flask_sqlalchemy import SQLAlchemy
from requestid import requestid, RequestIdFilter
log_level = getattr(logging, os.environ.get('APP_LOG_LEVEL', 'INFO'))
# Configure JSON filesystem log handler
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logmatic.JsonFormatter())
handler.addFilter(RequestIdFilter())
# Configure global logger
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(log_level)
# Create app and database session
app = Flask('flask-demo-app')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('APP_DATABASE_URI')
db = SQLAlchemy(app)
class Entry(db.Model):
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String(80), unique=False)
comment = db.Column(db.String(120), unique=False)
def as_dict(self):
return {
'id': self.id,
'description': self.description,
'comment': self.comment
}
@app.before_first_request
def setup():
# Configure application logger
app.logger.addHandler(handler)
app.logger.setLevel(log_level)
# Bootstrapping database schema
db.create_all()
@requestid
@app.route('/api/v1/entries', methods=['GET'])
def list_entries():
entries = list(map(lambda x: x.as_dict(), Entry.query.all()))
app.logger.info('Retrieved %d entries' % len(entries))
return make_response(jsonify(entries), 200)
@requestid
@app.route('/api/v1/entries/<id>', methods=['GET'])
def list_entry(id):
entry = Entry.query.get(id)
if entry:
app.logger.info('Entry found for id %s: %s' % (id, entry.as_dict()))
return make_response(jsonify(entry.as_dict()), 200)
else:
app.logger.info('Entry not found for id %s' % id)
return make_response(jsonify(''), 404)
@requestid
@app.route('/api/v1/entries', methods=['POST'])
def add_entry():
content = request.json
entry = Entry()
entry.description = content['description']
entry.comment = content['comment']
db.session.add(entry)
db.session.commit()
app.logger.info('Added entry: %s' % entry.as_dict())
return make_response(jsonify(entry.as_dict()), 200)
@requestid
@app.route('/api/v1/entries/<id>', methods=['POST'])
def update_entry(id):
content = request.json
content.pop('id', None)
db.session.query(Entry).filter_by(id=id).update(content)
entry = Entry.query.get(id)
if entry:
db.session.commit()
app.logger.info('Updated entry %s with data: %s' % (id, content))
return make_response(jsonify(entry.as_dict()), 200)
else:
app.logger.info('Entry not found for id %s' % id)
return make_response(jsonify(''), 404)
@requestid
@app.route('/api/v1/entries/<id>', methods=['DELETE'])
def remove_entry(id):
entry = Entry.query.get(id)
if entry:
db.session.delete(entry)
db.session.commit()
app.logger.info('Removed entry: %s', entry.as_dict())
return make_response(jsonify(entry.as_dict()), 200)
else:
app.logger.info('Entry not found for id %s' % id)
return make_response(jsonify(''), 404)
@app.route('/api/v1/status', methods=['GET'])
def status():
return make_response(jsonify('OK'), 200)
@requestid
@app.errorhandler(500)
def internal_error(exception):
app.logger.error(exception)
return make_response(jsonify(exception), 500)
if __name__ == "__main__":
host = os.environ.get('APP_HOST', '0.0.0.0')
port = os.environ.get('APP_PORT', 5000)
app.run(host=host, port=port)