delete duplicate caddy conf
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
# myapp/views/vscode.py
|
# myapp/views/vscode.py
|
||||||
|
import re
|
||||||
import os, uuid, json
|
import os, uuid, json
|
||||||
from flask import Blueprint, current_app, session, redirect, url_for, render_template, request, jsonify
|
from flask import Blueprint, current_app, session, redirect, url_for, render_template, request, jsonify
|
||||||
from ..services.backboard_service import realtime_response
|
from ..services.backboard_service import realtime_response
|
||||||
@@ -39,21 +40,32 @@ def desktop(user_uuid, course_id, chapter_name, lesson_name):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
caddy_conf = f"""
|
caddy_conf = f"""
|
||||||
handle_path /vsc{user_uuid}/* {{
|
handle_path /vsc{user_uuid}/* {{
|
||||||
reverse_proxy localhost:{code_server_port}
|
reverse_proxy localhost:{code_server_port}
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
caddy_config_path = "/etc/caddy/Caddyfile"
|
caddy_config_path = "/etc/caddy/Caddyfile"
|
||||||
with open(caddy_config_path, "r") as f:
|
with open(caddy_config_path, "r") as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
insert_position = len(lines) - 2 if len(lines) >= 2 else len(lines)
|
pattern = re.compile(
|
||||||
|
r'handle_path\s+/vsc' + re.escape(user_uuid) + r'/\* {.*?reverse_proxy.*?}',
|
||||||
|
re.DOTALL
|
||||||
|
)
|
||||||
|
content = "".join(lines)
|
||||||
|
if pattern.search(content):
|
||||||
|
new_content = pattern.sub('', content)
|
||||||
|
lines = new_content.splitlines(keepends=True)
|
||||||
|
if len(lines) >= 2:
|
||||||
|
insert_position = len(lines) - 2
|
||||||
|
else:
|
||||||
|
insert_position = len(lines)
|
||||||
|
if caddy_conf[-1] not in ['\n', '\r']:
|
||||||
|
caddy_conf += '\n'
|
||||||
lines.insert(insert_position, caddy_conf)
|
lines.insert(insert_position, caddy_conf)
|
||||||
with open(caddy_config_path, "w") as f:
|
with open(caddy_config_path, "w") as f:
|
||||||
f.writelines(lines)
|
f.writelines(lines)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(["sudo", "caddy", "reload", "--config", "/etc/caddy/Caddyfile"])
|
subprocess.run(["sudo", "caddy", "reload", "--config", "/etc/caddy/Caddyfile"])
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user