Skip to content

Commit

Permalink
base init
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Feb 22, 2018
1 parent 1615d9f commit db16258
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.1
3.6.4
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM python:3.6.1
# base image
FROM python:3.6.4-alpine

# set working directory
RUN mkdir -p /usr/src/app
Expand All @@ -8,4 +9,4 @@ WORKDIR /usr/src/app
ADD ./requirements.txt /usr/src/app/requirements.txt

# install requirements
RUN pip install -r requirements.txt
RUN pip install -r requirements.txt
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2017 Michael Herman
Copyright (c) 2018 Michael Herman

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:

Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ $ docker-compose up -d
```

Open your browser to http://localhost:5001

### Example

![](app.png)
Binary file removed app.png
Binary file not shown.
21 changes: 3 additions & 18 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
version: '2.1'
version: '3.5'

services:

web:
build: .
image: web
container_name: web
environment:
- APP_SETTINGS=project.server.config.DevelopmentConfig
ports:
- '5001:5000'
command: python manage.py runserver -h 0.0.0.0
command: python manage.py run -h 0.0.0.0
volumes:
- .:/usr/src/app
depends_on:
- redis

worker:
image: web
container_name: worker
environment:
- FLASK_DEBUG=1
- APP_SETTINGS=project.server.config.DevelopmentConfig
command: python manage.py run_worker
volumes:
- .:/usr/src/app
depends_on:
- redis

redis:
image: redis:3.2.11
22 changes: 6 additions & 16 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# manage.py


import os
import unittest

import redis
from rq import Connection, Worker
from flask_script import Manager
from flask.cli import FlaskGroup

from project.server import app
from project.server import create_app


manager = Manager(app)
app = create_app()
cli = FlaskGroup(create_app=create_app)


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


@manager.command
def run_worker():
redis_url = app.config['REDIS_URL']
redis_connection = redis.from_url(redis_url)
with Connection(redis_connection):
worker = Worker(app.config['QUEUES'])
worker.work()

if __name__ == '__main__':
manager.run()
cli()
9 changes: 4 additions & 5 deletions project/client/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ $('.btn').on('click', function() {
})
.fail((err) => {
console.log(err)
})
})
});
});

function getStatus(taskID) {
$.ajax({
Expand All @@ -30,7 +30,7 @@ function getStatus(taskID) {
<td>${res.data.task_status}</td>
<td>${res.data.task_result}</td>
</tr>`
$('#tasks').prepend(html)
$('#tasks').prepend(html);
const taskStatus = res.data.task_status;
if (taskStatus === 'finished' || taskStatus === 'failed') return false;
setTimeout(function() {
Expand All @@ -39,6 +39,5 @@ function getStatus(taskID) {
})
.fail((err) => {
console.log(err)
})
});
}

6 changes: 3 additions & 3 deletions project/client/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<meta name="author" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<!-- styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link href="{{url_for('static', filename='main.css')}}" rel="stylesheet" media="screen">
{% block css %}{% endblock %}
</head>
Expand All @@ -25,8 +25,8 @@

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

Expand Down
33 changes: 23 additions & 10 deletions project/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
from flask_bootstrap import Bootstrap


app = Flask(
__name__,
template_folder='../client/templates',
static_folder='../client/static'
)
# instantiate the extensions
bootstrap = Bootstrap()


app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig')
app.config.from_object(app_settings)
def create_app(script_info=None):

bootstrap = Bootstrap(app)
# instantiate the app
app = Flask(
__name__,
template_folder='../client/templates',
static_folder='../client/static'
)

# set config
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)

from project.server.main.views import main_blueprint
app.register_blueprint(main_blueprint)
# set up extensions
bootstrap.init_app(app)

# register blueprints
from project.server.main.views import main_blueprint
app.register_blueprint(main_blueprint)

# shell context for flask cli
app.shell_context_processor({'app': app})

return app
11 changes: 1 addition & 10 deletions project/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@

class BaseConfig(object):
"""Base configuration."""
DEBUG = False
WTF_CSRF_ENABLED = True
REDIS_URL = 'redis://redis:6379/0'
QUEUES = ['default']


class DevelopmentConfig(BaseConfig):
"""Development configuration."""
DEBUG = True
WTF_CSRF_ENABLED = False


class TestingConfig(BaseConfig):
"""Testing configuration."""
DEBUG = True
TESTING = True
WTF_CSRF_ENABLED = False
PRESERVE_CONTEXT_ON_EXCEPTION = False


class ProductionConfig(BaseConfig):
"""Production configuration."""
DEBUG = False
9 changes: 0 additions & 9 deletions project/server/main/tasks.py

This file was deleted.

41 changes: 3 additions & 38 deletions project/server/main/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# project/server/main/views.py


import redis
from rq import Queue, push_connection, pop_connection
from flask import current_app, render_template, Blueprint, jsonify, request

from project.server.main.tasks import create_task
from flask import render_template, Blueprint, jsonify, request

main_blueprint = Blueprint('main', __name__,)

Expand All @@ -18,40 +14,9 @@ def home():
@main_blueprint.route('/tasks', methods=['POST'])
def run_task():
task_type = request.form['type']
q = Queue()
task = q.enqueue(create_task, task_type)
response_object = {
'status': 'success',
'data': {
'task_id': task.get_id()
}
}
return jsonify(response_object), 202
return jsonify(task_type), 202


@main_blueprint.route('/tasks/<task_id>', methods=['GET'])
def get_status(task_id):
q = Queue()
task = q.fetch_job(task_id)
if task:
response_object = {
'status': 'success',
'data': {
'task_id': task.get_id(),
'task_status': task.get_status(),
'task_result': task.result,
}
}
else:
response_object = {'status': 'error'}
return jsonify(response_object)


@main_blueprint.before_request
def push_rq_connection():
push_connection(redis.from_url(current_app.config['REDIS_URL']))


@main_blueprint.teardown_request
def pop_rq_connection(exception=None):
pop_connection()
return jsonify(task_id)
4 changes: 3 additions & 1 deletion project/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from flask_testing import TestCase

from project.server import app
from project.server import create_app

app = create_app()


class BaseTestCase(TestCase):
Expand Down
18 changes: 4 additions & 14 deletions project/tests/test__config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# project/server/tests/test_config.py
# project/server/tests/test__config.py


import unittest

from flask import current_app
from flask_testing import TestCase

from project.server import app
from project.server import create_app

app = create_app()


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


class TestProductionConfig(TestCase):

def create_app(self):
app.config.from_object('project.server.config.ProductionConfig')
return app

def test_app_is_production(self):
self.assertFalse(current_app.config['TESTING'])
self.assertTrue(app.config['DEBUG'] is False)
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is True)


if __name__ == '__main__':
unittest.main()
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ Flask-Bootstrap==3.3.7.1
Flask-Script==2.0.6
Flask-Testing==0.6.2
Flask-WTF==0.14.2
redis==2.10.6
rq==0.8.2

0 comments on commit db16258

Please sign in to comment.