From 0b66d4ae0d8583836375c26cd721cdbbb81e8146 Mon Sep 17 00:00:00 2001 From: NilsGrunwald Date: Sun, 2 Oct 2022 17:43:55 +0200 Subject: [PATCH] set up docker generation --- .dockerignore | 26 +++++++ .gitignore | 107 ++++++++++++++++++++++++++++ .vscode/launch.json | 78 ++++++++++++++------ Dockerfile | 25 +++++++ LICENSE | 21 ++++++ README.md | 28 ++++++++ __init__.py | 6 -- app.py | 28 -------- docker-compose.debug.yml | 14 ++++ docker-compose.yml | 10 +++ requirements.txt | 4 +- web2mqtt/__init__.py | 8 +++ web2mqtt/static/data.json | 5 ++ web2mqtt/static/site.css | 32 +++++++++ web2mqtt/templates/about.html | 7 ++ web2mqtt/templates/contact.html | 7 ++ web2mqtt/templates/hello_there.html | 16 +++++ web2mqtt/templates/home.html | 7 ++ web2mqtt/templates/layout.html | 25 +++++++ web2mqtt/views.py | 45 ++++++++++++ web2mqtt/webapp.py | 6 ++ 21 files changed, 447 insertions(+), 58 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md delete mode 100644 __init__.py delete mode 100644 app.py create mode 100644 docker-compose.debug.yml create mode 100644 docker-compose.yml create mode 100644 web2mqtt/__init__.py create mode 100644 web2mqtt/static/data.json create mode 100644 web2mqtt/static/site.css create mode 100644 web2mqtt/templates/about.html create mode 100644 web2mqtt/templates/contact.html create mode 100644 web2mqtt/templates/hello_there.html create mode 100644 web2mqtt/templates/home.html create mode 100644 web2mqtt/templates/layout.html create mode 100644 web2mqtt/views.py create mode 100644 web2mqtt/webapp.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..21bf6d4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,26 @@ +**/__pycache__ +**/.venv +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45e7bbe --- /dev/null +++ b/.gitignore @@ -0,0 +1,107 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +.vscode/ +settings.json diff --git a/.vscode/launch.json b/.vscode/launch.json index d193fa2..d6ac91b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,25 +1,57 @@ { - // Verwendet IntelliSense zum Ermitteln möglicher Attribute. - // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. - // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Python: Flask", - "type": "python", - "request": "launch", - "module": "flask", - "env": { - "FLASK_APP": "app.py", - "FLASK_DEBUG": "1" - }, - "args": [ - "run", - "--no-debugger", - "--no-reload" - ], - "jinja": true, - "justMyCode": true - } - ] + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Flask (0.11.x or later)", + "type": "python", + "request": "launch", + "module": "flask", + "env": { + "FLASK_APP": "web2mqtt.webapp" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload" + ] + }, + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "env": { + "FLASK_ENV": "development" + } + }, + { + "name": "Python: Attach", + "type": "python", + "request": "attach", + "localRoot": "${workspaceFolder}", + "remoteRoot": "${workspaceFolder}", + "port": 3000, + "secret": "my_secret", + "host": "localhost" + }, + { + "name": "Docker: Python - Flask", + "type": "docker", + "request": "launch", + "preLaunchTask": "docker-run: debug", + "python": { + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "/app" + } + ], + "projectType": "flask" + } + } + ] } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..06350eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +# For more information, please refer to https://aka.ms/vscode-docker-python +FROM python:3.8-slim + +EXPOSE 5002 + +# Keeps Python from generating .pyc files in the container +ENV PYTHONDONTWRITEBYTECODE=1 + +# Turns off buffering for easier container logging +ENV PYTHONUNBUFFERED=1 + +# Install pip requirements +COPY requirements.txt . +RUN python -m pip install -r requirements.txt + +WORKDIR /app +COPY . /app + +# Creates a non-root user with an explicit UID and adds permission to access the /app folder +# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers +RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app +USER appuser + +# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug +CMD ["gunicorn", "--bind", "0.0.0.0:5002", "web2mqtt.webapp:app"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2107107 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ + MIT License + + Copyright (c) Microsoft Corporation. All rights reserved. + + 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: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE diff --git a/README.md b/README.md new file mode 100644 index 0000000..d08e047 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# Python/Flask Tutorial Sample for VS Code + +* This sample contains the completed program from the tutorial [Using Flask in Visual Studio Code](https://code.visualstudio.com/docs/python/tutorial-flask). Intermediate steps are not included. + +To run the sample: + +1. Clone the repo by running `git clone -b tutorial https://github.com/microsoft/python-sample-vscode-flask-tutorial.git` +2. In VS Code Terminal, run `python -m venv env` to create a virtual environment as described in the tutorial. +3. Press Ctrl + Shift + P and run command `Python: Select Interpreter`. If possible, select the interpreter ending with "('env': venv)" +4. Activate the virtual environment by running `env/scripts/activate` if you are on Windows or run `source env/bin/activate` if you are on Linux/MacOS. +5. In terminal, run `pip install flask`. +6. From Run and Debug section, select `Python: Flask` launch configuration and hit F5. + +* For steps on running this app in a Docker container, see [Python in containers](https://code.visualstudio.com/docs/containers/quickstart-python) on the VS Code Docs website. + +## Contributing + +Contributions to the sample are welcome. When submitting changes, also consider submitting matching changes to the tutorial's source file [tutorial-flask.md](https://github.com/Microsoft/vscode-docs/blob/master/docs/python/tutorial-flask.md). + +Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and willingly choose to, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com. + +When you submit a pull request, a CLA-bot automatically determines whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. + +## Additional details + +* This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). +* For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or +* Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 608a2f0..0000000 --- a/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -import paho.mqtt.client as mqtt - -client = mqtt.Client("Web2Mqtt") -client.connect("192.168.178.76",1884) -client.publish("web2mqtt/status","Web2Mqtt starting up...") - diff --git a/app.py b/app.py deleted file mode 100644 index 8bf0031..0000000 --- a/app.py +++ /dev/null @@ -1,28 +0,0 @@ -from flask import Flask, request -from datetime import datetime -from . import client - -import re - -app = Flask(__name__) - -CMD = "web2mqtt/command" - -@app.route("/") -def home(): - return "Small URL to MQTT Gateway for home automation...!" - -@app.route("/cmnd") -def command(): - opts = "" - command = CMD - - if request.args.get('cmnd'): - command += "/" + request.args.get('cmnd') - if request.args.get('opts'): - opts = request.args.get('opts') - - client.publish(command,opts) - - content = f"Command: {command} triggered with opts: {opts}" - return content \ No newline at end of file diff --git a/docker-compose.debug.yml b/docker-compose.debug.yml new file mode 100644 index 0000000..b40b198 --- /dev/null +++ b/docker-compose.debug.yml @@ -0,0 +1,14 @@ +version: '3.4' + +services: + web2mqtt: + image: web2mqtt + build: + context: . + dockerfile: ./Dockerfile + command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m flask run --no-debugger --no-reload --host 0.0.0.0 --port 5002"] + ports: + - 5002:5002 + - 5678:5678 + environment: + - FLASK_APP=web2mqtt\webapp.py diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..731d2d1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: '3.4' + +services: + web2mqtt: + image: web2mqtt + build: + context: . + dockerfile: ./Dockerfile + ports: + - 5002:5002 diff --git a/requirements.txt b/requirements.txt index 4b2c12c..a83d0d8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ -paho-mqtt == 1.5.0 \ No newline at end of file +paho-mqtt == 1.5.0 +Flask == 2.2.2 +gunicorn == 20.1.0 diff --git a/web2mqtt/__init__.py b/web2mqtt/__init__.py new file mode 100644 index 0000000..97eb3eb --- /dev/null +++ b/web2mqtt/__init__.py @@ -0,0 +1,8 @@ +from flask import Flask # Import the Flask class +app = Flask(__name__) # Create an instance of the class for our use + +import paho.mqtt.client as mqtt + +client = mqtt.Client("Web2Mqtt") +client.connect("192.168.178.76",1884) +client.publish("web2mqtt/status","Web2Mqtt starting up...") \ No newline at end of file diff --git a/web2mqtt/static/data.json b/web2mqtt/static/data.json new file mode 100644 index 0000000..d8fdc16 --- /dev/null +++ b/web2mqtt/static/data.json @@ -0,0 +1,5 @@ +{ + "01": { + "note" : "Data is very simple because we're demonstrating only the mechanism." + } +} \ No newline at end of file diff --git a/web2mqtt/static/site.css b/web2mqtt/static/site.css new file mode 100644 index 0000000..c14da26 --- /dev/null +++ b/web2mqtt/static/site.css @@ -0,0 +1,32 @@ +.message { + font-weight: 600; + color: blue; +} + +.navbar { + background-color: lightslategray; + font-size: 1em; + font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; + color: white; + padding: 8px 5px 8px 5px; +} + +.navbar a { + text-decoration: none; + color: inherit; +} + +.navbar-brand { + font-size: 1.2em; + font-weight: 600; +} + +.navbar-item { + font-variant: small-caps; + margin-left: 30px; +} + +.body-content { + padding: 5px; + font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; +} \ No newline at end of file diff --git a/web2mqtt/templates/about.html b/web2mqtt/templates/about.html new file mode 100644 index 0000000..8611b64 --- /dev/null +++ b/web2mqtt/templates/about.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} +{% block title %} +About us +{% endblock %} +{% block content %} +

About page for the Visual Studio Code Flask tutorial.

+{% endblock %} diff --git a/web2mqtt/templates/contact.html b/web2mqtt/templates/contact.html new file mode 100644 index 0000000..3321c94 --- /dev/null +++ b/web2mqtt/templates/contact.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} +{% block title %} +Contact us +{% endblock %} +{% block content %} +

