introduce blueprints and unit tests
This commit is contained in:
parent
f5d8f1d6e3
commit
f7ede63681
7 changed files with 121 additions and 35 deletions
|
@ -1,32 +1,23 @@
|
|||
from flask import Flask, render_template
|
||||
import socket
|
||||
import os
|
||||
import json
|
||||
from flask import Flask
|
||||
from config import config
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
def create_app(config_name):
|
||||
app = Flask(__name__)
|
||||
from .index import index_blueprint
|
||||
from .color import color_blueprint
|
||||
app.register_blueprint(index_blueprint)
|
||||
app.register_blueprint(color_blueprint)
|
||||
app.config.from_object(config[config_name])
|
||||
config[config_name].init_app(app)
|
||||
|
||||
@app.cli.command()
|
||||
def test():
|
||||
"""Run the unit tests."""
|
||||
import unittest
|
||||
tests = unittest.TestLoader().discover('tests')
|
||||
unittest.TextTestRunner(verbosity=2).run(tests)
|
||||
|
||||
@app.route('/color/<color>', methods=['POST'])
|
||||
def color(color):
|
||||
socket_path = '/var/www/vhosts/rgb.local/rgb_socket'
|
||||
if os.path.exists(socket_path):
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
client.connect(socket_path)
|
||||
client.sendall(color.encode())
|
||||
client.close()
|
||||
return (json.dumps({'success': True}),
|
||||
200,
|
||||
{'ContentType': 'application/json'})
|
||||
else:
|
||||
return (json.dumps({'success': False}),
|
||||
500,
|
||||
{'ContentType': 'application/json'})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
return app
|
||||
|
|
27
app/color.py
Normal file
27
app/color.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
from flask import Blueprint
|
||||
import socket
|
||||
import os
|
||||
import json
|
||||
|
||||
|
||||
color_blueprint = Blueprint('color', __name__)
|
||||
|
||||
|
||||
@color_blueprint.route('/color/<color>', methods=['POST'])
|
||||
def color(color):
|
||||
socket_path = '/var/www/vhosts/rgb.local/rgb_socket'
|
||||
if os.path.exists(socket_path):
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
client.connect(socket_path)
|
||||
client.sendall(color.encode())
|
||||
client.close()
|
||||
return (json.dumps({'success': True}),
|
||||
200,
|
||||
{'ContentType': 'application/json'})
|
||||
else:
|
||||
return (json.dumps({
|
||||
'success': False,
|
||||
'message': 'socket not found'
|
||||
}),
|
||||
500,
|
||||
{'ContentType': 'application/json'})
|
8
app/index.py
Normal file
8
app/index.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from flask import render_template, Blueprint
|
||||
|
||||
index_blueprint = Blueprint('index', __name__)
|
||||
|
||||
|
||||
@index_blueprint.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
|
@ -1,9 +0,0 @@
|
|||
blinker==1.6.2
|
||||
click==8.1.3
|
||||
Flask==2.3.2
|
||||
importlib-metadata==6.6.0
|
||||
itsdangerous==2.1.2
|
||||
Jinja2==3.1.2
|
||||
MarkupSafe==2.1.2
|
||||
Werkzeug==2.3.4
|
||||
zipp==3.15.0
|
Loading…
Add table
Add a link
Reference in a new issue