Bladeren bron

Restructure

https
jakobsn 6 jaren geleden
bovenliggende
commit
82ee4f2b61
5 gewijzigde bestanden met toevoegingen van 111 en 132 verwijderingen
  1. +2
    -2
      src/Dockerfile
  2. +15
    -0
      src/app/forms.py
  3. +2
    -106
      src/app/main.py
  4. +91
    -0
      src/app/views.py
  5. +1
    -24
      src/app/wait-for-it.sh

+ 2
- 2
src/Dockerfile Bestand weergeven

@@ -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"]

+ 15
- 0
src/app/forms.py Bestand weergeven

@@ -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"),
)

+ 2
- 106
src/app/main.py Bestand weergeven

@@ -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()

+ 91
- 0
src/app/views.py Bestand weergeven

@@ -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"

+ 1
- 24
src/app/wait-for-it.sh Bestand weergeven

@@ -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}


Laden…
Annuleren
Opslaan