Skip to content

Commit 5f08cf0

Browse files
committed
flask users service setup
0 parents  commit 5f08cf0

File tree

17 files changed

+288
-0
lines changed

17 files changed

+288
-0
lines changed

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__
2+
venv
3+
.env

‎dockerfile-compose-dev.yml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: '3.8'
2+
services:
3+
users:
4+
container_name: users
5+
build:
6+
context: ./services/users
7+
dockerfile: Dockerfile-dev
8+
volumes:
9+
- app:/usr/src/app
10+
ports:
11+
- 5001:5000
12+
environment:
13+
- FLASK_APP=project/__init__.py
14+
- FLASK_DEBUG=1
15+
- APP_SETTINGS=project.config.DevelopmentConfig
16+
- DATABASE_URL=postgresql://postgres:postgres@users-db:5432/users_dev
17+
- DATABASE_TEST_URL=postgresql://postgres:postgres@users-db:5432/users_test
18+
19+
depends_on:
20+
- users-db
21+
links:
22+
- users-db
23+
24+
users-db:
25+
container_name: users-db
26+
build:
27+
context: ./services/users/project/db
28+
dockerfile: Dockerfile
29+
ports:
30+
- 5435:5432
31+
environment:
32+
- POSTGRES_USER=postgres
33+
- POSTGRES_PASSWORD=postgres
34+
volumes:
35+
app:

‎services/users/.dockerignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Dockerfile-dev
2+
Dockerfile-prod

‎services/users/Dockerfile-dev‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
FROM python:3.8-alpine
2+
3+
# install environment dependencies
4+
# RUN apt-get update -yqq \
5+
# && apt-get install -yqq --no-install-recommends \
6+
# netcat \
7+
# && apt-get -q clean
8+
9+
# set working directory
10+
RUN mkdir -p /usr/src/app
11+
WORKDIR /usr/src/app
12+
13+
# add requirements.txt
14+
COPY ./requirements.txt /usr/src/app/requirements.txt
15+
16+
17+
# install packages
18+
RUN pip install -r requirements.txt
19+
20+
# add app
21+
COPY . /usr/src/app
22+
23+
# add entrypoint.sh
24+
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
25+
26+
# run server
27+
CMD python manage.py run -h 0.0.0.0
28+
29+
# CMD ["bash", "./entrypoint.sh"]

‎services/users/entrypoint.sh‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
echo "Waiting for postgres"
4+
5+
while ! nc -z users-db 5432; do
6+
sleep 0.1
7+
done
8+
9+
echo "PostgreSQL started"
10+
11+
python manage.py run -h 0.0.0.0

‎services/users/manage.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
from flask.cli import FlaskGroup
3+
from project import app, db
4+
5+
cli = FlaskGroup(app)
6+
7+
8+
@cli.command()
9+
def recreate_db():
10+
db.drop_all()
11+
db.create_all()
12+
db.session.com
13+
14+
15+
@cli.command()
16+
def test():
17+
"""Runs the tests without code coverage"""
18+
tests = unittest.TestLoader().discover("project/tests", pattern="test*.py")
19+
result = unittest.TextTestRunner(verbosity=2).run(tests)
20+
if result.wasSuccessful():
21+
return 0
22+
return 1
23+
24+
25+
if __name__ == "__main__":
26+
cli()

‎services/users/project/__init__.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
from flask import Flask, jsonify, Blueprint
3+
from flask_sqlalchemy import SQLAlchemy
4+
5+
app = Flask(__name__)
6+
7+
app_settings = os.getenv("APP_SETTINGS")
8+
app.config.from_object("project.config.DevelopmentConfig")
9+
10+
db = SQLAlchemy(app)
11+
12+
13+
# Application factory
14+
def create_app(script_info=None):
15+
# instantiate app
16+
app = Flask(__name__)
17+
18+
# set config
19+
app.settings = os.getenv("APP_SETTINGS")
20+
app.config.from_object(app_settings)
21+
22+
# set up extensions
23+
db.init_app(app)
24+
25+
# register blueprints
26+
from project.api.users import users_blueprint
27+
28+
app.register_blueprint(users_blueprint)
29+
30+
# shell context from flask cli
31+
app.shell_context_processor({"app": app, "db": db})
32+
33+
return app
34+
35+
36+
@app.route("/", methods=["GET"])
37+
def index():
38+
return jsonify({"1": "hallooo", "2": "yessss"})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from project import db
2+
3+
4+
class User(db.Model):
5+
__tablename__ = "users"
6+
7+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
8+
username = db.Column(db.String(128), unique=True, nullable=False)
9+
email = db.Column(db.String(120), unique=True, nullable=False)
10+
active = db.Column(db.Boolean(), default=True, nullable=False)
11+
12+
def __init__(self, username, email) -> None:
13+
self.username = username
14+
self.email = email
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask import Blueprint, jsonify
2+
3+
4+
users_blueprint = Blueprint("users", __name__)
5+
6+
7+
@users_blueprint.route("/users/ping", methods=["GET"])
8+
def ping():
9+
return jsonify({"status": "success", "message": "pong"})

‎services/users/project/config.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
4+
class BaseConfig:
5+
TESTING = False
6+
SQLALCHEMY_TRACK_MODIFICATIONS = False
7+
SECRET_KEY="very nice"
8+
9+
10+
class DevelopmentConfig(BaseConfig):
11+
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL")
12+
13+
14+
class TestingConfig(BaseConfig):
15+
TESTING = True
16+
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_TEST_URL")
17+
18+
19+
class ProductionConfig(BaseConfig):
20+
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL")

0 commit comments

Comments
 (0)