This commit is contained in:
2021-01-16 19:47:34 +01:00
commit 9a907c3a8e
8 changed files with 310 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__/
ENV

42
Dockerfile Normal file
View File

@ -0,0 +1,42 @@
FROM python:latest
LABEL Maintainer="Wolfgang Hottgenroth wolfgang.hottgenroth@icloud.com"
ARG APP_DIR="/opt/app"
ARG CONF_DIR="${APP_DIR}/config"
ENV DB_HOST="172.16.10.18"
ENV DB_NAME="hausverwaltung"
ENV DB_USER="hausverwaltung-ui"
ENV DB_PASS="test123"
RUN \
apt update && \
apt install -y libmariadbclient-dev && \
pip3 install mariadb && \
pip3 install connexion && \
pip3 install connexion[swagger-ui] && \
pip3 install uwsgi && \
pip3 install flask-cors
RUN \
mkdir -p ${APP_DIR} && \
mkdir -p ${CONF_DIR} && \
useradd -d ${APP_DIR} -u 1000 user
COPY *.py ${APP_DIR}/
COPY swagger.yaml ${APP_DIR}/
COPY server.ini ${CONF_DIR}/
USER 1000:1000
WORKDIR ${APP_DIR}
VOLUME ${CONF_DIR}
EXPOSE 5000
EXPOSE 9191
CMD [ "uwsgi", "./config/server.ini" ]

46
Objekte.py Normal file
View File

@ -0,0 +1,46 @@
from dbpool import getConnection
def get_objekte():
try:
dbh = getConnection()
objekte = []
cur = dbh.cursor()
cur.execute("SELECT id, shortname, flaeche FROM objekt")
for (id, shortname, flaeche) in cur:
objekte.append({
"id": id,
"shortname": shortname,
"flaeche": flaeche
})
return objekte
except Exception as err:
return str(err), 500
finally:
dbh.close()
def get_objekt(id=None):
try:
dbh = getConnection()
cur = dbh.cursor()
cur.execute("SELECT id, shortname, flaeche FROM objekt WHERE id = ?", (id,))
objekt = None
try:
(id, shortname, flaeche) = cur.next()
objekt = {
"id": id,
"shortname": shortname,
"flaeche": flaeche
}
except StopIteration:
return "Objekt not found", 404
try:
(id, shortname, flaeche) = cur.next()
return "More than one Objekt by that id ({}, {}, {})".format(id, shortname, flaeche), 500
except:
pass
return objekt
except Exception as err:
return str(err), 500
finally:
dbh.close()

67
Wohnungen.py Normal file
View File

@ -0,0 +1,67 @@
from dbpool import getConnection
def get_wohnungen():
try:
dbh = getConnection()
wohnungen = []
cur = dbh.cursor()
cur.execute("""
SELECT w.id as id,
w.objekt as objekt_id,
w.shortname as wohnung,
w.flaeche as flaeche,
o.shortname as objekt
FROM wohnung w, objekt o
WHERE o.id = w.objekt
""")
for (id, objekt_id, wohnung, flaeche, objekt) in cur:
wohnungen.append({
"id": id,
"objekt_id": objekt_id,
"wohnung": wohnung,
"flaeche": flaeche,
"objekt": objekt
})
return wohnungen
except Exception as err:
return str(err), 500
finally:
dbh.close()
def get_wohnung(id=None):
try:
dbh = getConnection()
cur = dbh.cursor()
cur.execute("""
SELECT w.id as id,
w.objekt as objekt_id,
w.shortname as wohnung,
w.flaeche as flaeche,
o.shortname as objekt
FROM wohnung w, objekt o
WHERE o.id = w.objekt AND
w.id = ?
""", (id, ))
wohnung = None
try:
(id, objekt_id, wohnung_name, flaeche, objekt_name) = cur.next()
wohnung = {
"id": id,
"objekt_id": objekt_id,
"wohnung": wohnung_name,
"flaeche": flaeche,
"objekt": objekt_name
}
except StopIteration:
return "Wohnung not found", 404
try:
(id, objekt_id, wohnung_name, flaeche, objekt_name) = cur.next()
return "More than one Wohnung by that id ({}, {}, {}, {})".format(id, objekt_name, wohnung_name, flaeche), 500
except:
pass
return wohnung
except Exception as err:
return str(err), 500
finally:
dbh.close()

28
dbpool.py Normal file
View File

@ -0,0 +1,28 @@
import mariadb
import os
pool = None
def createConnectionPool():
global pool
try:
user = os.environ["DB_USER"]
password = os.environ["DB_PASS"]
host = os.environ["DB_HOST"]
database = os.environ["DB_NAME"]
except KeyError as err:
raise Exception("Database configuration variable {} not available".format(err))
pool = mariadb.ConnectionPool(
user = user,
password = password,
host = host,
database = database,
pool_name = 'hv-wep-app',
pool_size = 5
)
def getConnection():
global pool
return pool.get_connection()

7
server.ini Normal file
View File

@ -0,0 +1,7 @@
[uwsgi]
http = :5000
wsgi-file = server.py
processes = 4
threads = 2
stats = :9191

16
server.py Normal file
View File

@ -0,0 +1,16 @@
import connexion
from flask_cors import CORS
from dbpool import createConnectionPool
# prepare database connections
createConnectionPool()
# instantiate the webservice
app = connexion.App(__name__)
app.add_api('swagger.yaml')
# CORSify it - otherwise Angular won't accept it
CORS(app.app)
# provide the webservice application to uwsgi
application = app.app

102
swagger.yaml Normal file
View File

@ -0,0 +1,102 @@
swagger: '2.0'
info:
title: Hausverwaltung
version: "0.1"
paths:
/hv/objekte:
get:
tags: [ "Objekte" ]
operationId: Objekte.get_objekte
summary: Returns all Objekte
responses:
200:
description: Successful response.
schema:
type: array
items:
$ref: '#/definitions/Objekt'
404:
description: No Objekte available
500:
description: Some server error
/hv/objekt/{id}:
get:
tags: [ "Objekte" ]
operationId: Objekte.get_objekt
summary: Returns Objekt by id
parameters:
- name: id
in: path
type: integer
required: true
responses:
200:
description: Successful response.
schema:
$ref: '#/definitions/Objekt'
404:
description: Objekt not found
500:
description: Some server error
/hv/wohnungen:
get:
tags: [ "Wohnungen" ]
operationId: Wohnungen.get_wohnungen
summary: Returns all Wohnungen
responses:
200:
description: Successful response.
schema:
type: array
items:
$ref: '#/definitions/Wohnung'
404:
description: No Wohnung available
500:
description: Some server error
/hv/wohnung/{id}:
get:
tags: [ "Wohnungen" ]
operationId: Wohnungen.get_wohnung
summary: Returns Wohnung by id
parameters:
- name: id
in: path
type: integer
required: true
responses:
200:
description: Successful response.
schema:
$ref: '#/definitions/Wohnung'
404:
description: Wohnung not found
500:
description: Some server error
definitions:
Objekt:
description: Objekt type
type: object
properties:
id:
type: integer
shortname:
type: string
flaeche:
type: number
Wohnung:
description: Wohnung type
type: object
properties:
id:
type: integer
objekt_id:
type: integer
wohnung:
type: string
flaeche:
type: number
objekt:
type: string