import chdb
import config
import handlers
import utils

import flask
import flask_sslify
from flask.ext.compress import Compress

import os
import urllib
import urlparse
import traceback
import logging.handlers

# Cache duration for snippets.
# Since each page contains a link to the next one, even when no category is
# selected, we risk users getting trapped circling among their cached pages
# sometimes. We do depend on caching for prefetching to work, but let's do
# it for only a short period to be safe.
# An alternative would be to never cache when no category is selected UNLESS
# when prefetching, but that's a bit more complex.
CACHE_DURATION_SNIPPET = 30

app = flask.Flask(__name__)
Compress(app)
debug = 'DEBUG' in os.environ
if not debug:
    flask_sslify.SSLify(app, permanent = True)

@app.route('/')
@handlers.validate_lang_code
def index(lang_code):
    pass # nothing to do but validate lang_code

app.add_url_rule('/<lang_code>', view_func = handlers.citation_hunt)
app.add_url_rule('/<lang_code>/stats.html', view_func = handlers.stats)
app.after_request(handlers.log_request)
app.add_url_rule('/<lang_code>/search/category',
    view_func = handlers.search_category)
app.add_url_rule('/<lang_code>/fixed', view_func = handlers.fixed)

if not debug:
    log_dir = config.get_global_config().log_dir
    utils.mkdir_p(log_dir)
    log_file = os.path.join(log_dir, 'ch.log')
    log_handler = logging.handlers.RotatingFileHandler(
        log_file, maxBytes = 1024 * 1024, encoding = 'utf-8')
    log_handler.setLevel(logging.INFO if not debug else logging.DEBUG)
    log_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [%(pathname)s:%(lineno)d]'))
    app.logger.addHandler(log_handler)
    print 'writing server logs to %s' % log_file

@app.route('/<lang_code>/redirect')
@handlers.validate_lang_code
def redirect(lang_code):
    to = urllib.unquote(flask.request.args.get('to', ''))
    cfg = config.get_localized_config(lang_code)
    return flask.redirect(
        urlparse.urljoin('https://' + cfg.wikipedia_domain, to))

@app.after_request
def add_cache_header(response):
    if response.status_code == 302:
        response.cache_control.no_cache = True
        response.cache_control.no_store = True
        return response

    if response.status_code != 200:
        return response

    response.cache_control.public = True
    if response.cache_control.max_age is not None:
        return response

    response.cache_control.max_age = CACHE_DURATION_SNIPPET
    return response

@app.errorhandler(404)
def page_not_found(e):
    if hasattr(flask.g, '_cfg'):
        cfg = flask.g._cfg
    else:
        cfg = config.get_localized_config('en')
    return flask.render_template(
        '404.html', config = cfg), 404

@app.errorhandler(500)
def internal_server_error(e):
    app.logger.error(traceback.format_exc())
    response = flask.Response()
    response.status_code = 500
    handlers.log_request(response)
    return '<h1>Internal Error</h1><p>Sorry :(</p>', 500

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host = '0.0.0.0', port = port, debug = debug)

@@ -1510,79 +1510,8 @@
 8')%0A
-    log_handler.setLevel(logging.INFO if not debug else logging.DEBUG)%0A
     
@@ -1627,24 +1627,63 @@
 ineno)d%5D'))%0A
+    log_handler.setLevel(logging.INFO)%0A
     app.logg
@@ -1709,16 +1709,54 @@
 andler)%0A
+    app.logger.setLevel(logging.INFO)%0A
     prin
@@ -1797,16 +1797,103 @@
 g_file%0A%0A
+@app.before_first_request%0Adef log_hello():%0A    app.logger.info('logging says hello!')%0A%0A
 @app.rou

Make sure we log INFO messages in production mode.