commit f4b23845b1b8461e3c77ff97333f4448a5ed133b Author: Marc Date: Thu Oct 29 15:34:07 2020 +0100 first diff --git a/README.md b/README.md new file mode 100644 index 0000000..82bfdf4 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ + +## Requirements +pip: +* wheel +* gunicorn +* flask + + +## Unit File +```shell +[Unit] +Description=Gunicorn instance for primemeat +After=network.target + +[Service] +User=maintenance +Group=www-data +WorkingDirectory=/path/to/project +Environment="PATH=/path/to/project/bin" +#remove --reload when site is ready +#reload restarts workers when code changes +ExecStart=/path/to/project/bin/gunicorn --reload --workers 3 --bind 0.0.0.0:8900 wsgi:app + +[Install] +WantedBy=multi-user.target +``` + +## Caddy +```shell +primemeat.some.domain.org { + root * /path/to/project + reverse_proxy localhost:8900 +} +``` diff --git a/backend/project.py b/backend/project.py new file mode 100644 index 0000000..7cd1e18 --- /dev/null +++ b/backend/project.py @@ -0,0 +1,67 @@ +from flask import Flask, request, jsonify +from flask_sqlalchemy import SQLAlchemy +import time + +app = Flask(__name__) +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///primemeat.db" + +# DB +db = SQLAlchemy(app) +class Umsatz(db.Model): + id = db.Column('id', db.Integer, primary_key = True) + monat = db.Column(db.String(7)) + wert = db.Column(db.Integer) + + def __init__(self, monat, wert): + self.monat = monat + self.wert = wert + +@app.route("/") +def hello(): + db.create_all() + return "

Hello, Prime Meat!

" + +@app.route('/time') +def get_current_time(): + return jsonify({'time': time.time()}) + +@app.route("/api/umsatz", methods=["GET", "POST", "DELETE"]) +def umsatz(): + method = request.method + if (method.lower() == "get"): + umsatz = Umsatz.query.all() + return jsonify([{"id": i.id, "monat": i.monat, "wert": i.wert} for i in umsatz]) + elif (method.lower() == "post"): + try: + monat = request.json["monat"] + wert = request.json["wert"] + if (monat and wert): + try: + umsatz = Umsatz(monat, wert) + db.session.add(umsatz) + db.session.commit() + return jsonify({"success": True}) + except Exception as e: + return ({"error": e}) + else: + return jsonify({"error": "Invalid form"}) + except: + return jsonify({"error": "Invalid form"}) + elif (method.lower() == "delete"): + try: + uid = request.json["id"] + if (uid): + try: + umsatz = Umsatz.query.get(uid) + db.session.delete(umsatz) + db.session.commit() + return jsonify({"success": True}) + except Exception as e: + return jsonify({"error": e}) + else: + return jsonify({"error": "Invalid form"}) + except: + return jsonify({"error": "m"}) + +if __name__ == "__main__": + app.run(host='0.0.0.0') diff --git a/backend/wsgi.py b/backend/wsgi.py new file mode 100644 index 0000000..f664a1d --- /dev/null +++ b/backend/wsgi.py @@ -0,0 +1,5 @@ +from project import app + +if __name__ == "__main__": + app.run() +