Update main.py

This commit is contained in:
Alexandr Bogomyakov
2023-10-31 04:51:57 +02:00
committed by GitHub
parent 7dd5ad2703
commit d4071c2c9b

17
main.py
View File

@@ -10,6 +10,7 @@ from lib import Server
logging.getLogger('werkzeug').setLevel(logging.ERROR) logging.getLogger('werkzeug').setLevel(logging.ERROR)
CFG_PATH = '/usr/local/etc/outfleet'
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
@@ -42,10 +43,10 @@ def update_state():
CLIENTS = dict() CLIENTS = dict()
config = dict() config = dict()
try: try:
with open("config.yaml", "r") as file: with open(CFG_PATH, "r") as file:
config = yaml.safe_load(file) config = yaml.safe_load(file)
except: except:
with open("config.yaml", "w"): with open(CFG_PATH, "w"):
pass pass
if config: if config:
@@ -110,7 +111,7 @@ def clients():
def add_server(): def add_server():
if request.method == 'POST': if request.method == 'POST':
try: try:
with open("config.yaml", "r") as file: with open(CFG_PATH, "r") as file:
config = yaml.safe_load(file) or {} config = yaml.safe_load(file) or {}
servers = config.get('servers', dict()) servers = config.get('servers', dict())
@@ -124,7 +125,7 @@ def add_server():
'cert': request.form['cert'] 'cert': request.form['cert']
} }
config["servers"] = servers config["servers"] = servers
with open("config.yaml", "w") as file: with open(CFG_PATH, "w") as file:
yaml.safe_dump(config, file) yaml.safe_dump(config, file)
log.info("Added server: %s", new_server.data["name"]) log.info("Added server: %s", new_server.data["name"])
update_state() update_state()
@@ -136,7 +137,7 @@ def add_server():
@app.route('/add_client', methods=['POST']) @app.route('/add_client', methods=['POST'])
def add_client(): def add_client():
if request.method == 'POST': if request.method == 'POST':
with open("config.yaml", "r") as file: with open(CFG_PATH, "r") as file:
config = yaml.safe_load(file) or {} config = yaml.safe_load(file) or {}
clients = config.get('clients', dict()) clients = config.get('clients', dict())
@@ -148,7 +149,7 @@ def add_client():
'servers': request.form.getlist('servers') 'servers': request.form.getlist('servers')
} }
config["clients"] = clients config["clients"] = clients
with open("config.yaml", "w") as file: with open(CFG_PATH, "w") as file:
yaml.safe_dump(config, file) yaml.safe_dump(config, file)
log.info("Client %s updated", request.form.get('name')) log.info("Client %s updated", request.form.get('name'))
@@ -181,7 +182,7 @@ def add_client():
@app.route('/del_client', methods=['POST']) @app.route('/del_client', methods=['POST'])
def del_client(): def del_client():
if request.method == 'POST': if request.method == 'POST':
with open("config.yaml", "r") as file: with open(CFG_PATH, "r") as file:
config = yaml.safe_load(file) or {} config = yaml.safe_load(file) or {}
clients = config.get('clients', dict()) clients = config.get('clients', dict())
@@ -193,7 +194,7 @@ def del_client():
server.delete_key(client.key_id) server.delete_key(client.key_id)
config["clients"].pop(user_id) config["clients"].pop(user_id)
with open("config.yaml", "w") as file: with open(CFG_PATH, "w") as file:
yaml.safe_dump(config, file) yaml.safe_dump(config, file)
log.info("Deleting client %s", request.form.get('name')) log.info("Deleting client %s", request.form.get('name'))
update_state() update_state()