Skip to content

Commit db16258

Browse files
committed
base init
1 parent 1615d9f commit db16258

File tree

17 files changed

+55
-147
lines changed

17 files changed

+55
-147
lines changed

.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.6.1
1+
3.6.4

.travis.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM python:3.6.1
1+
# base image
2+
FROM python:3.6.4-alpine
23

34
# set working directory
45
RUN mkdir -p /usr/src/app
@@ -8,4 +9,4 @@ WORKDIR /usr/src/app
89
ADD ./requirements.txt /usr/src/app/requirements.txt
910

1011
# install requirements
11-
RUN pip install -r requirements.txt
12+
RUN pip install -r requirements.txt

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
Copyright (c) 2017 Michael Herman
2+
Copyright (c) 2018 Michael Herman
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
55

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,3 @@ $ docker-compose up -d
1111
```
1212

1313
Open your browser to http://localhost:5001
14-
15-
### Example
16-
17-
![](app.png)

app.png

-156 KB
Binary file not shown.

docker-compose.yml

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
1-
version: '2.1'
1+
version: '3.5'
22

33
services:
44

55
web:
66
build: .
77
image: web
88
container_name: web
9-
environment:
10-
- APP_SETTINGS=project.server.config.DevelopmentConfig
119
ports:
1210
- '5001:5000'
13-
command: python manage.py runserver -h 0.0.0.0
11+
command: python manage.py run -h 0.0.0.0
1412
volumes:
1513
- .:/usr/src/app
16-
depends_on:
17-
- redis
18-
19-
worker:
20-
image: web
21-
container_name: worker
2214
environment:
15+
- FLASK_DEBUG=1
2316
- APP_SETTINGS=project.server.config.DevelopmentConfig
24-
command: python manage.py run_worker
25-
volumes:
26-
- .:/usr/src/app
27-
depends_on:
28-
- redis
29-
30-
redis:
31-
image: redis:3.2.11

manage.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
# manage.py
22

33

4-
import os
54
import unittest
65

7-
import redis
8-
from rq import Connection, Worker
9-
from flask_script import Manager
6+
from flask.cli import FlaskGroup
107

11-
from project.server import app
8+
from project.server import create_app
129

1310

14-
manager = Manager(app)
11+
app = create_app()
12+
cli = FlaskGroup(create_app=create_app)
1513

1614

17-
@manager.command
15+
@cli.command()
1816
def test():
1917
"""Runs the unit tests without test coverage."""
2018
tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
@@ -24,13 +22,5 @@ def test():
2422
return 1
2523

2624

27-
@manager.command
28-
def run_worker():
29-
redis_url = app.config['REDIS_URL']
30-
redis_connection = redis.from_url(redis_url)
31-
with Connection(redis_connection):
32-
worker = Worker(app.config['QUEUES'])
33-
worker.work()
34-
3525
if __name__ == '__main__':
36-
manager.run()
26+
cli()

project/client/static/main.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ $('.btn').on('click', function() {
1515
})
1616
.fail((err) => {
1717
console.log(err)
18-
})
19-
})
18+
});
19+
});
2020

2121
function getStatus(taskID) {
2222
$.ajax({
@@ -30,7 +30,7 @@ function getStatus(taskID) {
3030
<td>${res.data.task_status}</td>
3131
<td>${res.data.task_result}</td>
3232
</tr>`
33-
$('#tasks').prepend(html)
33+
$('#tasks').prepend(html);
3434
const taskStatus = res.data.task_status;
3535
if (taskStatus === 'finished' || taskStatus === 'failed') return false;
3636
setTimeout(function() {
@@ -39,6 +39,5 @@ function getStatus(taskID) {
3939
})
4040
.fail((err) => {
4141
console.log(err)
42-
})
42+
});
4343
}
44-

project/client/templates/_base.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<meta name="author" content="">
1010
<meta name="viewport" content="width=device-width,initial-scale=1">
1111
<!-- styles -->
12-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
12+
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
1313
<link href="{{url_for('static', filename='main.css')}}" rel="stylesheet" media="screen">
1414
{% block css %}{% endblock %}
1515
</head>
@@ -25,8 +25,8 @@
2525

2626
<!-- scripts -->
2727
<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
28-
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
29-
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
28+
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
29+
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
3030
<script src="{{url_for('static', filename='main.js')}}" type="text/javascript"></script>
3131
{% block js %}{% endblock %}
3232

project/server/__init__.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,31 @@
77
from flask_bootstrap import Bootstrap
88

99

10-
app = Flask(
11-
__name__,
12-
template_folder='../client/templates',
13-
static_folder='../client/static'
14-
)
10+
# instantiate the extensions
11+
bootstrap = Bootstrap()
1512

1613

