wireguard_peer_manager/kiss/toml-manager/controllers/toml_controller.py

36 lines
1.3 KiB
Python
Raw Normal View History

import cherrypy
from services.topic_service import TopicService
from auth.token_auth import validate_token
class GeneralTOMLController:
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def index(self):
if cherrypy.request.method != "POST":
raise cherrypy.HTTPError(405, "Method Not Allowed. Use POST.")
auth_header = cherrypy.request.headers.get('Authorization')
encrypted_data = cherrypy.request.body.read()
try:
decrypted_data = validate_token(auth_header, encrypted_data)
action = decrypted_data.get('action')
file_path = decrypted_data.get('file_path')
# Handle any TOML file actions
if action == 'read_file':
config = read_toml_file(file_path)
return {"content": config}
elif action == 'write_file':
data_to_write = decrypted_data.get('data')
write_toml_file(file_path, data_to_write)
return {"message": f"File {file_path} updated successfully"}
else:
return {"error": "Invalid action"}
except Exception as e:
cherrypy.log(f"Error processing request: {str(e)}")
return {"error": "Invalid request"}, 400