|
1 | 1 | import os
|
2 | 2 | import re
|
3 | 3 | import rollbar
|
4 |
| -from flask import Flask, render_template |
| 4 | +import rollbar.contrib.flask |
| 5 | +from flask import Flask, render_template, Response |
| 6 | +from flask import got_request_exception |
5 | 7 | from werkzeug.exceptions import NotFound
|
6 | 8 |
|
7 | 9 |
|
8 | 10 | app = Flask(__name__)
|
9 |
| -rollbar.init(os.environ.get('ROLLBAR_SECRET')) |
10 |
| -rollbar.report_message('Rollbar is configured correctly') |
11 |
| - |
12 | 11 | MIN_PAGE_NAME_LENGTH = 2
|
13 | 12 |
|
14 | 13 |
|
| 14 | +@app.before_first_request |
| 15 | +def add_monitoring(): |
| 16 | + rollbar.init(os.environ.get('ROLLBAR_SECRET')) |
| 17 | + ## delete the next line if you dont want this event anymore |
| 18 | + rollbar.report_message('Rollbar is configured correctly') |
| 19 | + got_request_exception.connect(rollbar.contrib.flask.report_exception, app) |
| 20 | + |
| 21 | + |
15 | 22 | @app.route("/<string:page>/")
|
16 | 23 | def show_page(page):
|
17 |
| - valid_length = len(page) >= MIN_PAGE_NAME_LENGTH |
18 |
| - valid_name = re.match('^[a-z]+$', page.lower()) is not None |
19 |
| - if valid_length and valid_name: |
20 |
| - return render_template("{}.html".format(page)) |
21 |
| - else: |
22 |
| - msg = "Sorry, couldn't find page with name {}".format(page) |
23 |
| - raise NotFound(msg) |
| 24 | + try: |
| 25 | + valid_length = len(page) >= MIN_PAGE_NAME_LENGTH |
| 26 | + valid_name = re.match('^[a-z]+$', page.lower()) is not None |
| 27 | + if valid_length and valid_name: |
| 28 | + return render_template("{}.html".format(page)) |
| 29 | + else: |
| 30 | + msg = "Sorry, couldn't find page with name {}".format(page) |
| 31 | + raise NotFound(msg) |
| 32 | + except: |
| 33 | + rollbar.report_exc_info() |
| 34 | + return Response("404 Not Found") |
24 | 35 |
|
25 | 36 |
|
26 | 37 | if __name__ == "__main__":
|
|
0 commit comments