-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (52 loc) · 2.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import secrets
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from flask import Flask, request, session, jsonify, render_template, redirect, url_for, flash
app = Flask(__name__)
app.config['SECRET_KEY'] = secrets.token_urlsafe(16)
limiter = Limiter(get_remote_address, app=app)
# Session Management
@app.before_request
def sessionChecks():
session.permanent = True
app.permanent_session_lifetime = 60 * 60
if 'username' not in session and request.endpoint not in ['login', 'favicon', 'static', 'api']:
return render_template('login.html')
# Error Handling
# In case of using a custom error page, uncomment the following code
# @app.errorhandler(429) # Too Many Requests
# def ratelimit_handler(e):
# return render_template("429.html"), 429
# @app.errorhandler(404) # Page Not Found
# def page_not_found(e):
# return render_template("404.html"), 404
@app.route("/favicon.ico")
def favicon():
return redirect(url_for("static", filename="favicon.ico"), code=302)
@app.route('/')
@limiter.exempt
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
@limiter.limit("5 per minute") # Limit to 5 login attempts per minute
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username == 'admin' and password == 'admin':
session['username'] = username
flash("Login Successful", "success")
return redirect(url_for('index'))
else:
flash("Invalid Credentials", "error")
return render_template('login.html')
# API Endpoints
@app.route('/api', methods=['GET', 'POST'])
def api():
if request.method == 'GET':
return jsonify({'status': 'success', 'message': 'GET request received'})
elif request.method == 'POST':
data = request.get_json()
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True)