From f52d29f74483be543b8620d950eca622cf9df977 Mon Sep 17 00:00:00 2001 From: jakobsn Date: Wed, 2 Oct 2019 21:44:55 +0200 Subject: [PATCH] Launches webserver with nginx, wsgi and webpy. Next step is connecting to database --- docker-compose.yml | 23 +++++++ mysql/Dockerfile | 10 +++ {static => mysql}/sql/init.sql | 5 +- src/Dockerfile | 21 ++++++ src/app/main.py | 32 +++++++++ requirements.txt => src/app/requirements.txt | 1 - .../images => src/app/static}/friends.jpeg | Bin src/app/static/stylesheet.css | 3 + src/app/templates/index.html | 24 +++++++ src/entrypoint.sh | 27 ++++++++ static/styles/stylesheet.css | 3 - vulnapp.py | 64 ------------------ 12 files changed, 141 insertions(+), 72 deletions(-) create mode 100644 docker-compose.yml create mode 100644 mysql/Dockerfile rename {static => mysql}/sql/init.sql (76%) create mode 100644 src/Dockerfile create mode 100644 src/app/main.py rename requirements.txt => src/app/requirements.txt (97%) rename {static/images => src/app/static}/friends.jpeg (100%) create mode 100644 src/app/static/stylesheet.css create mode 100644 src/app/templates/index.html create mode 100644 src/entrypoint.sh delete mode 100644 static/styles/stylesheet.css delete mode 100644 vulnapp.py diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..31c98fb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: "2" +services: + + + db: + ports: + - "3306:3306" + build: + context: ./mysql + dockerfile: Dockerfile + + + app: + build: + context: ./src + dockerfile: Dockerfile + + links: + - db + ports: + - "8080:8080" + - "443:443" + diff --git a/mysql/Dockerfile b/mysql/Dockerfile new file mode 100644 index 0000000..486a081 --- /dev/null +++ b/mysql/Dockerfile @@ -0,0 +1,10 @@ +FROM mysql:5.7.15 + +MAINTAINER me + +ENV MYSQL_DATABASE=db \ + MYSQL_ROOT_PASSWORD=root + +ADD /sql/init.sql /docker-entrypoint-initdb.d + +EXPOSE 3306 diff --git a/static/sql/init.sql b/mysql/sql/init.sql similarity index 76% rename from static/sql/init.sql rename to mysql/sql/init.sql index 2faa50a..06c3a45 100644 --- a/static/sql/init.sql +++ b/mysql/sql/init.sql @@ -1,7 +1,4 @@ -create database db; - -use db CREATE TABLE `db`.`users` ( `userid` INT NOT NULL, @@ -10,7 +7,7 @@ CREATE TABLE `db`.`users` ( PRIMARY KEY (`userid`)); insert into users values (0, "admin", "password"); -insert into users values (0, "bernt", "inge"); +insert into users values (1, "bernt", "inge"); GRANT ALL PRIVILEGES ON db.* TO 'root'@'%'; diff --git a/src/Dockerfile b/src/Dockerfile new file mode 100644 index 0000000..a90d2ef --- /dev/null +++ b/src/Dockerfile @@ -0,0 +1,21 @@ +FROM tiangolo/uwsgi-nginx:python3.7 + +ENV LISTEN_PORT 8080 +EXPOSE 8080 + +COPY ./app /app +WORKDIR /app + +ENV PYTHONPATH=/app + +RUN pip install --no-cache-dir -r requirements.txt + +# Move the base entrypoint to reuse it +RUN mv /entrypoint.sh /uwsgi-nginx-entrypoint.sh +# Copy the entrypoint that will generate Nginx additional configs +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] + +CMD ["/start.sh"] diff --git a/src/app/main.py b/src/app/main.py new file mode 100644 index 0000000..636355e --- /dev/null +++ b/src/app/main.py @@ -0,0 +1,32 @@ +import web +from web import form + + +urls = ( + '/', 'index' +) + +# Initialize application using the web py framework +app = web.application(urls, globals()) + +# Get html templates +render = web.template.render('templates/') + +login_form = form.Form( + form.Textbox("username", description="Username"), + form.Password("password", description="Password"), + form.Button("submit", type="submit", description="Login"), +) + +class index(): + + + def GET(self): + #friends = db.select('users') + return render.index(login_form) + + +if __name__ == "__main__": + app.run() + +application = app.wsgifunc() diff --git a/requirements.txt b/src/app/requirements.txt similarity index 97% rename from requirements.txt rename to src/app/requirements.txt index 4695e76..c8ba3f5 100644 --- a/requirements.txt +++ b/src/app/requirements.txt @@ -1,3 +1,2 @@ web.py==0.40 mysql-connector==2.2.9 - diff --git a/static/images/friends.jpeg b/src/app/static/friends.jpeg similarity index 100% rename from static/images/friends.jpeg rename to src/app/static/friends.jpeg diff --git a/src/app/static/stylesheet.css b/src/app/static/stylesheet.css new file mode 100644 index 0000000..3704e43 --- /dev/null +++ b/src/app/static/stylesheet.css @@ -0,0 +1,3 @@ +body { + background-color: gray +} \ No newline at end of file diff --git a/src/app/templates/index.html b/src/app/templates/index.html new file mode 100644 index 0000000..bd2742a --- /dev/null +++ b/src/app/templates/index.html @@ -0,0 +1,24 @@ +$def with (login_form) + + + Friends + + + + + + + +

Friends

+ + Friends + +

Login, or become a friend!

+ +
+ $:login_form.render() +
+ + + + \ No newline at end of file diff --git a/src/entrypoint.sh b/src/entrypoint.sh new file mode 100644 index 0000000..5052949 --- /dev/null +++ b/src/entrypoint.sh @@ -0,0 +1,27 @@ +#! /usr/bin/env bash +set -e + +/uwsgi-nginx-entrypoint.sh + +# Get the URL for static files from the environment variable +USE_STATIC_URL=${STATIC_URL:-'/static'} +# Get the absolute path of the static files from the environment variable +USE_STATIC_PATH=${STATIC_PATH:-'/app/static'} +# Get the listen port for Nginx, default to 8080 +USE_LISTEN_PORT=${LISTEN_PORT:-8080} + +content_server='server {\n' +content_server=$content_server" listen ${USE_LISTEN_PORT};\n" +content_server=$content_server' location / {\n' +content_server=$content_server' include uwsgi_params;\n' +content_server=$content_server' uwsgi_pass unix:///tmp/uwsgi.sock;\n' +content_server=$content_server' }\n' +content_server=$content_server" location $USE_STATIC_URL {\n" +content_server=$content_server" alias $USE_STATIC_PATH;\n" +content_server=$content_server' }\n' +content_server=$content_server'}\n' + +# Save generated server /etc/nginx/conf.d/nginx.conf +printf "$content_server" > /etc/nginx/conf.d/nginx.conf + +exec "$@" \ No newline at end of file diff --git a/static/styles/stylesheet.css b/static/styles/stylesheet.css deleted file mode 100644 index 4316b68..0000000 --- a/static/styles/stylesheet.css +++ /dev/null @@ -1,3 +0,0 @@ -body { - background-color: whitesmoke -} \ No newline at end of file diff --git a/vulnapp.py b/vulnapp.py deleted file mode 100644 index 0be0de8..0000000 --- a/vulnapp.py +++ /dev/null @@ -1,64 +0,0 @@ -import web -from web import form - -# Turn of debug because sessions doesnt support it -web.config.debug = False - -# Define routes -urls = ( - '/', 'application', - "/logout", "logout", -) - -# Initialize application using the web py framework -app = web.application(urls, globals()) - -# Enable sessions -session = web.session.Session(app, web.session.DiskStore("sessions"), initializer={"username": None}) - -# Get html templates -render = web.template.render('templates/') -render._add_global(session, 'session') - - -# Connect to database -db = web.database( - dbn="mysql", - host='127.0.0.1', - port=3306, - user='root', - #pw='password', - db='db' -) - -class application(): - - login_form = form.Form( - form.Textbox("username", description="Username"), - form.Password("password", description="Password"), - form.Button("submit", type="submit", description="Login"), - ) - - def GET(self): - friends = db.select('users') - return render.index(self.login_form, friends) - - def POST(self): - friends = db.select('users') - data = web.input() - print("name:", data.username) - for row in friends: - print(row) - print(row.username) - if data.username == row.username and data.password == row.password: - friends = db.select('users') - session.username = data.username - return render.index(self.login_form, friends) - -class logout: - def GET(self): - session.kill() - return "Logged out" - -if __name__ == "__main__": - app.run()