99 lines
4.1 KiB
JavaScript
99 lines
4.1 KiB
JavaScript
import { h, PageHeader, Empty, Table, renderGuard, esc, enc, $val, apiFetch, toast, openModal, closeModal, formModal, definePage, getModel, modelFetch, ActionCell, certStatusBadge, poll } from '/static/hoover/index.js?v=7';
|
|
|
|
function issueCertModal(state) {
|
|
openModal((inner, idx) => {
|
|
formModal(inner, 'Issue Certificate',
|
|
[
|
|
{ label: 'Domain', id: 'ic-domain', placeholder: 'example.com' },
|
|
{ label: 'Email (optional)', id: 'ic-email', placeholder: 'you@example.com' },
|
|
],
|
|
[
|
|
{ label: 'Cancel', cls: 'btn-outline', action: 'c', handler: () => closeModal(idx) },
|
|
{
|
|
label: 'Issue', cls: 'btn-primary', action: 's', handler: async () => {
|
|
const domain = ($val('ic-domain') || '').trim();
|
|
if (!domain) { toast('Domain is required', 'error'); return; }
|
|
const body = { domain, email: ($val('ic-email') || '').trim() || undefined };
|
|
const resp = await apiFetch('/api/certs/issue/start', {
|
|
method: 'POST',
|
|
body,
|
|
});
|
|
if (resp.ok) {
|
|
toast('Issuance started for ' + domain, 'success');
|
|
closeModal(idx);
|
|
const rid = resp.data?.request_id;
|
|
if (rid) pollCertIssue(rid, state);
|
|
} else {
|
|
toast(resp.error || 'Failed', 'error');
|
|
}
|
|
},
|
|
},
|
|
],
|
|
);
|
|
});
|
|
}
|
|
|
|
async function pollCertIssue(rid, state) {
|
|
poll({
|
|
url: '/api/certs/issue/' + enc(rid),
|
|
successKey: (d) => d.status === 'completed',
|
|
onErrorKey: (d) => d.status === 'failed',
|
|
onComplete: (d) => {
|
|
toast('Certificate issued for ' + (d.domain || rid), 'success');
|
|
modelFetch('acme');
|
|
},
|
|
onError: (d) => {
|
|
toast('Issuance failed: ' + (d?.error || 'unknown'), 'error');
|
|
},
|
|
});
|
|
}
|
|
|
|
export default definePage({
|
|
init() {
|
|
return {
|
|
acme: getModel('acme'),
|
|
};
|
|
},
|
|
render(state) {
|
|
const guard = renderGuard(state.acme, 'Certificates', 'ACME certificate management', state.acme.data);
|
|
if (guard) return guard;
|
|
|
|
const rows = (state.acme.data?.certs || []).map(c => {
|
|
const badge = certStatusBadge({ expired: c.expired, daysRemaining: c.days_remaining });
|
|
|
|
return h('tr', { key: c.domain },
|
|
h('td', null, h('strong', null, esc(c.domain || 'unknown'))),
|
|
h('td', { class: 'text-sm' }, esc(c.issuer || '-')),
|
|
h('td', null, esc(c.expiry || 'N/A')),
|
|
h('td', null, badge),
|
|
ActionCell({
|
|
editLabel: 'Renew',
|
|
editClick: async () => {
|
|
const resp = await apiFetch('/api/certs/' + enc(c.domain) + '/renew', { method: 'POST' });
|
|
if (resp.ok) toast('Renewal started for ' + c.domain, 'success');
|
|
else toast(resp.error || 'Failed', 'error');
|
|
},
|
|
removeUrl: '/api/certs/' + enc(c.domain),
|
|
removeMessage: 'Remove certificate for ' + c.domain + '?',
|
|
removeSuccess: 'Certificate removed',
|
|
removeRefresh: 'acme',
|
|
}),
|
|
);
|
|
});
|
|
|
|
return [
|
|
PageHeader({
|
|
title: 'Certificates',
|
|
subtitle: 'ACME certificate management',
|
|
actions: h('button', { class: 'btn btn-primary',
|
|
'on:click': () => issueCertModal(state) }, 'Issue Certificate'),
|
|
}),
|
|
rows.length
|
|
? Table({
|
|
columns: ['Domain', 'Issuer', 'Expiry', 'Status', 'Actions'],
|
|
rows,
|
|
})
|
|
: Empty({ text: 'No certificates found. Issue a certificate to get started.' }),
|
|
];
|
|
},
|
|
}); |