diff --git a/main.py b/main.py index dedfe13..61bb192 100755 --- a/main.py +++ b/main.py @@ -5,15 +5,38 @@ redirect = bottle.redirect import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart -import os +import os # for environ vars import sys # to print ot stderr -import re -import pymongo +import re # to match our template system +import pymongo # database from dotenv import load_dotenv -import random, string +import random, string # for tokens import html # for sanitization +##################################################### Bottle stuff ############################################$ + +# The exception that is thrown when an argument is missing +class MissingParameterException (Exception): + pass + + +class StripPathMiddleware(object): + ''' + Get that slash out of the request + ''' + def __init__(self, a): + self.a = a + def __call__(self, e, h): + e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') + return self.a(e, h) + + +app = application = bottle.Bottle() + + +##################################################### Configuration ############################################$ + # Load file from .env file. load_dotenv(os.path.dirname(__file__) + '.env') @@ -21,12 +44,6 @@ token_chars = string.ascii_lowercase+string.ascii_uppercase+string.digits token_len = 50 -# The exception that is thrown when an argument is missing -class MissingParameterException (Exception): - pass - -app = application = bottle.Bottle() - # Get address and port from env listen_address = os.environ['LISTEN_ADDRESS'] if 'LISTEN_ADDRESS' in os.environ else '0.0.0.0' listen_port = os.environ['LISTEN_PORT'] if 'LISTEN_PORT' in os.environ else 8080 @@ -97,14 +114,12 @@ form_regex = '\{\{(\w+)(\|\w+)?\}\}' @app.post('/fail') -@app.post('/fail/') def fail (): a = 2/0 print('lol, failed', file=sys.stderr) return 'failed' @app.post('/submit') -@app.post('/submit/') def submission (): # Getting subject if 'token' in request.forms: @@ -212,7 +227,6 @@ def login(request): ##################################################### Forms ############################################$ @app.post('/form') -@app.post('/form/') def create_form (): # Getting subject template if 'subject' in request.forms: @@ -255,7 +269,6 @@ def create_form (): return 'Créé : ' + token @app.post('/form/list') -@app.post('/form/list/') def list_forms (): user = login(request) if user['_privilege'] == 0: @@ -269,7 +282,6 @@ def list_forms (): @app.delete('/form/') -@app.delete('/form//') def delete_form(token): # If admin or form owner user = login(request) @@ -296,7 +308,6 @@ def delete_form(token): ##################################################### Users ############################################$ @app.post('/user/list') -@app.post('/user/list/') def list_users (): user = login(request) if user['_privilege'] > 0: @@ -305,7 +316,6 @@ def list_users (): return bottle.template("list.tpl", data=mongodb_database['users'].find()) @app.put('/user/') -@app.put('/user//') def create_user (username): user = login(request) if user['_privilege'] > 0: @@ -323,7 +333,6 @@ def create_user (username): @app.delete('/user/') -@app.delete('/user//') def delete_user (username): user = login(request) if user['_privilege'] > 0: @@ -340,17 +349,7 @@ def delete_user (username): return 'Supprimé ' + username -##################################################### Bottle stuff ############################################$ - -class StripPathMiddleware(object): - ''' - Get that slash out of the request - ''' - def __init__(self, a): - self.a = a - def __call__(self, e, h): - e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') - return self.a(e, h) - if __name__ == '__main__': bottle.run(app=StripPathMiddleware(app), host=listen_address, port=listen_port, debug=True) +else: + prod_app = StripPathMiddleware(app)