Skip to content

Commit b55a5a3

Browse files
committed
improved readibility and removed unused code
1 parent a20492b commit b55a5a3

File tree

9 files changed

+41
-61
lines changed

9 files changed

+41
-61
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It includes but is not limited to the following -
66
* Complete PostgreSQL database support with sample db, model and dummy data examples included in the project.
77
* Reverse proxy using nginx.
88
* RedisQueue with Celery for asynchronous process
9-
9+
* There are two branches: master and frontend_backend_db_docker. Use the latter for a simpler skeleton code.
1010
<p>All components created from Docker images that expand on the respective official images from Docker Hub. Docker-compose file binds them together </p>
1111

1212
## Working
@@ -17,10 +17,9 @@ Prerequisites [here](https://github.com/angular/angular-cli#prerequisites).
1717

1818
## How to run
1919
- Clone this repository
20-
There are two branches- master and frontend_backend_db_docker. use -> git checkout branch_name
2120
- Then navigate to project_dir and execute following commands:
2221
- `docker-compose build`
23-
- `docker-compose up`
22+
- `docker-compose up` Note: use `npm install` in frontend directory for the first time
2423
- _OR_ just run one command: `docker-compose -f docker-compose.yml up --build`
2524

2625
- Open Browser and type following URL:
@@ -44,6 +43,7 @@ There are two branches- master and frontend_backend_db_docker. use -> git checko
4443
- The project structure supports multiple environments defined in setting.py
4544
- Edit environment variables in .env and .env_docker
4645
- ENV variables are loaded through setting.py
46+
- (Angular) This is a development setup; For Prod use nginx server to host the contents of dist folder (`ng build --prod`)
4747
- Some helpful commands are provided in Makefile
4848
- Eg. make backend-access
4949
- docker-compose-local.yml has just postgresql and redis

backend/app/celery/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from celery import Celery
22

3+
34
def create_celery(app_name=__name__):
45
return Celery(app_name, broker='redis://localhost:6379/0')
56

7+
68
def init_celery(celery, app):
79
celery.conf.update(app.config)
8-
# celery.config_from_object(app.config)
910
TaskBase = celery.Task
1011

1112
class ContextTask(TaskBase):

backend/app/celery/celery_worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66

77
@celery.task()
8-
def add_dummy_blogs(count, duration):
8+
def add_dummy_blogs(count):
99
for i in range(0, count):
1010
blog_service.save(Blog("A blog from Celery", "A content from Celery"))
1111
return "Task Complete"
1212

13+
1314
app = create_app(env='celery')
1415
CORS(app, origins='*')
15-
# app.app_context().push()
1616

1717

1818
# Run Using

backend/app/models/blog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class Blog(db.Model):
1010
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
1111
title = db.Column(db.String(500), nullable=False)
1212
description = db.Column(db.Text, nullable=False)
13-
created_date = db.Column(db.DateTime, nullable=False,)
13+
created_date = db.Column(db.DateTime, nullable=False, )
14+
1415
# password_hash = db.Column(db.String(128)
1516

1617
def __init__(self, title, description):
@@ -23,13 +24,12 @@ def as_dict(self):
2324
return {c.key: getattr(self, c.key)
2425
for c in inspect(self).mapper.column_attrs}
2526

26-
#another alternate to using as_dict
27+
# another alternate to using as_dict
2728
# @property
2829
# def serialize(self):
2930
# return {
3031
# 'title': self.title,
3132
# 'description': self.description,
32-
# 'created_date': self.reated_date,
33+
# 'created_date': self.created_date,
3334
# 'id': self.id,
3435
# }
35-

backend/app/services/blog_service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66

77
class BlogService(SQLAlchemyService):
8-
__model__ = Blog
9-
__db__ = db
8+
model = Blog
9+
db = db
10+
11+
def __init__(self):
12+
SQLAlchemyService.__init__(self, self.db, self.model)
1013

1114
def update(self, id, title, desc):
1215
to_update = self.__model__.query.get(id)

backend/app/services/sql_alchemy_service.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,38 @@
33

44

55
class SQLAlchemyService:
6-
__db__ = None
7-
__model__ = None
86

7+
def __init__(self, db, model):
8+
self.db = db
9+
self.model = model
10+
11+
# Optional: validation before insert in db
912
# def _isinstance(self, obj, raise_error=True):
1013
# rv = isinstance(obj, self.__model__)
1114
# if not rv and raise_error:
1215
# raise ValueError('%s is not of type %s' % (obj, self.__model__))
1316
# return rv
17+
1418
def all(self):
15-
data = self.__model__.query.all()
19+
data = self.model.query.all()
1620
data_dict = [row.as_dict() for row in data]
1721
return jsonify(data_dict)
1822

1923
def get(self, id):
20-
data = self.__model__.query.get(id)
24+
data = self.model.query.get(id)
2125
return jsonify(data.as_dict())
2226

2327
def get_all(self, *ids):
24-
return self.__model__.query.filter(self.__model__.id.in_(ids)).all()
28+
return self.model.query.filter(self.model.id.in_(ids)).all()
2529

2630
def save(self, obj):
27-
# self._isinstance(obj)
28-
self.__db__.session.add(obj)
29-
self.__db__.session.commit()
30-
# return jsonify(obj.as_dict())
31+
# self._isinstance(obj) # validation
32+
self.db.session.add(obj)
33+
self.db.session.commit()
3134
return 1
3235

3336
def delete(self, id):
34-
to_delete = self.__model__.query.get(id)
35-
self.__db__.session.delete(to_delete)
36-
self.__db__.session.commit()
37+
to_delete = self.model.query.get(id)
38+
self.db.session.delete(to_delete)
39+
self.db.session.commit()
3740
return jsonify(to_delete.as_dict())
38-
39-

backend/app/views/views.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from flask_restful import Resource, reqparse, abort
1+
from flask_restful import Resource, abort
22
from flask import request
33
from app.models.blog import Blog
44
from app.services.blog_service import BlogService
55

66
blog_service = BlogService()
77

8+
89
def register_view(api):
910
api.add_resource(BlogListApi, '/api/blogs')
1011
api.add_resource(BlogApi, '/api/blogs/<int:id>')
@@ -42,36 +43,10 @@ def put(self, id):
4243
def delete(self, id):
4344
return blog_service.delete(id)
4445

45-
# def __init__(self):
46-
# validation example
47-
# self.reqparse = reqparse.RequestParser()
48-
# self.reqparse.add_argument('title', type = str, location = 'json', required = True,
49-
# help = 'No title provided')
50-
# self.reqparse.add_argument('description', type = str, location = 'json', required = True,
51-
# help = 'No description provided')
52-
# self.reqparse.add_argument('blog_id', type = int, location = 'json')
53-
5446

5547
class DummyBlogs(Resource):
5648
def post(self):
5749
num = int(request.data.decode("utf-8"))
5850
from app.celery.celery_worker import add_dummy_blogs
59-
add_dummy_blogs.apply_async(args=[num, 10], countdown=10)
51+
add_dummy_blogs.apply_async(args=[num], countdown=10)
6052
return success_response()
61-
62-
# Routes approach to create api
63-
# @app.route('/')
64-
# @app.route('/index')
65-
# def index():
66-
# user = {'username': 'Saurabh'}
67-
# posts = [
68-
# {
69-
# 'author': {'username': 'John'},
70-
# 'body': 'Beautiful day in Portland!'
71-
# },
72-
# {
73-
# 'author': {'username': 'Susan'},
74-
# 'body': 'The Avengers movie was so cool!'
75-
# }
76-
# ]
77-
# return render_template('index.html', title='Home', user=user, posts=posts)

frontend/Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ RUN apk update && apk add --no-cache make git
33

44
WORKDIR /frontend
55

6-
COPY package.json package-lock.json /frontend/
7-
86
RUN npm i npm@latest -g && \
9-
npm install -g @angular/cli@7.3.9
7+
npm install -g @angular/cli@8.2.14
108

11-
RUN chown -R node /frontend
9+
## (Optional) To copy everything from frontend to server. Since we are using volume mount (to support live reload) its not needed
10+
#COPY . /frontend
11+
#RUN npm install
1212

13-
WORKDIR /frontend
13+
RUN chown -R node /frontend
1414

1515
USER node
1616

17-
RUN npm install
17+

frontend/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function (config) {
2626
colors: true,
2727
logLevel: config.LOG_INFO,
2828
autoWatch: true,
29-
browsers: ['Firefox'],
29+
browsers: ['Chrome'],
3030
singleRun: false,
3131
restartOnFileChange: true
3232
});

0 commit comments

Comments
 (0)