This commit is contained in:
nutzer26
2022-12-13 17:55:43 +01:00
parent a037410306
commit bed22c6720
12 changed files with 289 additions and 0 deletions

5
carpool/.dockerignore Normal file
View File

@@ -0,0 +1,5 @@
docker-compose.yml
*.sql
.dockerignore
*.json
Dockerfile

12
carpool/Dockerfile Normal file
View File

@@ -0,0 +1,12 @@
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
ARG DEBIAN_FRONTEND=noninteractive
ARG DEBCONF_NOWARNINGS="yes"
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install --disable-pip-version-check -r requirements.txt
COPY . .
ENV FLASK_APP=python_rest
EXPOSE 5000
STOPSIGNAL SIGINT
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

6
carpool/car_1.json Normal file
View File

@@ -0,0 +1,6 @@
{
"license_plate": "BO-PR-72",
"car_type": "mini clubman SD",
"fuel": "diesel",
"number_of_seats": "5"
}

6
carpool/car_2.json Normal file
View File

@@ -0,0 +1,6 @@
{
"license_plate": "BO-PR-46",
"car_type": "bmw",
"fuel": "benzin",
"number_of_seats": "5"
}

View File

@@ -0,0 +1,44 @@
version: "3.3"
services:
traefik:
image: "traefik:v2.8"
container_name: "traefik"
command:
#- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
rest:
build: .
image: bee42/carpool:0.1.0
labels:
- "traefik.enable=true"
- "traefik.http.routers.car.rule=Host(`car.localhost`)"
- "traefik.http.routers.car.entrypoints=web"
- "traefik.http.services.car.loadbalancer.server.port=5000"
environment:
FLASK_APP: python_rest
POSTGRES_DB: esentricar
POSTGRES_USER: cnbc
POSTGRES_PASSWORD: postgres
POSTGRES_HOST: postgres
postgres:
image: postgres
container_name: postgres
restart: always
environment:
POSTGRES_DB: postgres_db
POSTGRES_USER: cnbc
POSTGRES_PASSWORD: postgres
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- postgres:/var/lib/postgresql/data
- ./esentricar.sql:/docker-entrypoint-initdb.d/esentricar.sql
volumes:
postgres:

11
carpool/esentricar.sql Normal file
View File

@@ -0,0 +1,11 @@
CREATE DATABASE esentricar;
\c esentricar;
CREATE TABLE cars (
car_id SERIAL PRIMARY KEY,
license_plate varchar(30) NOT NULL,
car_type varchar(30) NOT NULL,
fuel varchar(30) NOT NULL,
number_of_seats integer NOT NULL
);
CREATE UNIQUE INDEX cars_license_plate
ON cars (license_plate);

1
carpool/index.env Normal file
View File

@@ -0,0 +1 @@
POSTGRES_PASSWORD=postgres

105
carpool/python_rest.py Normal file
View File

@@ -0,0 +1,105 @@
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow, fields
from flask_cors import CORS
import os
# Init app
app = Flask(__name__)
user = os.environ.get('POSTGRES_USER')
db = os.environ.get('POSTGRES_DB')
passwd = os.environ.get('POSTGRES_PASSWORD')
host = os.environ.get('POSTGRES_HOST')
#enable CORS
CORS(app)
# connect to already existing and running Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://' + user + ':' + passwd +'@' + host + ':5432/' + db
# not important but otherwise we get a warning every time
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#Init db
db = SQLAlchemy(app)
#Init ma
ma = Marshmallow(app)
# Pool Car Class/Model
class Pool_Car(db.Model):
#define table, in our case already existing
__tablename__ = 'cars'
car_id = db.Column(db.Integer, primary_key=True)
license_plate = db.Column(db.String(30), unique=True)
car_type = db.Column(db.String(20))
fuel = db.Column(db.String(20))
number_of_seats =db.Column(db.Integer)
# set class attributes
def __init__(self, license_plate, car_type, fuel, number_of_seats):
self.license_plate = license_plate
self.car_type = car_type
self.fuel = fuel
self.number_of_seats = number_of_seats
class Pool_CarSchema(ma.Schema):
class Meta:
fields = ('car_id','license_plate','car_type','fuel','number_of_seats')
# Init schema
pool_car_schema = Pool_CarSchema()
pool_cars_schema = Pool_CarSchema(many=True)
# Create a Pool_Car
@app.route('/car', methods=['POST'])
def add_pool_car():
# Get request data as json
car_entity = request.get_json()
license_plate= car_entity.get('license_plate')
car_type = car_entity.get('car_type')
fuel = car_entity.get('fuel')
number_of_seats = car_entity.get('number_of_seats')
new_pool_car = Pool_Car(license_plate, car_type, fuel, number_of_seats)
db.session.add(new_pool_car)
db.session.commit()
return pool_car_schema.jsonify(new_pool_car)
# Get car_id,license_plate,car_type of all products of the table
@app.route('/car', methods=['GET'])
def get_pool_cars():
all_pool_cars = Pool_Car.query.all()
result = pool_cars_schema.dump(all_pool_cars)
car_list= []
for item in result:
car_details = { "car_id":None, "license_plate":None, "car_type":None}
car_details['car_id'] = item['car_id']
car_details['license_plate'] = item['license_plate']
car_details['car_type'] = item['car_type']
car_list.append(car_details)
return jsonify(car_list)
# Get Single Products
@app.route('/car/<car_id>', methods=['GET'])
def get_pool_car(car_id):
pool_car = Pool_Car.query.get(car_id)
return pool_car_schema.jsonify(pool_car)
# Delete Product
@app.route('/car/<car_id>', methods=['DELETE'])
def delete_pool_car(car_id):
pool_car = Pool_Car.query.get(car_id)
db.session.delete(pool_car)
db.session.commit()
return pool_car_schema.jsonify(pool_car)
if __name__ == '__main__':
app.run(debug=True) #port='5002'

11
carpool/requirements.txt Normal file
View File

@@ -0,0 +1,11 @@
Flask==1.1.1
Flask-Cors==3.0.8
flask-marshmallow==0.10.1
Flask-SQLAlchemy==2.4.1
werkzeug==2.0.2
marshmallow==3.2.2
SQLAlchemy==1.3.11
marshmallow-sqlalchemy==0.19.0
psycopg2-binary==2.8.4
itsdangerous==2.0.1
Jinja2==3.0.3