Contact page for the Visual Studio Code Flask tutorial.

+{% endblock %} diff --git a/web2mqtt/templates/hello_there.html b/web2mqtt/templates/hello_there.html new file mode 100644 index 0000000..de1ef49 --- /dev/null +++ b/web2mqtt/templates/hello_there.html @@ -0,0 +1,16 @@ + + + + + + Hello, Flask + + + {%if name %} + Hello there, {{ name }}! It's {{ date.strftime("%A, %d %B, %Y at %X") }}. + {% else %} + What's your name? Provide it after /hello/ in the URL. + {% endif %} + + + diff --git a/web2mqtt/templates/home.html b/web2mqtt/templates/home.html new file mode 100644 index 0000000..95609fe --- /dev/null +++ b/web2mqtt/templates/home.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} +{% block title %} +Home +{% endblock %} +{% block content %} +

Home page for the Visual Studio Code Flask tutorial.

+{% endblock %} diff --git a/web2mqtt/templates/layout.html b/web2mqtt/templates/layout.html new file mode 100644 index 0000000..fd83b92 --- /dev/null +++ b/web2mqtt/templates/layout.html @@ -0,0 +1,25 @@ + + + + + {% block title %}{% endblock %} + + + + + + +
+ {% block content %} + {% endblock %} +
+
+

