|
| 1 | +import bottle |
1 | 2 | import os |
2 | 3 | import re |
3 | | -import rollbar |
4 | | -import rollbar.contrib.flask |
5 | | -from flask import Flask, render_template, Response |
6 | | -from flask import got_request_exception |
7 | | -from werkzeug.exceptions import NotFound |
8 | | - |
9 | | - |
10 | | -app = Flask(__name__) |
11 | | -MIN_PAGE_NAME_LENGTH = 2 |
12 | | - |
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 | | - |
22 | | -@app.route("/<string:page>/") |
23 | | -def show_page(page): |
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") |
| 4 | +from bottle import route, template |
| 5 | +from rollbar.contrib.bottle import RollbarBottleReporter |
| 6 | + |
| 7 | + |
| 8 | +TEMPLATE_STRING = """ |
| 9 | +<html> |
| 10 | + <head> |
| 11 | + <title>Full Stack Python Web App</title> |
| 12 | + </head> |
| 13 | + <body> |
| 14 | + <h1>{{ h1 }}</h1> |
| 15 | + </body> |
| 16 | +</html> |
| 17 | +""" |
| 18 | + |
| 19 | +MIN_MSG_LENGTH = 2 |
| 20 | +ROLLBAR_SECRET = os.environ.get("ROLLBAR_SECRET") |
| 21 | + |
| 22 | +rb_monitor = RollbarBottleReporter(access_token=ROLLBAR_SECRET, |
| 23 | + environment='production') |
| 24 | +bottle.install(rb_monitor) |
| 25 | + |
| 26 | + |
| 27 | +@route("/<msg>/") |
| 28 | +def show_message(msg): |
| 29 | + """Display a message if the msg value is greater than 2 characters |
| 30 | + in the path. |
| 31 | + """ |
| 32 | + valid_length = len(msg) >= MIN_MSG_LENGTH |
| 33 | + valid_name = re.match('^[a-z\-]+$', msg.lower()) is not None |
| 34 | + if valid_length and valid_name: |
| 35 | + return template(TEMPLATE_STRING, h1=msg) |
| 36 | + else: |
| 37 | + error_msg = "Sorry, only alpha characters and hyphens allowed." |
| 38 | + raise Exception(error_msg) |
35 | 39 |
|
36 | 40 |
|
37 | 41 | if __name__ == "__main__": |
38 | | - app.run(debug=True) |
| 42 | + bottle.run(host='localhost', port=8080) |
0 commit comments