Skip to content

Commit e55fd05

Browse files
committed
add setup script to only replace values in template html once
1 parent 571c145 commit e55fd05

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

main.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ class CApp(FastAPI):
3939

4040
LETTERS_AND_DIGITS = ascii_letters + digits
4141

42-
VSCODE_DOMAIN = os.getenv("VSCODE_DOMAIN")
43-
if VSCODE_DOMAIN is None:
44-
exit("VSCODE_DOMAIN is not set")
45-
46-
ROOT_DOMAIN = f".{VSCODE_DOMAIN.split('.')[-2]}.{VSCODE_DOMAIN.split('.')[-1]}"
4742
EXPIRE_TIME = int(os.getenv("EXPIRE_TIME", "30"))
4843

4944
SOCKET_FOLDER = os.getenv("SOCKET_FOLDER", "/run/code_server_sockets")
@@ -131,11 +126,7 @@ async def callback(code: str):
131126
await routes.write(json.dumps(socket_paths))
132127

133128
# redirect user to code-server
134-
return HTMLResponse(
135-
TEMPLATE_HTML.replace("%pls-replace-me%", session_id)
136-
.replace("%root_domain%", ROOT_DOMAIN)
137-
.replace("%vscode_domain%", VSCODE_DOMAIN)
138-
)
129+
return HTMLResponse(TEMPLATE_HTML.replace("%pls-replace-me%", session_id))
139130

140131

141132
@app.get("/oauth2/callback/reset_session")

modules/server_starter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ async def start_code_server(user: str, expire_time: int):
2323

2424
socket_path = f"{SOCKET_FOLDER}/{user}_code_server.sock"
2525

26+
Popen(["sudo", "rm", "-f", socket_path])
27+
2628
Popen(["sudo", "runuser", "-l", user, "-c", f"code-server --socket {socket_path}"])
2729

2830
while True:

response.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta http-equiv="X-UA-Compatible" content="IE=edge">
67
<meta name="viewport" content="width=device-width, initial-scale=1.0">
78
<meta id="session_id" content="%pls-replace-me%">
89
<title>Intermission</title>
910
</head>
11+
1012
<body onload="load()">
1113
<script>
1214
function setCookie() {
1315
const session_id = document.getElementById("session_id").getAttribute("content");
1416
const date = new Date();
1517
const expire_days = 2;
1618
date.setTime(date.getTime() + (expire_days * 24 * 60 * 60 * 1000));
17-
let expires = "expires="+ date.toUTCString();
19+
const expires = "expires=" + date.toUTCString();
1820
document.cookie = "session_id=" + session_id + ";" + expires + ";domain=%root_domain%;path=/";
1921
}
2022

@@ -27,6 +29,7 @@
2729
redirectUser();
2830
}
2931
</script>
30-
32+
3133
</body>
32-
</html>
34+
35+
</html>

setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
with open("response.html", "r+", encoding="utf8") as template_file:
2+
root_domain = "." + input("Enter your root domain (ex: tobycm.ga): ")
3+
vscode_domain = input(f"Enter your VS Code domain (default: vscode{root_domain}): ")
4+
if vscode_domain == "":
5+
vscode_domain = f"vscode{root_domain}"
6+
7+
template_file.write(
8+
template_file.read()
9+
.replace("%root_domain%", root_domain)
10+
.replace("%vscode_domain%", vscode_domain)
11+
)

0 commit comments

Comments
 (0)