17-
app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig')
18-
app.config.from_object(app_settings)
14+
def create_app(script_info=None):
1915

20-
bootstrap = Bootstrap(app)
16+
# instantiate the app
17+
app = Flask(
18+
__name__,
19+
template_folder='../client/templates',
20+
static_folder='../client/static'
21+
)
2122

23+
# set config
24+
app_settings = os.getenv('APP_SETTINGS')
25+
app.config.from_object(app_settings)
2226

23-
from project.server.main.views import main_blueprint
24-
app.register_blueprint(main_blueprint)
27+
# set up extensions
28+
bootstrap.init_app(app)
29+
30+
# register blueprints
31+
from project.server.main.views import main_blueprint
32+
app.register_blueprint(main_blueprint)
33+
34+
# shell context for flask cli
35+
app.shell_context_processor({'app': app})
36+
37+
return app

project/server/config.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,16 @@
66

77
class BaseConfig(object):
88
"""Base configuration."""
9-
DEBUG = False
109
WTF_CSRF_ENABLED = True
11-
REDIS_URL = 'redis://redis:6379/0'
12-
QUEUES = ['default']
10+
1311

1412
class DevelopmentConfig(BaseConfig):
1513
"""Development configuration."""
16-
DEBUG = True
1714
WTF_CSRF_ENABLED = False
1815

1916

2017
class TestingConfig(BaseConfig):
2118
"""Testing configuration."""
22-
DEBUG = True
2319
TESTING = True
2420
WTF_CSRF_ENABLED = False
2521
PRESERVE_CONTEXT_ON_EXCEPTION = False
26-
27-
28-
class ProductionConfig(BaseConfig):
29-
"""Production configuration."""
30-
DEBUG = False

project/server/main/tasks.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

project/server/main/views.py

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# project/server/main/views.py
22

33

4-
import redis
5-
from rq import Queue, push_connection, pop_connection
6-
from flask import current_app, render_template, Blueprint, jsonify, request
7-
8-
from project.server.main.tasks import create_task
4+
from flask import render_template, Blueprint, jsonify, request
95

106
main_blueprint = Blueprint('main', __name__,)
117

@@ -18,40 +14,9 @@ def home():
1814
@main_blueprint.route('/tasks', methods=['POST'])
1915
def run_task():
2016
task_type = request.form['type']
21-
q = Queue()
22-
task = q.enqueue(create_task, task_type)
23-
response_object = {
24-
'status': 'success',
25-
'data': {
26-
'task_id': task.get_id()
27-
}
28-
}
29-
return jsonify(response_object), 202
17+
return jsonify(task_type), 202
3018

3119

3220
@main_blueprint.route('/tasks/<task_id>', methods=['GET'])
3321
def get_status(task_id):
34-
q = Queue()
35-
task = q.fetch_job(task_id)
36-
if task:
37-
response_object = {
38-
'status': 'success',
39-
'data': {
40-
'task_id': task.get_id(),
41-
'task_status': task.get_status(),
42-
'task_result': task.result,
43-
}
44-
}
45-
else:
46-
response_object = {'status': 'error'}
47-
return jsonify(response_object)
48-
49-
50-
@main_blueprint.before_request
51-
def push_rq_connection():
52-
push_connection(redis.from_url(current_app.config['REDIS_URL']))
53-
54-
55-
@main_blueprint.teardown_request
56-
def pop_rq_connection(exception=None):
57-
pop_connection()
22+
return jsonify(task_id)

project/tests/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
from flask_testing import TestCase
55

6-
from project.server import app
6+
from project.server import create_app
7+
8+
app = create_app()
79

810

911
class BaseTestCase(TestCase):

project/tests/test__config.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# project/server/tests/test_config.py
1+
# project/server/tests/test__config.py
22

33

44
import unittest
55

66
from flask import current_app
77
from flask_testing import TestCase
88

9-
from project.server import app
9+
from project.server import create_app
10+
11+
app = create_app()
1012

1113

1214
class TestDevelopmentConfig(TestCase):
@@ -34,17 +36,5 @@ def test_app_is_testing(self):
3436
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is False)
3537

3638

37-
class TestProductionConfig(TestCase):
38-
39-
def create_app(self):
40-
app.config.from_object('project.server.config.ProductionConfig')
41-
return app
42-
43-
def test_app_is_production(self):
44-
self.assertFalse(current_app.config['TESTING'])
45-
self.assertTrue(app.config['DEBUG'] is False)
46-
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is True)
47-
48-
4939
if __name__ == '__main__':
5040
unittest.main()

requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@ Flask-Bootstrap==3.3.7.1
33
Flask-Script==2.0.6
44
Flask-Testing==0.6.2
55
Flask-WTF==0.14.2
6-
redis==2.10.6
7-
rq==0.8.2

0 commit comments

Comments
 (0)