Home

tailpolicy @dec30a19919334b7e74dde7b9382746a05b01f69 - refs - log -
-
https://git.jolheiser.com/tailpolicy.git
Tailscale policy editor on your tailnet
tailpolicy / static / index.tmpl
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>tailpolicy</title>
  <link rel="stylesheet" href="/tailwind.css" />
  <link rel="stylesheet" href="/prism.css" />
  <link rel="stylesheet" href="/ctp.css" />
</head>

<body class="latte dark:mocha bg-base/50 dark:bg-base/95 text-text">
  <div class="flex h-screen">
    <div class="flex-1 flex flex-col p-4">
      <h2 class="text-2xl font-bold mb-4">Jsonnet <button id="save" class="text-sm p-1 border rounded bg-base dark:bg-base/50 hover:not-disabled:bg-base/90 dark:hover:not-disabled:bg-surface0 disabled:text-subtext0 disabled:border-overlay0 disabled:bg-base/50 disabled:text-surface0" disabled>Save ACL</button></h2>
      <textarea id="input" name="content" class="bg-base dark:bg-base/50 flex-1 p-2 border rounded">{{ .content
        }}</textarea>
    </div>
    <div class="flex-1 flex flex-col p-4">
      <h2 class="text-2xl font-bold mb-4">Policy</h2>
      <pre class="bg-base dark:bg-base/50 flex-1 p-2 border rounded"><code id="output" class="language-json5"></code>
      </pre>
    </div>
  </div>

  <script src="/prism.js" data-manual></script>
  <script>
    const $inputText = document.getElementById('input');
    const $outputText = document.getElementById('output');
    const $saveButton = document.getElementById('save');
    let content = "{{ .content }}";

    $inputText.addEventListener("input", () => {
      convert();
      $saveButton.disabled = $inputText.value === content;
    });
    $inputText.addEventListener("keydown", (event) => {
      if (event.keyCode == 9) {
        event.preventDefault();
        const start = this.selectionStart;
        const end = this.selectionEnd;
        this.value = this.value.substring(0, start) + "  " + this.value.substring(end);
        this.selectionStart = this.selectionEnd = start + 2;
      }
    });
    $saveButton.addEventListener("click", () => {
      const form = new FormData();
      form.append("content", $inputText.value);
      fetch('/', {
        method: 'POST',
        body: form,
      })
      .then(response => response.text())
      .then(data => {
        $inputText.value = data;
        content = data;
        $saveButton.disabled = true;
      });
    });

    if ($inputText.value !== "") convert();

    function convert() {
      fetch('/preview', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          input: $inputText.value,
        }),
      })
        .then(response => response.text())
        .then(data => {
          $outputText.innerHTML = data;
          Prism.highlightElement($outputText);
        })
        .catch(error => {
          console.error('Error:', error);
          $outputText.innerHTML = 'An error occurred during conversion.';
        });
    }

    const error = "{{ .error }}";
    if (error !== "") alert(error);
  </script>
</body>

</html>