-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
77 lines (65 loc) · 2.8 KB
/
server.py
File metadata and controls
77 lines (65 loc) · 2.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from collections import namedtuple
from flask import Flask, render_template, request
import argparse
import login as mylogin
import signup as mysignup
import configuration
import inmemorydb
parser = argparse.ArgumentParser(description="Server to provide backend for login application.")
parser.add_argument("--ip", type=str, default="127.0.0.1", help="IP on which server will listen")
parser.add_argument("--port", type=str, default="5000", help="POST on which server will listen")
parser.add_argument("--protocol", type=str, default="http", choices=["http"],
help="Protocol on which server will listen")
parser.add_argument("--logLevel", type=str, default="INFO", choices=["INFO", "DEBUG"], help="log level")
args = parser.parse_args()
if args.logLevel == "INFO":
print(args)
configuration.init_config(args)
inmemorydb.initilize_db()
protocol = args.protocol
serverIp = args.ip
serverPort = args.port
baseAddress = protocol + "://" + serverIp + ":" + serverPort + "/"
signupAddress = baseAddress + "signup"
loginAddress = baseAddress + "login"
configuration.set_config("signupAddress", signupAddress, overrite=True)
configuration.set_config("loginAddress", loginAddress, overrite=True)
returnData = namedtuple('returnData', 'message, backToUrl, caption')
app = Flask(__name__, static_url_path='/static')
@app.route('/', methods=["POST", "GET"])
def index():
return render_template("login.html", signupURL=signupAddress, loginURL=loginAddress)
@app.route('/login', methods=["POST", "GET"])
def login():
if request.method == "POST":
rtn_data: returnData = mylogin.login(request)
return render_template("response.html", message=rtn_data.message, backToURL=rtn_data.backToUrl,
buttonCaption=rtn_data.caption)
elif request.method == "GET":
return render_template("login.html", signupURL=signupAddress, loginURL=loginAddress)
@app.route('/signup', methods=["POST", "GET"])
def signup():
if request.method == "POST":
rtn_data: returnData = mysignup.signup(request)
return render_template("response.html", message=rtn_data.message, backToURL=rtn_data.backToUrl,
buttonCaption=rtn_data.caption)
elif request.method == "GET":
return render_template("register.html", signupURL=signupAddress, loginURL=loginAddress)
# @app.errorhandler(404)
# def not_found():
# """Page not found."""
# return make_response(render_template("signup.html"), 404)
#
#
# @app.errorhandler(400)
# def bad_request():
# """Bad request."""
# return make_response(render_template("signup.html"), 400)
#
#
# @app.errorhandler(500)
# def server_error():
# """Internal server error."""
# return make_response(render_template("signup.html"), 500)
if __name__ == '__main__':
app.run(host=serverIp, port=serverPort)