Skip to content

Update #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# using ubuntu LTS version
FROM ubuntu:20.04 AS builder-image
FROM ubuntu:22.04 AS builder-image

# avoid stuck build due to user prompt
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install --no-install-recommends -y python3.9 python3.9-dev python3.9-venv python3-pip python3-wheel build-essential && \
RUN apt-get update && apt-get install --no-install-recommends -y python3.10 python3.10-dev python3.10-venv python3-pip python3-wheel build-essential && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# create and activate virtual environment
# using final folder name to avoid path issues with packages
RUN python3.9 -m venv /home/myuser/venv
RUN python3 -m venv /home/myuser/venv
ENV PATH="/home/myuser/venv/bin:$PATH"

# install requirements
COPY requirements.txt .
RUN pip3 install --no-cache-dir wheel
RUN pip3 install --no-cache-dir -r requirements.txt

FROM ubuntu:20.04 AS runner-image
RUN apt-get update && apt-get install --no-install-recommends -y python3.9 python3-venv && \
FROM ubuntu:22.04 AS runner-image
RUN apt-get update && apt-get install --no-install-recommends -y python3.10 python3-venv && \
apt-get clean && rm -rf /var/lib/apt/lists/*

RUN useradd --create-home myuser
COPY --from=builder-image /home/myuser/venv /home/myuser/venv

USER myuser
RUN mkdir /home/myuser/code
WORKDIR /home/myuser/code/app
COPY . .

RUN chown -R myuser:myuser /home/myuser
USER myuser
EXPOSE 5000

# make sure all messages always reach console
Expand All @@ -37,7 +37,7 @@ ENV PYTHONUNBUFFERED=1
# activate virtual environment
ENV VIRTUAL_ENV=/home/myuser/venv
ENV PATH="/home/myuser/venv/bin:$PATH"
ENV FLASK_APP=app:app.py
ENV FLASK_APP=app
ENV FLASK_ENV=development
ENV FLASK_DEBUG=1
# /dev/shm is mapped to shared memory and should be used for gunicorn heartbeat
Expand Down
4 changes: 2 additions & 2 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# -*- coding: utf-8 -*-
# standard python imports

mssql = {'host': 'dbhost',
mssql = {'host': 'database',
'user': 'dbuser',
'passwd': 'dbPwd',
'db': 'db'}

postgresql = {'host': '0.0.0.0',
postgresql = {'host': 'database',
'user': 'postgres',
'passwd': 'magical_password',
'db': 'db'}
Expand Down
20 changes: 10 additions & 10 deletions app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Flask==2.0.1
Flask-RESTful==0.3.9
Flask-SQLAlchemy==2.5.1
Jinja2==3.0.1
Flask-JWT-Extended==4.2.1
python-dateutil==2.8.1
pytz==2021.1
SQLAlchemy==1.4.19
Werkzeug==2.0.1
Flask==2.3.2
Flask-RESTful==0.3.10
Flask-SQLAlchemy==3.0.5
Jinja2==3.1.2
Flask-JWT-Extended==4.5.2
python-dateutil==2.8.2
pytz==2023.3
SQLAlchemy==2.0.19
Werkzeug==2.3.6
psycopg2-binary
rich
rich==13.4.2
18 changes: 4 additions & 14 deletions app/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# -*- coding: utf-8 -*-
# standard python imports

from flask_restful import Resource, reqparse
from flask import jsonify
from flask_restful import Resource
from flask import jsonify, request
from flask_jwt_extended import create_access_token, jwt_required
from flask_jwt_extended import current_user
from app.models.user import UserModel
Expand All @@ -16,14 +16,9 @@ class User(Resource):
def __init__(self):
self.logger = create_logger()

parser = reqparse.RequestParser() # only allow price changes, no name changes allowed
parser.add_argument('username', type=str, required=True,
help='This field cannot be left blank')
parser.add_argument('password', type=str, required=True,
help='This field cannot be left blank')

def post(self):
data = User.parser.parse_args()
data = request.get_json(force=True)
username = data['username']
password = data['password']

Expand All @@ -49,14 +44,9 @@ class UserRegister(Resource):
def __init__(self):
self.logger = create_logger()

parser = reqparse.RequestParser() # only allow price changes, no name changes allowed
parser.add_argument('username', type=str, required=True,
help='This field cannot be left blank')
parser.add_argument('password', type=str, required=True,
help='This field cannot be left blank')

def post(self):
data = UserRegister.parser.parse_args()
data = request.get_json(force=True)

if UserModel.find_by_username(data['username']):
return {'message': 'UserModel has already been created, aborting.'}, 400
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '3.7'
services:

app:
hostname: app
build: ./app
restart: always
ports:
Expand All @@ -10,6 +11,7 @@ services:
- database

database:
hostname: database
image: postgres:latest # use latest official postgres version
env_file:
- postgres.env # configure postgres
Expand Down
15 changes: 10 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ on Udemy. https://www.udemy.com/course/rest-api-flask-and-python/learn/lecture/6

### Example endpoints
#### Add user
`curl -d "username=user1&password=abcd" -X POST http://localhost:5000/register`
`curl -X POST http://localhost:5000/register -H "Content-Type: application/json" -d '{"username": "luna", "password": "badgirl"}'`

#### Login
###### _`(Returns Auth Token)`_
`curl -d "username=user1&password=abcd" -X POST http://localhost:5000/user`
`curl -X POST http://localhost:5000/user -H "Content-Type: application/json" -d '{"username": "luna", "password": "badgirl"}'`

#### Add Item
### Grab token in variable
`export JWT=$(curl -s -X POST http://localhost:5000/user -H "Content-Type: application/json" -d @creds.json | jq .access_token)`

#### Add Store
###### _`(Replace with Auth Token)`_
`curl -XGET -d "store_id=1&price=2.309" \
-H "Authorization: Bearer paste_token_here http://localhost:5000/item/xyz`
`curl -X POST http://localhost:5000/store/xyz -H "Authorization: Bearer $JWT" -H "Content-Type: application/json" -H "Accepts: application/json" `

### Add Item
`curl -X POST http://localhost:5000/item/apple -H "Authorization: Bearer $JWT" -H "Content-Type: application/json" -H "Accepts: application/json" -d '{"store_id": 1, "price": "2.40"}'`