-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
87 lines (70 loc) · 2.52 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
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
import logging
from flask import Flask, request
from logging import Formatter, FileHandler
from models import db
from models.Notes import Note
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///api.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.url_map.strict_slashes = False
db.init_app(app)
db.app = app
with app.app_context():
db.create_all()
@app.teardown_request
def shutdown_session(exception=None):
db.session.remove()
#----------------------------------------------------------------------------#
# Routes.
#----------------------------------------------------------------------------#
@app.route('/', methods=['GET'])
def home():
return "Hello, World!"
@app.route('/health', methods=['GET'])
def api_health():
return {
"status": "OK"
}
@app.route('/notes', methods=['GET', 'POST'])
def api_notes():
if request.method == 'POST':
data = request.get_json()
n = Note(note=data['note'])
db.session.add(n)
db.session.commit()
return {"status":"saved"}
else:
return Note.fs_get_delete_put_post()
@app.route('/note/<note_id>', methods=['GET', 'DELETE', 'PUT'])
def get_delete_put_note(note_id):
return Note.fs_get_delete_put_post(note_id)
#----------------------------------------------------------------------------#
# Error handlers.
#----------------------------------------------------------------------------#
@app.errorhandler(500)
def internal_error(error):
db.session.rollback()
return "Oh No, 500 Server Error!", 500
@app.errorhandler(404)
def not_found_error(error):
return "404, Nothing to see here!", 404
if not app.debug:
file_handler = FileHandler('error.log')
file_handler.setFormatter(
Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
)
app.logger.setLevel(logging.INFO)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('errors')
#----------------------------------------------------------------------------#
# Launch.
#----------------------------------------------------------------------------#
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)