© 2018

+
+
+ + diff --git a/web2mqtt/views.py b/web2mqtt/views.py new file mode 100644 index 0000000..d7942bd --- /dev/null +++ b/web2mqtt/views.py @@ -0,0 +1,45 @@ +from datetime import datetime +from flask import Flask, render_template,request +from . import app, client + +@app.route("/") +def home(): + return render_template("home.html") + +@app.route("/about/") +def about(): + return render_template("about.html") + +@app.route("/contact/") +def contact(): + return render_template("contact.html") + +@app.route("/hello/") +@app.route("/hello/") +def hello_there(name = None): + return render_template( + "hello_there.html", + name=name, + date=datetime.now() + ) + +@app.route("/api/data") +def get_data(): + return app.send_static_file("data.json") + +CMD = "web2mqtt/command" + +@app.route("/cmnd") +def command(): + opts = "" + command = CMD + + if request.args.get('cmnd'): + command += "/" + request.args.get('cmnd') + if request.args.get('opts'): + opts = request.args.get('opts') + + client.publish(command,opts) + + content = f"Command: {command} triggered with opts: {opts}" + return content \ No newline at end of file diff --git a/web2mqtt/webapp.py b/web2mqtt/webapp.py new file mode 100644 index 0000000..56386de --- /dev/null +++ b/web2mqtt/webapp.py @@ -0,0 +1,6 @@ +# Entry point for the application. +from . import app # For application discovery by the 'flask' command. +from . import views # For import side-effects of setting up routes. + +# Time-saver: output a URL to the VS Code terminal so you can easily Ctrl+click to open a browser +# print('http://127.0.0.1:5000/hello/VSCode')