Skip to content

Commit 4361bfa

Browse files
committed
update code latest post
1 parent e9ff7ed commit 4361bfa

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ ENV/
9191
*.swp
9292

9393
monitor-flask-apps/.env
94+
monitor-python-bottle-apps/.env

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Posts and associated code:
77

88
|Post|Code Directory|Notes|
99
|---|---|---|
10+
|[How to Monitor Python Web Applications](https://www.fullstackpython.com/blog/blog/monitor-python-web-applications.html)|[monitor-python-bottle-apps](https://github.com/fullstackpython/blog-code-examples/tree/master/monitor-python-bottle-apps)||
1011
|[How to Provision Ubuntu 16.04 Linux Servers on Linode](https://www.fullstackpython.com/blog/provision-ubuntu-linux-servers-linode.html)|No code for this post.||
1112
|[Creating Bar Chart Visuals with Bokeh, Bottle and Python 3](https://www.fullstackpython.com/blog/python-bottle-bokeh-bar-charts.html)|[bar-charts-bokeh-bottle-python-3](https://github.com/fullstackpython/blog-code-examples/tree/master/bar-charts-bokeh-bottle-python-3)||
1213
|[How to Add Hosted Monitoring to Flask Web Applications](https://www.fullstackpython.com/blog/hosted-monitoring-flask-web-apps.html)|[monitor-flask-apps](https://github.com/fullstackpython/blog-code-examples/tree/master/monitor-flask-apps)||

monitor-python-bottle-apps/app.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
1+
import bottle
12
import os
23
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)
3539

3640

3741
if __name__ == "__main__":
38-
app.run(debug=True)
42+
bottle.run(host='localhost', port=8080)
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
blinker==1.4
2-
Flask==0.12.2
3-
rollbar==0.13.12
1+
bottle==0.12.13
2+
rollbar==0.13.13

0 commit comments

Comments
 (0)