-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
138 lines (105 loc) · 4.98 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
from datetime import datetime
from flask import Flask
from database.mongo import Users
from database.settings import flaskSecret
from database.settings import uploadPath
from database.settings import WebServer
from routes.general import *
from routes.stations import *
from routes.users import *
from routes.admin import *
# Initialize flask app
app = Flask(__name__)
# Set up flask app config
app.secret_key = flaskSecret
app.config['UPLOAD_FOLDER'] = uploadPath
# Keep track of number of unique visitiors
uniqueVisitors = set()
@app.before_request
def check_user():
# handle and check if user is logged in
g.user = None
if 'user_id' in session:
user = Users.getUserByID(id=session['user_id'])
g.user = user
@app.before_request
def log_requests():
"""
Both log every request made and log all unique visitors by IP
Structure of log of all requests ->
{time} - {ip} - {user agent} - {route}
Structure of log of unique visitors ->
{time} - {ip} - {user agent}
"""
now = datetime.now()
ip = request.remote_addr
# Log - All
with open(now.strftime("logs/all_%Y_%m_%d.log"), "a") as f:
f.write("{} - {} - {} - {}\n".format(
now,
ip,
request.user_agent,
request.path
))
# Log - Unique Visitors
if not (ip in uniqueVisitors):
# If IP is not in our set, then proceed
uniqueVisitors.add(ip)
with open(now.strftime("logs/unique_%Y_%m_%d.log"), "a") as f:
f.write("{} - {} - {}\n".format(
now,
ip,
request.user_agent
))
# General
app.add_url_rule("/", 'index', index, methods=['GET'])
app.add_url_rule("/contact", 'contact_us', contact_us, methods=['GET'])
# Stations
app.add_url_rule("/map", 'map', map, methods=['GET'])
app.add_url_rule("/map/petrol", 'map_petrol', map_petrol, methods=['GET'])
app.add_url_rule("/map/diesel", 'map_diesel', map_diesel, methods=['GET'])
app.add_url_rule("/amount", 'amounts_no_id', amounts_no_id, methods=['GET'])
app.add_url_rule("/amount/<id>", 'amounts', amounts, methods=['GET'])
# Users
app.add_url_rule("/login", 'login', login, methods=['GET', 'POST'])
app.add_url_rule("/signup", 'signup', signup, methods=['GET', 'POST'])
app.add_url_rule("/logout", 'logout', logout, methods=['GET'])
app.add_url_rule("/dashboard", 'panel', panel, methods=['GET'])
app.add_url_rule("/edit/user", 'panel_edit_user',panel_edit_user, methods=['GET', 'POST'])
app.add_url_rule("/edit/station", 'panel_edit_station',panel_edit_station, methods=['GET', 'POST'])
app.add_url_rule("/add/station", 'add_new_station',add_new_station, methods=['GET', 'POST'])
# Admin
app.add_url_rule("/admin", 'admin_home', admin_panel, methods=['GET'])
app.add_url_rule("/admin/update", 'admin_update',admin_update, methods=['GET'])
app.add_url_rule("/admin/approve", 'admin_approve',admin_approve, methods=['GET', 'POST'])
app.add_url_rule("/admin/panel", 'admin_panel', admin_panel, methods=['GET'])
app.add_url_rule("/admin/panel/<category>", 'admin_panel_catergory',admin_panel_catergory, methods=['GET'])
app.add_url_rule("/admin/download/log/latest", 'admin_download_log_noargs',admin_download_log_noargs, methods=['GET'])
app.add_url_rule("/admin/download/log/latest/<logtype>",'admin_download_log', admin_download_log, methods=['GET'])
app.add_url_rule("/admin/download/log/old", 'admin_download_file_no_arg',admin_download_file_no_arg, methods=['GET'])
app.add_url_rule("/admin/download/log/old/<logfilename>",'admin_download_file', admin_download_file, methods=['GET'])
app.add_url_rule("/admin/delete/log/old", 'admin_delete_file_no_arg',admin_delete_file_no_arg, methods=['GET'])
app.add_url_rule("/admin/delete/log/old/<logfilename>",'admin_delete_file', admin_delete_file, methods=['GET'])
app.add_url_rule("/admin/delete/user/<uid>", 'admin_delete_user',admin_delete_user, methods=['GET'])
app.add_url_rule("/admin/delete/station/<sid>",'admin_delete_station', admin_delete_station, methods=['GET'])
app.add_url_rule("/admin/update/<what>", 'amdin_settings_change',amdin_settings_change, methods=['GET', 'POST'])
app.add_url_rule("/admin/pending/image", 'admin_stations_pending_image_no_args',admin_stations_pending_image_no_args, methods=['GET'])
app.add_url_rule("/admin/pending/image/<image>", 'admin_stations_pending_image',admin_stations_pending_image, methods=['GET'])
def runApp():
# create the logs folder if it doesnt exist
if not (os.path.isdir(os.path.join(os.getcwd(), "logs"))):
os.makedirs("logs")
# generate the map inititally before starting web app
makeMap()
# display web server stats
_host = f"{'localhost' if (WebServer.host == '0.0.0.0') or (WebServer.host == '127.0.0.1') else WebServer.host}:{WebServer.port}"
print(f"[*] The server is running on:\n\t-> http://{_host}/")
# start the flask app
app.run(
WebServer.host,
port=WebServer.port,
debug=WebServer.debug
)
if __name__ == "__main__":
runApp()