commit e9ffb5d3e3a7f48b4a42980e682b21c4839e2afc Author: Francesco Luzzana Date: Sat Mar 12 21:50:58 2022 +0100 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9011c2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +venv/ + +*.pyc +__pycache__/ +.ipynb_checkpoints + +instance/ + +.pytest_cache/ +.coverage +htmlcov/ + +dist/ +build/ +*.egg-info/ + +.env \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0ceb6db --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "black" +} diff --git a/config.py b/config.py new file mode 100644 index 0000000..745f39b --- /dev/null +++ b/config.py @@ -0,0 +1,18 @@ +import os + + +class Config(object): + DEBUG = False + TESTING = False + URL_PREFIX = "" + + +class ProductionConfig(Config): + DEBUG = False + URL_PREFIX = os.environ.get("URL_PREFIX") + + +class DevelopmentConfig(Config): + ENV = "development" + DEVELOPMENT = True + DEBUG = True diff --git a/postit/__init__.py b/postit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/postit/app.py b/postit/app.py new file mode 100644 index 0000000..e056e48 --- /dev/null +++ b/postit/app.py @@ -0,0 +1,36 @@ +import os +from flask import Flask, send_from_directory +from . import prefix + + +def create_app(test_config=None): + # Create and configure the Flask App + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping( + SECRET_KEY="dev", + ) + + if test_config is None: + # load the instance config, if it exists, when not testing + app.config.from_pyfile("config.py", silent=True) + else: + # load the test config if passed in + app.config.from_mapping(test_config) + + # ensure the instance folder exists + try: + os.makedirs(app.instance_path) + except OSError: + pass + + @app.route("/favicon.ico") + def favicon(): + return send_from_directory( + os.path.join(app.root_path, "static"), + "favicon.ico", + mimetype="image/vnd.microsoft.icon", + ) + + app.wsgi_app = prefix.PrefixMiddleware( + app.wsgi_app, prefix=os.environ.get("URL_PREFIX", "") + ) diff --git a/postit/contents.py b/postit/contents.py new file mode 100644 index 0000000..e646a73 --- /dev/null +++ b/postit/contents.py @@ -0,0 +1,23 @@ +import os +from shutil import rmtree +from flask import Blueprint, render_template, request, url_for +from . import git + +repo_url = os.environ.get("REPO_URL") +repo_path = os.environ.get("REPO_PATH") + + +bp = Blueprint("contents", __name__, url_prefix="/contents") + + +@bp.route("/", method=("GET", "POST")) +def contents(): + if request.method == "POST": + print(request) + if request.form.get("secret") == os.environ.get("REPO_SECRET"): + print("Updating the contents!") + print("Cleaning the folder") + rmtree(repo_path) + print("Cloning the repo") + git.clone_repo(repo_url, repo_path) + return "Hello" diff --git a/postit/git.py b/postit/git.py new file mode 100644 index 0000000..11c2654 --- /dev/null +++ b/postit/git.py @@ -0,0 +1,5 @@ +from pygit2 import clone_repository + + +def clone_repo(repo_url, repo_path): + repo = clone_repository(repo_url, repo_path) diff --git a/postit/home.py b/postit/home.py new file mode 100644 index 0000000..06096c8 --- /dev/null +++ b/postit/home.py @@ -0,0 +1,8 @@ +from flask import Blueprint, render_template, request, url_for + +bp = Blueprint("home", __name__, url_prefix="/") + + +@bp.route("/") +def home(): + return "Home" diff --git a/postit/prefix.py b/postit/prefix.py new file mode 100644 index 0000000..a94e608 --- /dev/null +++ b/postit/prefix.py @@ -0,0 +1,14 @@ +class PrefixMiddleware(object): + def __init__(self, app, prefix=""): + self.app = app + self.prefix = prefix + + def __call__(self, environ, start_response): + + if environ["PATH_INFO"].startswith(self.prefix): + environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix) :] + environ["SCRIPT_NAME"] = self.prefix + return self.app(environ, start_response) + else: + start_response("404", [("Content-Type", "text/plain")]) + return ["This url does not belong to the app.".encode()] diff --git a/postit/static/favicon.ico b/postit/static/favicon.ico new file mode 100644 index 0000000..e6a3bb9 Binary files /dev/null and b/postit/static/favicon.ico differ diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..dbd4a75 --- /dev/null +++ b/setup.py @@ -0,0 +1,10 @@ +from setuptools import find_packages, setup + +setup( + name="postit", + version="1.0.0", + packages=find_packages(), + include_package_data=True, + zip_safe=False, + install_requires=["flask", "python-dotenv", "pygit2"], +)