From: Brendan Hansen Date: Sat, 12 Mar 2022 22:03:02 +0000 (-0600) Subject: added permalinked files X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=70becad75f52d9250c7882f343a4a9319e59e56f;p=onyx-live.git added permalinked files --- diff --git a/clean_permalinked.py b/clean_permalinked.py new file mode 100644 index 0000000..71b2903 --- /dev/null +++ b/clean_permalinked.py @@ -0,0 +1,6 @@ +import os, time + +for f in os.listdir("./permalinked"): + full_path = os.path.join("./permalinked", f) + if os.path.getctime(full_path) + 60 * 60 * 24 * 60 < time.gmtime(): + os.remove(full_path) diff --git a/serve.py b/serve.py index c4a89d4..30c13ed 100644 --- a/serve.py +++ b/serve.py @@ -15,7 +15,15 @@ def require_coep(response): @app.route("/") def get_homepage(): - response = flask.Response(flask.render_template("index.html")) + code_body = """#load "core/std" + +use package core + +main :: (args: [] cstr) { + println("Hello, Onyx!"); +}""" + + response = flask.Response(flask.render_template("index.html", code_body=code_body)) response.headers['Cross-Origin-Embedder-Policy'] = "require-corp" response.headers['Cross-Origin-Opener-Policy'] = "same-origin" return response @@ -68,3 +76,34 @@ def post_compile_onyx(): else: return compile_process.stdout, 500 +@app.route("/permalink", methods=['POST']) +def create_permalink(): + import tempfile, os + + code = request.json['code'] + code_file_desc, code_file_name = tempfile.mkstemp(prefix="", dir="./permalinked") + with os.fdopen(code_file_desc, "w+b") as code_file: + code_file.write(code.encode('utf-8')) + + _, link_id = os.path.split(code_file_name) + + response = {} + response["id"] = link_id + response["url"] = app.config["DOMAIN_NAME"] + "/" + link_id + return response, 200 + +@app.route("/") +def get_permalinked(link): + import os + path = os.path.join("./permalinked", link) + + code_body = "" + if os.path.isfile(path): + with open(path) as f: + code_body = f.read() + + response = flask.Response(flask.render_template("index.html", code_body=code_body)) + response.headers['Cross-Origin-Embedder-Policy'] = "require-corp" + response.headers['Cross-Origin-Opener-Policy'] = "same-origin" + return response + diff --git a/static/css/index.css b/static/css/index.css index 3fb2677..f6f4c83 100644 --- a/static/css/index.css +++ b/static/css/index.css @@ -212,6 +212,11 @@ select:hover { cursor: pointer; } +.modal button:disabled { + background: var(--dark-background-color); + cursor: not-allowed; +} + .modal .file-list { max-height: 400px; overflow: auto; diff --git a/static/src/index.js b/static/src/index.js index 51b7f86..6c7e87e 100644 --- a/static/src/index.js +++ b/static/src/index.js @@ -260,6 +260,27 @@ function load_example() { }); } +async function request_permalink() { + let editor = ace.edit('code-editor'); + let code = editor.getValue(); + + let response = await fetch(window.ROOT_ENDPOINT + "/permalink", { + method: 'POST', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ code }), + }); + + if (response.status == 200) { + let res = await response.json(); + console.log(res); + $("#permalink").html(`Permalink created: ${res.url}`); + $("#permalink + button").prop("disabled", "true"); + } +} + window.onload = () => { let editor = ace.edit('code-editor'); diff --git a/templates/index.html b/templates/index.html index 7802ae5..e90fc57 100644 --- a/templates/index.html +++ b/templates/index.html @@ -34,14 +34,7 @@
-
-#load "core/std"
-
-use package core
-
-main :: (args: [] cstr) {
-    println("Hello, Onyx!");
-}
+
{{ code_body }}
@@ -97,7 +90,14 @@ main :: (args: [] cstr) {