Skip to content

Instantly share code, notes, and snippets.

@brantz
Created December 5, 2012 13:53
Show Gist options
  • Save brantz/4215663 to your computer and use it in GitHub Desktop.
Save brantz/4215663 to your computer and use it in GitHub Desktop.
the bottle server
#encoding: utf-8
from os import getenv
from error_mailer import ErrorMailer
from bottle import route, run, template, request, BaseResponse
import sys
import ConfigParser
import logging
class Server():
# check if python version is >= 2.7
if sys.version_info[:2] != (2,7):
print "Error: This needs to be run at least with python 2.7"
exit(1)
# set environment
SERVICE_ENV = getenv('SERVICE_ENV') or 'development'
#set auto reload for develpment
auto_reload = (SERVICE_ENV == 'development' or
config.getboolean(SERVICE_ENV, 'auto_reload'))
# initialize logging
logging.basicConfig(
filename = 'logs/error_mailer.log',
level = logging.ERROR,
format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p'
)
server_log = logging.getLogger('server_log')
# read config file and set properties
try:
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.read('config/server.cfg')
service_host = config.get(SERVICE_ENV, 'service_host') or '127.0.0.1'
service_port = config.getint(SERVICE_ENV, 'service_port') or 9003
except Exception as e:
server_log.error('missing config file or config option: ' + e.message)
@route('/mailer', method='POST')
def mailer():
if request.forms.subject and request.forms.content:
mail_recipient = request.forms.to or ''
mail_recipient_cc = request.forms.cc or ''
mail_attachment = request.forms.attachment or ''
ErrorMailer(
request.forms.subject,
request.forms.content,
recipient = mail_recipient,
recipient_cc = mail_recipient_cc,
attachment = mail_attachment
).send()
else:
mandatory_params = set(['content', 'subject'])
request_params = set()
for form_data in request.forms:
request_params.add(form_data)
logging.error(
'invalid / insufficient parameters in request from: '
+ request.environ.get('REMOTE_ADDR')
+ "\t parameter(s) missing: "
+ ", ".join(mandatory_params.difference(request_params))
)
if __name__ == '__main__':
run(host=service_host, port=service_port, reloader=auto_reload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment