import json
import bottle
import logging
from bottle import Jinja2Template, jinja2_view, static_file, response, Bottle, request
import functools
from cheroot import wsgi
import os
# this is somehow easier than actually using sockets..
import sh

logger = logging.Logger('linktree')

Jinja2Template.settings = {'autoescape': True}
view = functools.partial(jinja2_view, template_lookup=["templates"])

app = Bottle()
app.config['TEMPLATES_AUTO_RELOAD'] = True

@app.hook('after_request')
def enable_cors():
    """
    You need to add some headers to each request.
    Don't use the wildcard '*' for Access-Control-Allow-Origin in production.
    """
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'PUT, GET, POST, DELETE, OPTIONS'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

@app.route('/')
@view('index.html')
def online():
    return {"links": {"De stembus is geopend, stem!": "https://liefvoorjou.nl/stem", "Op zoek naar de was-droogcombinatie van je dromen?": "https://m.iele.nl/liefnwi"}}

@app.route('/static/:path#.+#', name='static')
def static(path):
    return static_file(path, root='static')
