Browse Source

Change default retention to 1 day, make reverse proxy config option

master
Burathar 4 years ago
parent
commit
0f669fe371
  1. 1
      .flaskenv
  2. 4
      .flaskenv_example
  3. 3
      .gitignore
  4. 4
      app/forms.py
  5. 30
      app/routes.py
  6. 1
      config.py

1
.flaskenv

@ -1 +0,0 @@ @@ -1 +0,0 @@
FLASK_APP=linkshortener.py

4
.flaskenv_example

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
FLASK_APP=linkshortener.py
FLASK_ENV=production
#FLASK_ENV=development
REVERSE_PROXY=1

3
.gitignore vendored

@ -141,4 +141,5 @@ cython_debug/ @@ -141,4 +141,5 @@ cython_debug/
app.db
.vscode/
logs/
logs/
.flaskenv

4
app/forms.py

@ -4,6 +4,6 @@ from wtforms.validators import DataRequired, NumberRange, Required @@ -4,6 +4,6 @@ from wtforms.validators import DataRequired, NumberRange, Required
class UrlForm(FlaskForm):
url = StringField('Url', validators=[DataRequired()])
retention = IntegerField('Retention', default=5, validators = [NumberRange(min=0, max=100)])
retention_type = SelectField('Retention', choices = [ 'Minute', 'Hour', 'Day', 'Time'], default=2, validators = [Required()])
retention = IntegerField('Retention', default=1, validators = [NumberRange(min=0, max=100)])
retention_type = SelectField('Retention', choices = [(1, 'Minute'), (2, 'Hour'), (3, 'Day'), (4, 'Time')], default=3, validators = [Required()])
submit = SubmitField('Shorten Url')

30
app/routes.py

@ -21,8 +21,8 @@ def index(): @@ -21,8 +21,8 @@ def index():
if hash is None:
return redirect(url_for('index'))
death = calcDeath(form.retention.data, form.retention_type.data)
view_counter = None if form.retention_type.data != "Time" else form.retention.data
death = calcDeath(form.retention.data, int(form.retention_type.data))
view_counter = None if form.retention_type.data != 4 else form.retention.data
if death is None and view_counter is None:
app.logger.warning("Neither death nor view_counter was recieved for url")
flash('Please specify a retention time')
@ -32,7 +32,7 @@ def index(): @@ -32,7 +32,7 @@ def index():
db.session.add(url)
db.session.commit()
death = None if url.death is None else url.death.strftime('%Y-%m-%d %H:%M:%S')
app.logger.info(f"{request.environ['HTTP_X_FORWARDED_FOR']} created hash '{url.hash}' for '{url.url}'. Death: {death}, View Counter: {url.view_counter}")
app.logger.info(f"{getClientIp()} created hash '{url.hash}' for '{url.url}'. Death: {death}, View Counter: {url.view_counter}")
link = url_for("resolve_hash", hash=hash, _external = True)
flash(Markup(f'Your url is shortend to <a href="{link}">{link}</a>'))
return redirect(url_for('index'))
@ -48,14 +48,20 @@ def getHash(): @@ -48,14 +48,20 @@ def getHash():
flash('Failed generating a unique hash. Please try again.')
return None
def getClientIp():
if app.config['REVERSE_PROXY'] == 0:
return request.remote_addr
if app.config['REVERSE_PROXY'] == 1:
return request.environ['HTTP_X_FORWARDED_FOR']
def calcDeath(retention, retention_type):
if retention_type == 'Minute':
if retention_type == 1:
return datetime.now() + timedelta(minutes=retention)
if retention_type == 'Hour':
if retention_type == 2:
return datetime.now() + timedelta(hours=retention)
if retention_type == 'Day':
if retention_type == 3:
return datetime.now() + timedelta(days=retention)
if retention_type == 'Time':
if retention_type == 4:
return None
app.logger.error(f"Retention_type out of range: '{retention_type}'")
@ -66,11 +72,11 @@ def resolve_hash(hash): @@ -66,11 +72,11 @@ def resolve_hash(hash):
return redirect(url_for('index'))
url = Url.query.filter_by(hash=hash).first_or_404()
if check_url(url) is None:
if url.view_counter is not None:
url.view_counter -= 1
db.session.commit()
if check_url(url) is not None: abort(404)
if url.view_counter is not None:
url.view_counter -= 1
db.session.commit()
countermessage = "" if url.view_counter is None else f". View counter was lowered to {url.view_counter}"
app.logger.info(f"{request.environ['HTTP_X_FORWARDED_FOR']} requested hash '{url.hash}' which resolved to '{url.url}'{countermessage}")
app.logger.info(f"{getClientIp()} requested hash '{url.hash}' which resolved to '{url.url}'{countermessage}")
return redirect(url.url, 301)

1
config.py

@ -14,3 +14,4 @@ class Config(object): @@ -14,3 +14,4 @@ class Config(object):
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
ADMINS = ['your-email@example.com']
REVERSE_PROXY = os.environ.get('REVERSE_PROXY') or 0

Loading…
Cancel
Save