move templates to speparate file
This commit is contained in:
190
internal/web/templates.go
Normal file
190
internal/web/templates.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
)
|
||||
|
||||
var indexTemplate = template.Must(template.New("index").Parse(`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>luxtools-client</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; margin: 1.25rem; }
|
||||
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
|
||||
table { border-collapse: collapse; }
|
||||
th, td { border-bottom: 1px solid #ddd; padding: 0.4rem 0.6rem; text-align: left; vertical-align: top; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>luxtools-client</h1>
|
||||
|
||||
<h2>Endpoints</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>Path</th><th>Methods</th><th>Description</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{- range .Endpoints }}
|
||||
<tr>
|
||||
<td><a href="{{ .Path }}"><code>{{ .Path }}</code></a></td>
|
||||
<td><code>{{ .Methods }}</code></td>
|
||||
<td>{{ .Description }}</td>
|
||||
</tr>
|
||||
{{- end }}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>Info</h2>
|
||||
<pre>{{ .InfoJSON }}</pre>
|
||||
</body>
|
||||
</html>
|
||||
`))
|
||||
|
||||
var settingsTemplate = template.Must(template.New("settings").Parse(`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>luxtools-client Settings</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; margin: 1.25rem; }
|
||||
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
|
||||
table { border-collapse: collapse; width: 100%; max-width: 900px; }
|
||||
th, td { border-bottom: 1px solid #ddd; padding: 0.4rem 0.6rem; text-align: left; vertical-align: top; }
|
||||
input[type=text] { width: 100%; box-sizing: border-box; padding: 0.35rem; }
|
||||
button { padding: 0.35rem 0.7rem; }
|
||||
.small { color: #666; font-size: 0.9rem; }
|
||||
#status { margin-top: 0.75rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Path Aliases</h1>
|
||||
<p class="small">Define aliases like <code>PROJECTS</code> -> <code>/mnt/projects</code>. Use in <code>/open</code> as <code>PROJECTS>my/repo</code>.</p>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th style="width: 30%">Alias</th><th>Path</th><th style="width: 6rem"></th></tr>
|
||||
</thead>
|
||||
<tbody id="mapBody"></tbody>
|
||||
</table>
|
||||
<div style="margin-top: 0.75rem;">
|
||||
<button id="addRow">Add Alias</button>
|
||||
<button id="save">Save</button>
|
||||
</div>
|
||||
<div id="status" class="small"></div>
|
||||
|
||||
<script>
|
||||
const bodyEl = document.getElementById('mapBody');
|
||||
const statusEl = document.getElementById('status');
|
||||
|
||||
function setStatus(msg, isError) {
|
||||
statusEl.textContent = msg || '';
|
||||
statusEl.style.color = isError ? '#b00020' : '#00796b';
|
||||
}
|
||||
|
||||
function addRow(alias = '', path = '') {
|
||||
const tr = document.createElement('tr');
|
||||
const aliasTd = document.createElement('td');
|
||||
const pathTd = document.createElement('td');
|
||||
const actionTd = document.createElement('td');
|
||||
|
||||
const aliasInput = document.createElement('input');
|
||||
aliasInput.type = 'text';
|
||||
aliasInput.value = alias;
|
||||
aliasInput.placeholder = 'ALIAS';
|
||||
aliasTd.appendChild(aliasInput);
|
||||
|
||||
const pathInput = document.createElement('input');
|
||||
pathInput.type = 'text';
|
||||
pathInput.value = path;
|
||||
pathInput.placeholder = 'Absolute path';
|
||||
pathTd.appendChild(pathInput);
|
||||
|
||||
const removeBtn = document.createElement('button');
|
||||
removeBtn.textContent = 'Remove';
|
||||
removeBtn.addEventListener('click', () => tr.remove());
|
||||
actionTd.appendChild(removeBtn);
|
||||
|
||||
tr.appendChild(aliasTd);
|
||||
tr.appendChild(pathTd);
|
||||
tr.appendChild(actionTd);
|
||||
bodyEl.appendChild(tr);
|
||||
}
|
||||
|
||||
function isAbsolutePath(p) {
|
||||
if (!p) return false;
|
||||
if (p.startsWith('/') || p.startsWith('\\\\') || p.startsWith('//')) return true;
|
||||
return /^[A-Za-z]:[\\/]/.test(p) || p.toLowerCase().startsWith('file://');
|
||||
}
|
||||
|
||||
function collect() {
|
||||
const rows = Array.from(bodyEl.querySelectorAll('tr'));
|
||||
const map = {};
|
||||
for (const row of rows) {
|
||||
const alias = row.children[0].querySelector('input').value.trim();
|
||||
const path = row.children[1].querySelector('input').value.trim();
|
||||
if (!alias && !path) continue;
|
||||
if (!alias) throw new Error('Alias is required');
|
||||
if (alias.includes('>')) throw new Error('Alias must not contain ">"');
|
||||
if (!path) throw new Error('Path is required');
|
||||
if (!isAbsolutePath(path)) throw new Error('Path must be absolute');
|
||||
if (map[alias]) throw new Error('Duplicate alias: ' + alias);
|
||||
map[alias] = path;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
async function loadConfig() {
|
||||
setStatus('Loading...', false);
|
||||
const res = await fetch('/settings/config');
|
||||
const data = await res.json();
|
||||
bodyEl.textContent = '';
|
||||
if (!data.ok) {
|
||||
setStatus(data.message || 'Failed to load config', true);
|
||||
addRow();
|
||||
return;
|
||||
}
|
||||
const keys = Object.keys(data.path_map || {}).sort();
|
||||
if (keys.length === 0) addRow();
|
||||
for (const k of keys) addRow(k, data.path_map[k]);
|
||||
setStatus('Loaded', false);
|
||||
}
|
||||
|
||||
async function saveConfig() {
|
||||
try {
|
||||
const map = collect();
|
||||
setStatus('Saving...', false);
|
||||
const res = await fetch('/settings/config', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ path_map: map })
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!data.ok) {
|
||||
setStatus(data.message || 'Failed to save config', true);
|
||||
return;
|
||||
}
|
||||
setStatus('Saved', false);
|
||||
} catch (err) {
|
||||
setStatus(err.message || 'Validation error', true);
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('addRow').addEventListener('click', () => addRow());
|
||||
document.getElementById('save').addEventListener('click', () => saveConfig());
|
||||
loadConfig();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`))
|
||||
|
||||
// RenderIndex renders the main index page.
|
||||
func RenderIndex(w io.Writer, data any) error {
|
||||
return indexTemplate.Execute(w, data)
|
||||
}
|
||||
|
||||
// RenderSettings renders the settings page.
|
||||
func RenderSettings(w io.Writer) error {
|
||||
return settingsTemplate.Execute(w, nil)
|
||||
}
|
||||
179
main.go
179
main.go
@@ -23,6 +23,7 @@ import (
|
||||
"luxtools-client/internal/installer"
|
||||
"luxtools-client/internal/notify"
|
||||
"luxtools-client/internal/openfolder"
|
||||
"luxtools-client/internal/web"
|
||||
)
|
||||
|
||||
var version = "dev"
|
||||
@@ -38,180 +39,6 @@ func register(mux *http.ServeMux, docs *[]endpointDoc, path, methods, descriptio
|
||||
*docs = append(*docs, endpointDoc{Path: path, Methods: methods, Description: description})
|
||||
}
|
||||
|
||||
var indexTemplate = template.Must(template.New("index").Parse(`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>luxtools-client</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; margin: 1.25rem; }
|
||||
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
|
||||
table { border-collapse: collapse; }
|
||||
th, td { border-bottom: 1px solid #ddd; padding: 0.4rem 0.6rem; text-align: left; vertical-align: top; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>luxtools-client</h1>
|
||||
|
||||
<h2>Endpoints</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>Path</th><th>Methods</th><th>Description</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{- range .Endpoints }}
|
||||
<tr>
|
||||
<td><a href="{{ .Path }}"><code>{{ .Path }}</code></a></td>
|
||||
<td><code>{{ .Methods }}</code></td>
|
||||
<td>{{ .Description }}</td>
|
||||
</tr>
|
||||
{{- end }}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>Info</h2>
|
||||
<pre>{{ .InfoJSON }}</pre>
|
||||
</body>
|
||||
</html>
|
||||
`))
|
||||
|
||||
var settingsTemplate = template.Must(template.New("settings").Parse(`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>luxtools-client Settings</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; margin: 1.25rem; }
|
||||
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
|
||||
table { border-collapse: collapse; width: 100%; max-width: 900px; }
|
||||
th, td { border-bottom: 1px solid #ddd; padding: 0.4rem 0.6rem; text-align: left; vertical-align: top; }
|
||||
input[type=text] { width: 100%; box-sizing: border-box; padding: 0.35rem; }
|
||||
button { padding: 0.35rem 0.7rem; }
|
||||
.small { color: #666; font-size: 0.9rem; }
|
||||
#status { margin-top: 0.75rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Path Aliases</h1>
|
||||
<p class="small">Define aliases like <code>PROJECTS</code> -> <code>/mnt/projects</code>. Use in <code>/open</code> as <code>PROJECTS>my/repo</code>.</p>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th style="width: 30%">Alias</th><th>Path</th><th style="width: 6rem"></th></tr>
|
||||
</thead>
|
||||
<tbody id="mapBody"></tbody>
|
||||
</table>
|
||||
<div style="margin-top: 0.75rem;">
|
||||
<button id="addRow">Add Alias</button>
|
||||
<button id="save">Save</button>
|
||||
</div>
|
||||
<div id="status" class="small"></div>
|
||||
|
||||
<script>
|
||||
const bodyEl = document.getElementById('mapBody');
|
||||
const statusEl = document.getElementById('status');
|
||||
|
||||
function setStatus(msg, isError) {
|
||||
statusEl.textContent = msg || '';
|
||||
statusEl.style.color = isError ? '#b00020' : '#00796b';
|
||||
}
|
||||
|
||||
function addRow(alias = '', path = '') {
|
||||
const tr = document.createElement('tr');
|
||||
const aliasTd = document.createElement('td');
|
||||
const pathTd = document.createElement('td');
|
||||
const actionTd = document.createElement('td');
|
||||
|
||||
const aliasInput = document.createElement('input');
|
||||
aliasInput.type = 'text';
|
||||
aliasInput.value = alias;
|
||||
aliasInput.placeholder = 'ALIAS';
|
||||
aliasTd.appendChild(aliasInput);
|
||||
|
||||
const pathInput = document.createElement('input');
|
||||
pathInput.type = 'text';
|
||||
pathInput.value = path;
|
||||
pathInput.placeholder = 'Absolute path';
|
||||
pathTd.appendChild(pathInput);
|
||||
|
||||
const removeBtn = document.createElement('button');
|
||||
removeBtn.textContent = 'Remove';
|
||||
removeBtn.addEventListener('click', () => tr.remove());
|
||||
actionTd.appendChild(removeBtn);
|
||||
|
||||
tr.appendChild(aliasTd);
|
||||
tr.appendChild(pathTd);
|
||||
tr.appendChild(actionTd);
|
||||
bodyEl.appendChild(tr);
|
||||
}
|
||||
|
||||
function isAbsolutePath(p) {
|
||||
if (!p) return false;
|
||||
if (p.startsWith('/') || p.startsWith('\\\\') || p.startsWith('//')) return true;
|
||||
return /^[A-Za-z]:[\\/]/.test(p) || p.toLowerCase().startsWith('file://');
|
||||
}
|
||||
|
||||
function collect() {
|
||||
const rows = Array.from(bodyEl.querySelectorAll('tr'));
|
||||
const map = {};
|
||||
for (const row of rows) {
|
||||
const alias = row.children[0].querySelector('input').value.trim();
|
||||
const path = row.children[1].querySelector('input').value.trim();
|
||||
if (!alias && !path) continue;
|
||||
if (!alias) throw new Error('Alias is required');
|
||||
if (alias.includes('>')) throw new Error('Alias must not contain ">"');
|
||||
if (!path) throw new Error('Path is required');
|
||||
if (!isAbsolutePath(path)) throw new Error('Path must be absolute');
|
||||
if (map[alias]) throw new Error('Duplicate alias: ' + alias);
|
||||
map[alias] = path;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
async function loadConfig() {
|
||||
setStatus('Loading...', false);
|
||||
const res = await fetch('/settings/config');
|
||||
const data = await res.json();
|
||||
bodyEl.textContent = '';
|
||||
if (!data.ok) {
|
||||
setStatus(data.message || 'Failed to load config', true);
|
||||
addRow();
|
||||
return;
|
||||
}
|
||||
const keys = Object.keys(data.path_map || {}).sort();
|
||||
if (keys.length === 0) addRow();
|
||||
for (const k of keys) addRow(k, data.path_map[k]);
|
||||
setStatus('Loaded', false);
|
||||
}
|
||||
|
||||
async function saveConfig() {
|
||||
try {
|
||||
const map = collect();
|
||||
setStatus('Saving...', false);
|
||||
const res = await fetch('/settings/config', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ path_map: map })
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!data.ok) {
|
||||
setStatus(data.message || 'Failed to save config', true);
|
||||
return;
|
||||
}
|
||||
setStatus('Saved', false);
|
||||
} catch (err) {
|
||||
setStatus(err.message || 'Validation error', true);
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('addRow').addEventListener('click', () => addRow());
|
||||
document.getElementById('save').addEventListener('click', () => saveConfig());
|
||||
loadConfig();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`))
|
||||
|
||||
type allowList []string
|
||||
|
||||
func (a *allowList) String() string { return strings.Join(*a, ",") }
|
||||
@@ -383,7 +210,7 @@ func main() {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := indexTemplate.Execute(w, data); err != nil {
|
||||
if err := web.RenderIndex(w, data); err != nil {
|
||||
errLog.Printf("/ index-template error=%v", err)
|
||||
}
|
||||
})
|
||||
@@ -400,7 +227,7 @@ func main() {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := settingsTemplate.Execute(w, nil); err != nil {
|
||||
if err := web.RenderSettings(w); err != nil {
|
||||
errLog.Printf("/settings template error=%v", err)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user