This repository was archived by the owner on Mar 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
59 lines (44 loc) · 2.13 KB
/
server.py
File metadata and controls
59 lines (44 loc) · 2.13 KB
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
# ---------------------------------------------------------------------------------------------------------------------
# API Wars
# server: routes handling
# v 1.0
# ---------------------------------------------------------------------------------------------------------------------
from flask import Flask, render_template, redirect, request, jsonify
from modules import api, constants as c, data_controller as dc, utilities as util
app = Flask(__name__)
app.secret_key = '#I\'ll be back!:D' # encrypt session variables
# ---------------------------------------------------- main route -----------------------------------------------------
@app.route('/')
def index():
""" Shows starter page. """
return redirect('/planets/1')
@app.route('/<subject>/<int:page_number>')
def subject_page(subject, page_number):
""" Shows a page listing the data specified in the subject variable. """
subject_data = dc.data_get(subject, page_number)
button_data = dc.button_data_get(subject, subject_data)
if subject == c.SUBJECT.PEOPLE:
subject_data = dc.data_change_url_to_name(subject_data, c.KEY.People.HOMEWORLD)
return render_template(
'index.html',
subjects_list=c.SUBJECT_ORDER,
subject_name=subject,
subject_data=subject_data,
button_data=button_data,
column_names=dc.column_names_get(subject),
pages_number=util.pagination_number_get(subject),
page_active=page_number
)
# ------------------------------------------- api request & response routes -------------------------------------------
@app.route('/api', methods=['POST'])
def api_data():
""" Receives and responds to the client's request. """
response = api.data_get(request.get_json())
return jsonify(response)
# ----------------------------------------------------- main code -----------------------------------------------------
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=5000,
debug=True
)