Просмотр исходного кода

Launches webserver with nginx, wsgi and webpy. Next step is connecting to database

https
jakobsn 6 лет назад
Родитель
Сommit
f52d29f744
12 измененных файлов: 141 добавлений и 72 удалений
  1. +23
    -0
      docker-compose.yml
  2. +10
    -0
      mysql/Dockerfile
  3. +1
    -4
      mysql/sql/init.sql
  4. +21
    -0
      src/Dockerfile
  5. +32
    -0
      src/app/main.py
  6. +0
    -1
      src/app/requirements.txt
  7. +0
    -0
      src/app/static/friends.jpeg
  8. +3
    -0
      src/app/static/stylesheet.css
  9. +24
    -0
      src/app/templates/index.html
  10. +27
    -0
      src/entrypoint.sh
  11. +0
    -3
      static/styles/stylesheet.css
  12. +0
    -64
      vulnapp.py

+ 23
- 0
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"

+ 10
- 0
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

static/sql/init.sql → 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'@'%';

+ 21
- 0
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"]

+ 32
- 0
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()

requirements.txt → src/app/requirements.txt Просмотреть файл

@@ -1,3 +1,2 @@
web.py==0.40
mysql-connector==2.2.9


static/images/friends.jpeg → src/app/static/friends.jpeg Просмотреть файл


+ 3
- 0
src/app/static/stylesheet.css Просмотреть файл

@@ -0,0 +1,3 @@
body {
background-color: gray
}

+ 24
- 0
src/app/templates/index.html Просмотреть файл

@@ -0,0 +1,24 @@
$def with (login_form)

<head>
<title>Friends</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="static/stylesheet.css">

</head>

<body>

<h1>Friends</h1>

<img src="static/friends.jpeg" alt="Friends">

<h3>Login, or become a friend!</h3>

<form method="POST">
$:login_form.render()
</form>


</body>

+ 27
- 0
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 "$@"

+ 0
- 3
static/styles/stylesheet.css Просмотреть файл

@@ -1,3 +0,0 @@
body {
background-color: whitesmoke
}

+ 0
- 64
vulnapp.py Просмотреть файл

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

Загрузка…
Отмена
Сохранить