teilen 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- teilen-0.1.0/MANIFEST.in +1 -0
- teilen-0.1.0/PKG-INFO +115 -0
- teilen-0.1.0/setup.cfg +4 -0
- teilen-0.1.0/setup.py +51 -0
- teilen-0.1.0/teilen/__init__.py +0 -0
- teilen-0.1.0/teilen/api.py +163 -0
- teilen-0.1.0/teilen/app.py +277 -0
- teilen-0.1.0/teilen/client/asset-manifest.json +11 -0
- teilen-0.1.0/teilen/client/favicon2.ico +0 -0
- teilen-0.1.0/teilen/client/favicon2.svg +51 -0
- teilen-0.1.0/teilen/client/file2.svg +64 -0
- teilen-0.1.0/teilen/client/folder2.svg +29 -0
- teilen-0.1.0/teilen/client/index.html +1 -0
- teilen-0.1.0/teilen/client/static/css/main.b4df923a.css +3 -0
- teilen-0.1.0/teilen/client/static/js/main.88119a7e.js +2 -0
- teilen-0.1.0/teilen/client/static/js/main.88119a7e.js.LICENSE.txt +49 -0
- teilen-0.1.0/teilen/config.py +23 -0
- teilen-0.1.0/teilen.egg-info/PKG-INFO +115 -0
- teilen-0.1.0/teilen.egg-info/SOURCES.txt +21 -0
- teilen-0.1.0/teilen.egg-info/dependency_links.txt +1 -0
- teilen-0.1.0/teilen.egg-info/entry_points.txt +2 -0
- teilen-0.1.0/teilen.egg-info/requires.txt +2 -0
- teilen-0.1.0/teilen.egg-info/top_level.txt +1 -0
teilen-0.1.0/MANIFEST.in
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
graft client
|
teilen-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: teilen
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: a simple application to share data via http with python flask backend and react frontend
|
|
5
|
+
Home-page: https://pypi.org/project/teilen/
|
|
6
|
+
Author: Steffen Richters-Finger
|
|
7
|
+
Author-email: srichters@uni-muenster.de
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Source, https://github.com/RichtersFinger/teilen
|
|
10
|
+
Classifier: Development Status :: 1 - Planning
|
|
11
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
12
|
+
Classifier: Topic :: Communications
|
|
13
|
+
Classifier: Topic :: Communications :: File Sharing
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Framework :: Flask
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
Requires-Dist: Flask<4,>=3
|
|
23
|
+
Requires-Dist: gunicorn
|
|
24
|
+
Dynamic: author
|
|
25
|
+
Dynamic: author-email
|
|
26
|
+
Dynamic: classifier
|
|
27
|
+
Dynamic: description
|
|
28
|
+
Dynamic: description-content-type
|
|
29
|
+
Dynamic: home-page
|
|
30
|
+
Dynamic: license
|
|
31
|
+
Dynamic: project-url
|
|
32
|
+
Dynamic: requires-dist
|
|
33
|
+
Dynamic: requires-python
|
|
34
|
+
Dynamic: summary
|
|
35
|
+
|
|
36
|
+
# teilen
|
|
37
|
+
|
|
38
|
+
A simple application to share data via http with python flask backend and react frontend.
|
|
39
|
+
|
|
40
|
+
Share the contents of a local directory with a single command over the network.
|
|
41
|
+
|
|
42
|
+
It features
|
|
43
|
+
* a lightweight and responsive interface,
|
|
44
|
+
* downloads for entire directories as ZIP-files, and
|
|
45
|
+
* a simple authentication-system.
|
|
46
|
+
|
|
47
|
+
See the [demo-gallery](./gallery/gallery.md) for some impressions.
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
`teilen` can be installed using `pip` by entering
|
|
51
|
+
```
|
|
52
|
+
pip install teilen
|
|
53
|
+
```
|
|
54
|
+
It is recommended to use a virtual environment
|
|
55
|
+
```
|
|
56
|
+
python3 -m venv venv
|
|
57
|
+
source venv/bin/activate
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Run
|
|
61
|
+
After installing, run the `teilen`-application with
|
|
62
|
+
```
|
|
63
|
+
teilen <path/to/directory>
|
|
64
|
+
```
|
|
65
|
+
By default, the web-UI is available at `http://localhost:27183`.
|
|
66
|
+
If you are done, stop by hitting `Ctrl`+`C`.
|
|
67
|
+
|
|
68
|
+
### Command line options
|
|
69
|
+
* the last argument to the cli is interpreted as the path to the directory that should be shared, e.g.,
|
|
70
|
+
```
|
|
71
|
+
teilen <path/to/directory>
|
|
72
|
+
```
|
|
73
|
+
omit to share the current working directory
|
|
74
|
+
* `-p`, `--password`: (optionally) set a password that is required to access the share
|
|
75
|
+
|
|
76
|
+
The authentication method is implement via a custom request-header `X-Teilen-Auth` that, if the option is set, is required for any content-related endpoints of the `teilen`-API.
|
|
77
|
+
* `--port`: (optionally) changes the port that the application is running on (default is 27183)
|
|
78
|
+
|
|
79
|
+
## Update
|
|
80
|
+
Run
|
|
81
|
+
```
|
|
82
|
+
pip install teilen --upgrade
|
|
83
|
+
```
|
|
84
|
+
to get the latest version.
|
|
85
|
+
|
|
86
|
+
## Environment configuration
|
|
87
|
+
The following environment variables can be set to configure `teilen`:
|
|
88
|
+
|
|
89
|
+
- `WORKING_DIR` [DEFAULT \<cwd>] path to the shared directory (see also the argument to the cli)
|
|
90
|
+
- `PASSWORD` [DEFAULT null] optional password to access the share (see also the cli-option `-p`, `--password`)
|
|
91
|
+
- `PORT` [DEFAULT 27183] teilen port (see also the cli-option `--port`)
|
|
92
|
+
- `SECRET_KEY` [DEFAULT \<random uuid>] secret key
|
|
93
|
+
|
|
94
|
+
### Running in dev-mode
|
|
95
|
+
The development setup requires both `python3` and the node package manager `npm` to be installed.
|
|
96
|
+
Contrary to the pure python production server, back- and frontend are run separately in the development context.
|
|
97
|
+
|
|
98
|
+
To run the backend server, enter
|
|
99
|
+
```
|
|
100
|
+
cd backend
|
|
101
|
+
python3 -m venv venv
|
|
102
|
+
source venv/bin/activate
|
|
103
|
+
pip install .
|
|
104
|
+
pip install -r dev-requirements.txt
|
|
105
|
+
MODE=dev flask run
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Based on pre-defined scripts, the frontend development server can be started with
|
|
109
|
+
```
|
|
110
|
+
cd frontend
|
|
111
|
+
npm install
|
|
112
|
+
npm start
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The client can then be accessed via the node-development server at `http://localhost:3000`.
|
teilen-0.1.0/setup.cfg
ADDED
teilen-0.1.0/setup.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from setuptools import setup
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
long_description = (Path(__file__).parent.parent / "README.md").read_text(
|
|
8
|
+
encoding="utf8"
|
|
9
|
+
)
|
|
10
|
+
except FileNotFoundError:
|
|
11
|
+
long_description = "See docs at https://github.com/RichtersFinger/teilen"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
setup(
|
|
15
|
+
version=os.environ.get("VERSION", "0.1.0"),
|
|
16
|
+
name="teilen",
|
|
17
|
+
description="a simple application to share data via http with python flask backend and react frontend",
|
|
18
|
+
long_description=long_description,
|
|
19
|
+
long_description_content_type="text/markdown",
|
|
20
|
+
author="Steffen Richters-Finger",
|
|
21
|
+
author_email="srichters@uni-muenster.de",
|
|
22
|
+
license="MIT",
|
|
23
|
+
url="https://pypi.org/project/teilen/",
|
|
24
|
+
project_urls={"Source": "https://github.com/RichtersFinger/teilen"},
|
|
25
|
+
python_requires=">=3.10",
|
|
26
|
+
install_requires=[
|
|
27
|
+
"Flask>=3,<4",
|
|
28
|
+
"gunicorn",
|
|
29
|
+
],
|
|
30
|
+
packages=[
|
|
31
|
+
"teilen",
|
|
32
|
+
],
|
|
33
|
+
package_data={"teilen": ["client/**/*"]},
|
|
34
|
+
entry_points={
|
|
35
|
+
"console_scripts": [
|
|
36
|
+
"teilen = teilen.app:run",
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
classifiers=[
|
|
40
|
+
"Development Status :: 1 - Planning",
|
|
41
|
+
"Intended Audience :: End Users/Desktop",
|
|
42
|
+
"Topic :: Communications",
|
|
43
|
+
"Topic :: Communications :: File Sharing",
|
|
44
|
+
"License :: OSI Approved :: MIT License",
|
|
45
|
+
"Programming Language :: Python :: 3",
|
|
46
|
+
"Programming Language :: Python :: 3.10",
|
|
47
|
+
"Programming Language :: Python :: 3.11",
|
|
48
|
+
"Programming Language :: Python :: 3.12",
|
|
49
|
+
"Framework :: Flask",
|
|
50
|
+
],
|
|
51
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""teilen api-definition"""
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
from functools import wraps
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from urllib.parse import unquote
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from tempfile import TemporaryDirectory
|
|
9
|
+
import zipfile
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
from flask import Flask, Response, jsonify, request, send_from_directory
|
|
13
|
+
|
|
14
|
+
from teilen.config import AppConfig
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def login_required(password: Optional[str]):
|
|
18
|
+
"""Protect endpoint with auth via 'X-Teilen-Auth'-header."""
|
|
19
|
+
|
|
20
|
+
def decorator(route):
|
|
21
|
+
@wraps(route)
|
|
22
|
+
def __():
|
|
23
|
+
if request.headers.get("X-Teilen-Auth") != password:
|
|
24
|
+
return Response("FAILED", mimetype="text/plain", status=401)
|
|
25
|
+
return route()
|
|
26
|
+
|
|
27
|
+
return __
|
|
28
|
+
|
|
29
|
+
return decorator
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def register_api(app: Flask, config: AppConfig):
|
|
33
|
+
"""Sets up api endpoints."""
|
|
34
|
+
|
|
35
|
+
@app.route("/configuration", methods=["GET"])
|
|
36
|
+
def get_configuration():
|
|
37
|
+
"""
|
|
38
|
+
Get basic info on configuration.
|
|
39
|
+
"""
|
|
40
|
+
return jsonify({"passwordRequired": config.PASSWORD is not None}), 200
|
|
41
|
+
|
|
42
|
+
@app.route("/login", methods=["GET"])
|
|
43
|
+
@login_required(config.PASSWORD)
|
|
44
|
+
def get_login():
|
|
45
|
+
"""
|
|
46
|
+
Test login.
|
|
47
|
+
"""
|
|
48
|
+
return Response("OK", mimetype="text/plain", status=200)
|
|
49
|
+
|
|
50
|
+
def get_location(provide_default: bool = True) -> Optional[Path]:
|
|
51
|
+
"""Parse and return location-arg."""
|
|
52
|
+
if request.args.get("location") is None:
|
|
53
|
+
if provide_default:
|
|
54
|
+
return config.WORKING_DIR
|
|
55
|
+
return None
|
|
56
|
+
return (
|
|
57
|
+
config.WORKING_DIR / unquote(request.args["location"])
|
|
58
|
+
).resolve()
|
|
59
|
+
|
|
60
|
+
@app.route("/contents", methods=["GET"])
|
|
61
|
+
@login_required(config.PASSWORD)
|
|
62
|
+
def get_contents():
|
|
63
|
+
"""
|
|
64
|
+
Returns contents for given location.
|
|
65
|
+
"""
|
|
66
|
+
_location = get_location()
|
|
67
|
+
|
|
68
|
+
# check for problems
|
|
69
|
+
if (
|
|
70
|
+
config.WORKING_DIR != _location
|
|
71
|
+
and config.WORKING_DIR not in _location.parents
|
|
72
|
+
):
|
|
73
|
+
return Response("Not allowed.", mimetype="text/plain", status=403)
|
|
74
|
+
if not _location.is_dir():
|
|
75
|
+
return Response(
|
|
76
|
+
"Does not exist.", mimetype="text/plain", status=404
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
contents = list(_location.glob("*"))
|
|
80
|
+
folders = filter(lambda p: p.is_dir(), contents)
|
|
81
|
+
files = filter(lambda p: p.is_file(), contents)
|
|
82
|
+
return (
|
|
83
|
+
jsonify(
|
|
84
|
+
[
|
|
85
|
+
{
|
|
86
|
+
"type": "folder",
|
|
87
|
+
"name": f.name,
|
|
88
|
+
"mtime": f.stat().st_mtime,
|
|
89
|
+
}
|
|
90
|
+
for f in folders
|
|
91
|
+
]
|
|
92
|
+
+ [
|
|
93
|
+
{
|
|
94
|
+
"type": "file",
|
|
95
|
+
"name": f.name,
|
|
96
|
+
"mtime": f.stat().st_mtime,
|
|
97
|
+
"size": f.stat().st_size,
|
|
98
|
+
}
|
|
99
|
+
for f in files
|
|
100
|
+
]
|
|
101
|
+
),
|
|
102
|
+
200,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
@app.route("/content", methods=["GET"])
|
|
106
|
+
@login_required(config.PASSWORD)
|
|
107
|
+
def get_content():
|
|
108
|
+
"""
|
|
109
|
+
Returns file/folder (as archive) for given location.
|
|
110
|
+
"""
|
|
111
|
+
_location = get_location(False)
|
|
112
|
+
|
|
113
|
+
if _location is None:
|
|
114
|
+
return Response(
|
|
115
|
+
"Missing 'location' arg.", mimetype="text/plain", status=400
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# check for problems
|
|
119
|
+
if config.WORKING_DIR not in _location.parents:
|
|
120
|
+
return Response("Not allowed.", mimetype="text/plain", status=403)
|
|
121
|
+
if not _location.exists():
|
|
122
|
+
return Response(
|
|
123
|
+
"Does not exist.", mimetype="text/plain", status=404
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
if _location.is_file():
|
|
127
|
+
return send_from_directory(
|
|
128
|
+
config.WORKING_DIR,
|
|
129
|
+
_location.relative_to(config.WORKING_DIR),
|
|
130
|
+
as_attachment=True,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
# generate archive in /tmp
|
|
134
|
+
if _location.is_dir():
|
|
135
|
+
with TemporaryDirectory() as tmp_dir:
|
|
136
|
+
tmp_path = Path(tmp_dir).resolve()
|
|
137
|
+
archive_path = tmp_path / (
|
|
138
|
+
_location.name + "-" + str(uuid4()) + ".zip"
|
|
139
|
+
)
|
|
140
|
+
print(
|
|
141
|
+
f"[{datetime.now().isoformat()}] Creating archive "
|
|
142
|
+
+ f"for '{_location.resolve()}' "
|
|
143
|
+
+ f"in '{Path(archive_path).resolve()}'."
|
|
144
|
+
)
|
|
145
|
+
with zipfile.ZipFile(
|
|
146
|
+
archive_path, "w", zipfile.ZIP_STORED
|
|
147
|
+
) as archive:
|
|
148
|
+
for f in _location.glob("**/*"):
|
|
149
|
+
if f.is_file():
|
|
150
|
+
archive.write(
|
|
151
|
+
f,
|
|
152
|
+
f.resolve().relative_to(
|
|
153
|
+
_location.parent.resolve()
|
|
154
|
+
),
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
return send_from_directory(
|
|
158
|
+
tmp_path,
|
|
159
|
+
archive_path.relative_to(tmp_path),
|
|
160
|
+
as_attachment=True,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
return Response("Unkown type.", mimetype="text/plain", status=501)
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"""teilen-backend definition."""
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import socket
|
|
6
|
+
import urllib.request
|
|
7
|
+
from importlib.metadata import version
|
|
8
|
+
|
|
9
|
+
from flask import (
|
|
10
|
+
Flask,
|
|
11
|
+
Response,
|
|
12
|
+
send_from_directory,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from teilen.config import AppConfig
|
|
16
|
+
from teilen.api import register_api
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def load_cors(_app: Flask, url: str) -> None:
|
|
20
|
+
"""Loads CORS-extension if required."""
|
|
21
|
+
try:
|
|
22
|
+
# pylint: disable=import-outside-toplevel
|
|
23
|
+
from flask_cors import CORS
|
|
24
|
+
except ImportError:
|
|
25
|
+
print(
|
|
26
|
+
"\033[31mERROR: Missing 'Flask-CORS'-package for dev-server. "
|
|
27
|
+
+ "Install with 'pip install flask-cors'.\033[0m",
|
|
28
|
+
file=sys.stderr,
|
|
29
|
+
)
|
|
30
|
+
sys.exit(1)
|
|
31
|
+
else:
|
|
32
|
+
print("INFO: Configuring app for CORS.", file=sys.stderr)
|
|
33
|
+
_ = CORS(
|
|
34
|
+
_app,
|
|
35
|
+
resources={"*": {"origins": url}},
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def load_callback_url_options() -> list[dict]:
|
|
40
|
+
"""
|
|
41
|
+
Returns a list of IP-addresses with a name.
|
|
42
|
+
|
|
43
|
+
Every record contains the fields 'name' and 'address'.
|
|
44
|
+
"""
|
|
45
|
+
options = []
|
|
46
|
+
|
|
47
|
+
# get LAN-address (https://stackoverflow.com/a/28950776)
|
|
48
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
49
|
+
s.settimeout(0)
|
|
50
|
+
try:
|
|
51
|
+
s.connect(("10.254.254.254", 1))
|
|
52
|
+
options.append(
|
|
53
|
+
{"address": "http://" + s.getsockname()[0], "name": "local"}
|
|
54
|
+
)
|
|
55
|
+
# pylint: disable=broad-exception-caught
|
|
56
|
+
except Exception:
|
|
57
|
+
pass
|
|
58
|
+
finally:
|
|
59
|
+
s.close()
|
|
60
|
+
|
|
61
|
+
# get global IP
|
|
62
|
+
try:
|
|
63
|
+
with urllib.request.urlopen(
|
|
64
|
+
"https://api.ipify.org", timeout=1
|
|
65
|
+
) as response:
|
|
66
|
+
options.append(
|
|
67
|
+
{
|
|
68
|
+
"address": "http://" + response.read().decode("utf-8"),
|
|
69
|
+
"name": "global",
|
|
70
|
+
}
|
|
71
|
+
)
|
|
72
|
+
# pylint: disable=broad-exception-caught
|
|
73
|
+
except Exception:
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
return options
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def print_welcome_message(config: AppConfig) -> None:
|
|
80
|
+
"""Prints welcome message to stdout."""
|
|
81
|
+
url_options = load_callback_url_options()
|
|
82
|
+
lines = (
|
|
83
|
+
(["Running in dev-mode."] if config.MODE == "dev" else [])
|
|
84
|
+
+ [
|
|
85
|
+
"Your teilen-instance will be available shortly.",
|
|
86
|
+
"",
|
|
87
|
+
"The contents of the following directory will be available:",
|
|
88
|
+
(
|
|
89
|
+
str(config.WORKING_DIR)[:30]
|
|
90
|
+
+ "..."
|
|
91
|
+
+ str(config.WORKING_DIR)[-30:]
|
|
92
|
+
if len(str(config.WORKING_DIR)) > 70
|
|
93
|
+
else str(config.WORKING_DIR)
|
|
94
|
+
),
|
|
95
|
+
]
|
|
96
|
+
+ (
|
|
97
|
+
["Password protection is active."]
|
|
98
|
+
if config.PASSWORD is not None
|
|
99
|
+
else []
|
|
100
|
+
)
|
|
101
|
+
+ (
|
|
102
|
+
["", "The following addresses have been detected automatically:"]
|
|
103
|
+
if url_options
|
|
104
|
+
else []
|
|
105
|
+
)
|
|
106
|
+
+ list(
|
|
107
|
+
map(
|
|
108
|
+
lambda o: f" * {o['name']}: {o['address']}:{config.PORT}",
|
|
109
|
+
url_options,
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
delimiter = "#" * (max(map(len, lines)) + 4)
|
|
114
|
+
print(delimiter)
|
|
115
|
+
for line in lines:
|
|
116
|
+
print(f"# {line}{' '*(len(delimiter) - len(line) - 4)} #")
|
|
117
|
+
print(delimiter)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def app_factory(config: AppConfig) -> Flask:
|
|
121
|
+
"""Returns teilen-Flask app."""
|
|
122
|
+
if not config.WORKING_DIR.is_dir():
|
|
123
|
+
print(
|
|
124
|
+
"\033[1;31mERROR\033[0m: "
|
|
125
|
+
+ f"Requested directory '{config.WORKING_DIR}' does not exist.",
|
|
126
|
+
file=sys.stderr,
|
|
127
|
+
)
|
|
128
|
+
sys.exit(1)
|
|
129
|
+
if not config.WORKING_DIR.is_absolute():
|
|
130
|
+
config.WORKING_DIR = config.WORKING_DIR.resolve()
|
|
131
|
+
|
|
132
|
+
# define Flask-app
|
|
133
|
+
_app = Flask(__name__, static_folder=config.STATIC_PATH)
|
|
134
|
+
|
|
135
|
+
_app.config.from_object(config)
|
|
136
|
+
|
|
137
|
+
# extensions
|
|
138
|
+
if config.MODE == "dev":
|
|
139
|
+
load_cors(_app, config.DEV_CORS_FRONTEND_URL)
|
|
140
|
+
|
|
141
|
+
# print welcome message
|
|
142
|
+
print_welcome_message(config)
|
|
143
|
+
|
|
144
|
+
@_app.route("/ping", methods=["GET"])
|
|
145
|
+
def ping():
|
|
146
|
+
"""
|
|
147
|
+
Returns 'pong'.
|
|
148
|
+
"""
|
|
149
|
+
return Response("pong", mimetype="text/plain", status=200)
|
|
150
|
+
|
|
151
|
+
@_app.route("/version", methods=["GET"])
|
|
152
|
+
def get_version():
|
|
153
|
+
"""
|
|
154
|
+
Returns app version.
|
|
155
|
+
"""
|
|
156
|
+
return Response(version("teilen"), mimetype="text/plain", status=200)
|
|
157
|
+
|
|
158
|
+
register_api(_app, config)
|
|
159
|
+
|
|
160
|
+
@_app.route("/", defaults={"path": ""})
|
|
161
|
+
@_app.route("/<path:path>")
|
|
162
|
+
def get_client(path):
|
|
163
|
+
"""Serve static content."""
|
|
164
|
+
if path != "":
|
|
165
|
+
return send_from_directory(config.STATIC_PATH, path)
|
|
166
|
+
return send_from_directory(config.STATIC_PATH, "index.html")
|
|
167
|
+
|
|
168
|
+
return _app
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def parse_cmdline_args(config: AppConfig):
|
|
172
|
+
"""Update config using command line arguments."""
|
|
173
|
+
index = 1
|
|
174
|
+
while True:
|
|
175
|
+
if index >= len(sys.argv):
|
|
176
|
+
break
|
|
177
|
+
|
|
178
|
+
# password
|
|
179
|
+
if sys.argv[index] in ["-p", "--password"]:
|
|
180
|
+
if len(sys.argv) <= index + 1:
|
|
181
|
+
print(
|
|
182
|
+
"\033[1;31mERROR\033[0m: "
|
|
183
|
+
+ f"Missing value for option '{sys.argv[index]}'.",
|
|
184
|
+
file=sys.stderr,
|
|
185
|
+
)
|
|
186
|
+
sys.exit(1)
|
|
187
|
+
config.PASSWORD = sys.argv[index + 1]
|
|
188
|
+
index += 2
|
|
189
|
+
continue
|
|
190
|
+
|
|
191
|
+
# port
|
|
192
|
+
if sys.argv[index] in ["--port"]:
|
|
193
|
+
if len(sys.argv) <= index + 1:
|
|
194
|
+
print(
|
|
195
|
+
"\033[1;31mERROR\033[0m: "
|
|
196
|
+
+ f"Missing value for option '{sys.argv[index]}'.",
|
|
197
|
+
file=sys.stderr,
|
|
198
|
+
)
|
|
199
|
+
sys.exit(1)
|
|
200
|
+
config.PORT = sys.argv[index + 1]
|
|
201
|
+
index += 2
|
|
202
|
+
continue
|
|
203
|
+
|
|
204
|
+
# working directory (should by the last and a singular argument)
|
|
205
|
+
if len(sys.argv) > index + 1:
|
|
206
|
+
print(
|
|
207
|
+
"\033[1;31mERROR\033[0m: "
|
|
208
|
+
+ f"Unknown option '{sys.argv[index]}'.",
|
|
209
|
+
file=sys.stderr,
|
|
210
|
+
)
|
|
211
|
+
sys.exit(1)
|
|
212
|
+
config.WORKING_DIR = Path(sys.argv[index]).resolve()
|
|
213
|
+
index += 1
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def run(app=None, config=None):
|
|
217
|
+
"""Run flask-app."""
|
|
218
|
+
# load default config
|
|
219
|
+
if not config:
|
|
220
|
+
config = AppConfig()
|
|
221
|
+
|
|
222
|
+
# parse command line arguments
|
|
223
|
+
parse_cmdline_args(config)
|
|
224
|
+
|
|
225
|
+
# load default app
|
|
226
|
+
if not app:
|
|
227
|
+
app = app_factory(config)
|
|
228
|
+
|
|
229
|
+
# not intended for production due to, e.g., cors
|
|
230
|
+
if config.MODE != "prod":
|
|
231
|
+
print(
|
|
232
|
+
"\033[1;33mWARNING\033[0m: "
|
|
233
|
+
+ f"Running in unexpected MODE '{config.Mode}'.",
|
|
234
|
+
file=sys.stderr,
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
# prioritize gunicorn over werkzeug
|
|
238
|
+
try:
|
|
239
|
+
import gunicorn.app.base
|
|
240
|
+
except ImportError:
|
|
241
|
+
print(
|
|
242
|
+
"\033[1;33mWARNING\033[0m: "
|
|
243
|
+
+ "Running without proper wsgi-server.",
|
|
244
|
+
file=sys.stderr,
|
|
245
|
+
)
|
|
246
|
+
app.run(host="0.0.0.0", port=config.PORT)
|
|
247
|
+
else:
|
|
248
|
+
|
|
249
|
+
class StandaloneApplication(gunicorn.app.base.BaseApplication):
|
|
250
|
+
"""See https://docs.gunicorn.org/en/stable/custom.html"""
|
|
251
|
+
|
|
252
|
+
def __init__(self, app_, options=None):
|
|
253
|
+
self.options = options or {}
|
|
254
|
+
self.application = app_
|
|
255
|
+
super().__init__()
|
|
256
|
+
|
|
257
|
+
def load_config(self):
|
|
258
|
+
_config = {
|
|
259
|
+
key: value
|
|
260
|
+
for key, value in self.options.items()
|
|
261
|
+
if key in self.cfg.settings and value is not None
|
|
262
|
+
}
|
|
263
|
+
for key, value in _config.items():
|
|
264
|
+
self.cfg.set(key.lower(), value)
|
|
265
|
+
|
|
266
|
+
def load(self):
|
|
267
|
+
return self.application
|
|
268
|
+
|
|
269
|
+
StandaloneApplication(
|
|
270
|
+
app,
|
|
271
|
+
{
|
|
272
|
+
"bind": f"0.0.0.0:{config.PORT}",
|
|
273
|
+
"workers": 1,
|
|
274
|
+
"threads": config.FLASK_THREADS,
|
|
275
|
+
}
|
|
276
|
+
| (config.GUNICORN_OPTIONS or {}),
|
|
277
|
+
).run()
|
|
Binary file
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
2
|
+
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
|
3
|
+
|
|
4
|
+
<svg
|
|
5
|
+
width="20.540001mm"
|
|
6
|
+
height="20.540003mm"
|
|
7
|
+
viewBox="0 0 20.540001 20.540004"
|
|
8
|
+
version="1.1"
|
|
9
|
+
id="svg5"
|
|
10
|
+
xml:space="preserve"
|
|
11
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
12
|
+
xmlns:svg="http://www.w3.org/2000/svg"><defs
|
|
13
|
+
id="defs2"><marker
|
|
14
|
+
style="overflow:visible"
|
|
15
|
+
id="RoundedArrow"
|
|
16
|
+
refX="0"
|
|
17
|
+
refY="0"
|
|
18
|
+
orient="auto-start-reverse"
|
|
19
|
+
markerWidth="6.1347523"
|
|
20
|
+
markerHeight="5.9304953"
|
|
21
|
+
viewBox="0 0 6.1347524 5.9304951"
|
|
22
|
+
preserveAspectRatio="xMidYMid"><path
|
|
23
|
+
transform="scale(0.7)"
|
|
24
|
+
d="m -0.21114562,-4.1055728 6.42229122,3.21114561 a 1,1 90 0 1 0,1.78885438 L -0.21114562,4.1055728 A 1.236068,1.236068 31.717474 0 1 -2,3 v -6 a 1.236068,1.236068 148.28253 0 1 1.78885438,-1.1055728 z"
|
|
25
|
+
style="fill:context-stroke;fill-rule:evenodd;stroke:none"
|
|
26
|
+
id="path1367" /></marker></defs><g
|
|
27
|
+
id="layer1"><g
|
|
28
|
+
id="g7187"
|
|
29
|
+
transform="matrix(0.39684999,0,0,0.39684999,24.969975,13.839997)"><g
|
|
30
|
+
id="g7175"><path
|
|
31
|
+
style="fill:#d8a323;fill-opacity:1;stroke-width:0.304001"
|
|
32
|
+
d="m -62.177083,6.35 c 0,0.79375 0,1.0583333 1.322916,1.0583333 H -23.8125 V -21.166667 c 0,-1.5875 -0.264583,-1.852083 -1.852083,-1.852083 H -39.6875 c -1.058333,0 -4.7625,-5.55625 -5.55625,-5.55625 h -15.610417 c -1.322916,0 -1.322916,0 -1.322916,1.058333 z"
|
|
33
|
+
id="path320" /><path
|
|
34
|
+
style="fill:#f4bd36;fill-opacity:1;stroke-width:0.304001"
|
|
35
|
+
d="m -60.854167,7.4082933 c 1.868763,0.173 7.138098,-20.9617993 7.138098,-20.9617993 0.270236,-0.733994 1.328569,-2.321494 3.445236,-2.321494 h 33.602083 c 0,0 2.327335,-0.268719 1.322917,3.175 0,0 0,0 -5.55625,19.05 -0.165655,0.5679614 -0.264584,1.0583333 -2.38125,1.0583333 h -37.570834 z"
|
|
36
|
+
id="path844" /></g><g
|
|
37
|
+
id="g7159"><circle
|
|
38
|
+
style="fill:#7ad32d;fill-opacity:1;stroke-width:0.304001"
|
|
39
|
+
id="path3218"
|
|
40
|
+
cx="-22.489584"
|
|
41
|
+
cy="-3.1789145e-07"
|
|
42
|
+
r="10.583333" /><path
|
|
43
|
+
style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:1.204;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
|
|
44
|
+
d="M -29.104166,2.6458333 V 5.2916666 H -15.875 V 2.6458333"
|
|
45
|
+
id="path4988" /><path
|
|
46
|
+
style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:0.804;stroke-dasharray:none;stroke-opacity:1;marker-start:url(#RoundedArrow)"
|
|
47
|
+
d="M -22.489583,-1.3229167 V -5.2916666"
|
|
48
|
+
id="path6310" /><path
|
|
49
|
+
style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:1.204;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
|
|
50
|
+
d="M -22.489583,0 V -5.2916666"
|
|
51
|
+
id="path6585" /></g></g></g></svg>
|