diff --git a/src/Dockerfile b/src/Dockerfile index db33cf3..218f989 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -24,5 +24,5 @@ ENTRYPOINT ["/entrypoint.sh"] # Allow waiting script to be executed RUN chmod +x ./wait-for-it.sh -# Hold the webserver from launching until the database is ready. Default timeout = 30s -CMD ["./wait-for-it.sh", "-h", "10.5.0.5", "db:3306", "--", "/start.sh"] +# Hold the webserver from launching until the database is ready with 50s timeout +CMD ["./wait-for-it.sh", "-t", "50", "-h", "10.5.0.5", "db:3306", "--", "/start.sh"] diff --git a/src/app/forms.py b/src/app/forms.py new file mode 100644 index 0000000..5baab1e --- /dev/null +++ b/src/app/forms.py @@ -0,0 +1,15 @@ +from web import form + +# Define the login form +login_form = form.Form( + form.Textbox("username", description="Username"), + form.Password("password", description="Password"), + form.Button("Log In", type="submit", description="Login"), +) + +# Define the register form +register_form = form.Form( + form.Textbox("username", description="Username"), + form.Password("password", description="Password"), + form.Button("Register", type="submit", description="Register"), +) \ No newline at end of file diff --git a/src/app/main.py b/src/app/main.py index 5bfade4..5c20663 100644 --- a/src/app/main.py +++ b/src/app/main.py @@ -1,111 +1,7 @@ -import web -from web import form -import mysql.connector - -# Define application routes -urls = ( - '/', 'index', - '/logout', 'logout', - '/register', 'register', -) - -# Access datavase using mysql connector package -db = mysql.connector.connect( - user='root', - password='root', - host='10.5.0.5', - database='db' -) - -# Initialize application using the web py framework -app = web.application(urls, globals()) - -# Get html templates -render = web.template.render('templates/') - -# Workaround to use sessions with reloader (debugger) http://webpy.org/cookbook/session_with_reloader -if web.config.get('_session') is None: - session = web.session.Session(app, web.session.DiskStore("sessions"), initializer={"username": None}) - web.config._session = session -else: - session = web.config._session - -# Add session to global variables -render._add_global(session, 'session') - - -class index(): - - # Define the login form - login_form = form.Form( - form.Textbox("username", description="Username"), - form.Password("password", description="Password"), - form.Button("Log In", type="submit", description="Login"), - ) - - # Get main page - def GET(self): - # Show other registered users if the user is logged in - if session.username: - cursor = db.cursor() - query = ("SELECT userid, username from users") - cursor.execute(query) - friends = cursor.fetchall() - cursor.close() - else: - friends = [[],[]] - return render.index(self.login_form, friends) - - # Log In - def POST(self): - # Validate login credential with database query - cursor = db.cursor() - query = ("SELECT userid, username from users where username = (%s) and password = (%s)") - data = web.input() - cursor.execute(query, (data.username, data.password)) - friends = cursor.fetchall() - # If there is a matching user/password in the database the user is logged in - if len(friends) == 1: - query = ("SELECT userid, username from users") - cursor.execute(query) - friends = cursor.fetchall() - session.username = data.username - cursor.close() - return render.index(self.login_form, friends) - cursor.close() - - -class register: - - # Define the register form - register_form = form.Form( - form.Textbox("username", description="Username"), - form.Password("password", description="Password"), - form.Button("Register", type="submit", description="Register"), - ) - - # Get the registration form - def GET(self): - return render.register(self.register_form) - - # Register new user in database - def POST(self): - cursor = db.cursor() - query = ("INSERT INTO users VALUES (NULL, (%s), (%s))") - data = web.input() - cursor.execute(query, (data.username, data.password)) - cursor.close() - return render.register(self.register_form) - - -class logout: - - # Kill session - def GET(self): - session.kill() - return "Logged Out" +from views import app if __name__ == "__main__": app.run() +# Use webpy module to create a wsgi function application = app.wsgifunc() diff --git a/src/app/views.py b/src/app/views.py new file mode 100644 index 0000000..6a97f9c --- /dev/null +++ b/src/app/views.py @@ -0,0 +1,91 @@ +import web +import mysql.connector +from forms import login_form, register_form + +# Define application routes +urls = ( + '/', 'index', + '/logout', 'logout', + '/register', 'register', +) + +# Access datavase using mysql connector package +db = mysql.connector.connect( + user='root', + password='root', + host='10.5.0.5', + database='db' +) + +# Initialize application using the web py framework +app = web.application(urls, globals()) + +# Get html templates +render = web.template.render('templates/') + +# Workaround to use sessions with reloader (debugger) http://webpy.org/cookbook/session_with_reloader +if web.config.get('_session') is None: + session = web.session.Session(app, web.session.DiskStore("sessions"), initializer={"username": None}) + web.config._session = session +else: + session = web.config._session + +# Add session to global variables +render._add_global(session, 'session') + +class index(): + + # Get main page + def GET(self): + # Show other registered users if the user is logged in + if session.username: + cursor = db.cursor() + query = ("SELECT userid, username from users") + cursor.execute(query) + friends = cursor.fetchall() + cursor.close() + else: + friends = [[],[]] + return render.index(login_form, friends) + + # Log In + def POST(self): + # Validate login credential with database query + cursor = db.cursor() + query = ("SELECT userid, username from users where username = (%s) and password = (%s)") + data = web.input() + cursor.execute(query, (data.username, data.password)) + friends = cursor.fetchall() + # If there is a matching user/password in the database the user is logged in + if len(friends) == 1: + query = ("SELECT userid, username from users") + cursor.execute(query) + friends = cursor.fetchall() + session.username = data.username + cursor.close() + return render.index(login_form, friends) + cursor.close() + + +class register: + + # Get the registration form + def GET(self): + return render.register(register_form) + + # Register new user in database + def POST(self): + cursor = db.cursor() + query = ("INSERT INTO users VALUES (NULL, (%s), (%s))") + data = web.input() + cursor.execute(query, (data.username, data.password)) + cursor.close() + return render.register(register_form) + + +class logout: + + # Kill session + def GET(self): + session.kill() + return "Logged Out" diff --git a/src/app/wait-for-it.sh b/src/app/wait-for-it.sh index 928d4e5..607a7d6 100755 --- a/src/app/wait-for-it.sh +++ b/src/app/wait-for-it.sh @@ -1,29 +1,6 @@ #!/usr/bin/env bash # Use this script to test if a given TCP host/port are available -""" -The MIT License (MIT) -Copyright (c) 2016 Giles Hall - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the \"Software\"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -""" - WAITFORIT_cmdname=${0##*/} echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } @@ -159,7 +136,7 @@ if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then usage fi -WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-30} +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}