-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (39 loc) · 1.25 KB
/
Copy pathapp.py
File metadata and controls
49 lines (39 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, render_template, request, jsonify
import json
import os
import shutil
app = Flask(__name__)
CONFIG_FILE = "D:/CODING/Programming Languages/JSON/config.json"
BACKUP_FILE = CONFIG_FILE + ".bak"
def read_config():
with open(CONFIG_FILE, "r") as file:
return json.load(file)
def write_config(config):
with open(CONFIG_FILE, "w") as file: # Corrected line
json.dump(config, file, indent=4)
def backup_config():
shutil.copy(CONFIG_FILE, BACKUP_FILE)
@app.route("/")
def index():
config = read_config()
return render_template("index.html", config=config)
@app.route("/update", methods=["POST"])
def update():
data = request.json
environment = data.get("environment")
section = data.get("section")
key = data.get("key")
value = data.get("value")
config = read_config()
if environment in config and section in config[environment]:
config[environment][section][key] = value
backup_config()
write_config(config)
return jsonify({"status": "success"})
return jsonify({"status": "error"}), 400
@app.route("/backup", methods=["GET"])
def backup():
backup_config()
return jsonify({"status": "success"})
if __name__ == "__main__":
app.run(debug=True)