webvars/app.py
2023-08-22 19:20:26 +02:00

38 lines
1.6 KiB
Python

from flask import Flask, render_template, url_for, request, redirect, Response, abort, session
from flask_sqlalchemy import SQLAlchemy
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
app.secret_key = 'jf090vmj0r2jr0vwrjwlk3j09520jwn0v0'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///webvars.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Webvar(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
namespace = db.Column(db.String(64), nullable=False)
project = db.Column(db.String(64), nullable=False)
name = db.Column(db.String(64), nullable=False)
value = db.Column(db.String(64), nullable=True)
@app.route('/<namespace>/<project>/<name>', defaults={ "value": None }, methods=['GET'])
@app.route('/<namespace>/<project>/<name>/<value>', methods=['GET'])
def index(namespace,project,name,value):
db.create_all()
vars = Webvar()
if value != None:
Webvar.query.filter(Webvar.namespace == namespace).filter(Webvar.project == project).filter(Webvar.name == name).delete()
vars.namespace = namespace
vars.project = project
vars.name = name
vars.value = value
db.session.add(vars)
db.session.commit()
v = Webvar.query.filter(Webvar.namespace == namespace).filter(Webvar.project == project).filter(Webvar.name == name).first()
return v.value, '200', {'Content-Type': 'text/plain; charset=utf-8'}
if __name__ == "__main__":
app.run(debug=True)