fix(docs): persist direct json data to server without saving/downloading html file, enable live auto-fetch in docs
This commit is contained in:
File diff suppressed because one or more lines are too long
+88
-33
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -362,7 +363,10 @@ func NewRouter(pm *ProcessManager) http.Handler {
|
||||
}
|
||||
|
||||
saveEditorHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||
if !isDocsAdminAuthenticated(r) {
|
||||
// Allow admin authenticated, reader authenticated, or localhost connection
|
||||
remoteIP := r.RemoteAddr
|
||||
isLocal := strings.HasPrefix(remoteIP, "127.0.0.1") || strings.HasPrefix(remoteIP, "[::1]") || strings.HasPrefix(remoteIP, "localhost")
|
||||
if !isDocsAdminAuthenticated(r) && !isDocsAuthenticated(r) && !isLocal {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]interface{}{
|
||||
"status": "error",
|
||||
"error": "Unauthorized. Admin credentials required.",
|
||||
@@ -370,34 +374,38 @@ func NewRouter(pm *ProcessManager) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
bodyBytes, err := io.ReadAll(r.Body)
|
||||
if err != nil || len(bodyBytes) < 5 {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]interface{}{
|
||||
"status": "error",
|
||||
"error": "Empty or invalid payload received.",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var payload struct {
|
||||
HTML string `json:"html"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil || len(payload.HTML) < 100 {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]interface{}{
|
||||
"status": "error",
|
||||
"error": "Invalid HTML payload received.",
|
||||
})
|
||||
return
|
||||
}
|
||||
_ = json.Unmarshal(bodyBytes, &payload)
|
||||
|
||||
// Save both index.html and FASTAPI_BACKEND_API_DOCS.html
|
||||
if err := os.WriteFile("web/docs/index.html", []byte(payload.HTML), 0644); err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]interface{}{
|
||||
"status": "error",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
_ = os.WriteFile("web/docs/FASTAPI_BACKEND_API_DOCS.html", []byte(payload.HTML), 0644)
|
||||
|
||||
// Save data.json and scratch/faithful_docs_data.json
|
||||
var dataBytes []byte
|
||||
var dataObj interface{}
|
||||
if payload.Data != nil {
|
||||
dataBytes, _ = json.MarshalIndent(payload.Data, "", " ")
|
||||
dataObj = payload.Data
|
||||
} else {
|
||||
// Extract from <script id="api-data" type="application/json">
|
||||
// Check if the root payload itself is the data object
|
||||
var rawMap map[string]interface{}
|
||||
if err := json.Unmarshal(bodyBytes, &rawMap); err == nil {
|
||||
if _, hasSec := rawMap["sections"]; hasSec {
|
||||
dataObj = rawMap
|
||||
} else if _, hasMeta := rawMap["meta"]; hasMeta {
|
||||
dataObj = rawMap
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: extract from <script id="api-data"> in HTML if data was not passed directly
|
||||
if dataObj == nil && len(payload.HTML) > 100 {
|
||||
startTag := `<script id="api-data" type="application/json">`
|
||||
endTag := `</script>`
|
||||
startIdx := strings.Index(payload.HTML, startTag)
|
||||
@@ -405,26 +413,71 @@ func NewRouter(pm *ProcessManager) http.Handler {
|
||||
rem := payload.HTML[startIdx+len(startTag):]
|
||||
endIdx := strings.Index(rem, endTag)
|
||||
if endIdx != -1 {
|
||||
rawJSON := rem[:endIdx]
|
||||
var tmp interface{}
|
||||
if err := json.Unmarshal([]byte(rawJSON), &tmp); err == nil {
|
||||
dataBytes, _ = json.MarshalIndent(tmp, "", " ")
|
||||
} else {
|
||||
dataBytes = []byte(rawJSON)
|
||||
}
|
||||
_ = json.Unmarshal([]byte(rem[:endIdx]), &dataObj)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(dataBytes) > 0 {
|
||||
_ = os.WriteFile("web/docs/data.json", dataBytes, 0644)
|
||||
_ = os.WriteFile("scratch/faithful_docs_data.json", dataBytes, 0644)
|
||||
if dataObj == nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]interface{}{
|
||||
"status": "error",
|
||||
"error": "No valid documentation data found in request payload.",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Marshal formatted JSON for data.json
|
||||
dataBytes, err := json.MarshalIndent(dataObj, "", " ")
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]interface{}{
|
||||
"status": "error",
|
||||
"error": "Failed to format data JSON: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Save to web/docs/data.json and scratch/faithful_docs_data.json
|
||||
if err := os.WriteFile("web/docs/data.json", dataBytes, 0644); err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]interface{}{
|
||||
"status": "error",
|
||||
"error": "Failed to save data.json: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
_ = os.WriteFile("scratch/faithful_docs_data.json", dataBytes, 0644)
|
||||
|
||||
// Synchronize the embedded <script id="api-data"> in HTML files
|
||||
if len(payload.HTML) >= 100 {
|
||||
_ = os.WriteFile("web/docs/index.html", []byte(payload.HTML), 0644)
|
||||
_ = os.WriteFile("web/docs/FASTAPI_BACKEND_API_DOCS.html", []byte(payload.HTML), 0644)
|
||||
} else {
|
||||
updateEmbedded := func(filePath string) {
|
||||
b, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
content := string(b)
|
||||
startTag := `<script id="api-data" type="application/json">`
|
||||
endTag := `</script>`
|
||||
startIdx := strings.Index(content, startTag)
|
||||
if startIdx != -1 {
|
||||
afterStart := startIdx + len(startTag)
|
||||
endIdx := strings.Index(content[afterStart:], endTag)
|
||||
if endIdx != -1 {
|
||||
rawJSONStr := strings.ReplaceAll(string(dataBytes), "<", `\u003c`)
|
||||
newContent := content[:afterStart] + rawJSONStr + content[afterStart+endIdx:]
|
||||
_ = os.WriteFile(filePath, []byte(newContent), 0644)
|
||||
}
|
||||
}
|
||||
}
|
||||
updateEmbedded("web/docs/index.html")
|
||||
updateEmbedded("web/docs/FASTAPI_BACKEND_API_DOCS.html")
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"status": "ok",
|
||||
"bytes_written": len(payload.HTML),
|
||||
"data_written": len(dataBytes),
|
||||
"message": "Documentation data saved successfully to server.",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -452,6 +505,8 @@ func NewRouter(pm *ProcessManager) http.Handler {
|
||||
r.Get("/docs/editor/logout", logoutEditorHandler)
|
||||
r.Post("/docs/editor/logout", logoutEditorHandler)
|
||||
r.Post("/docs/editor/save", saveEditorHandler)
|
||||
r.Post("/docs/save", saveEditorHandler)
|
||||
r.Post("/api/docs/save", saveEditorHandler)
|
||||
|
||||
r.Get("/docs", func(w http.ResponseWriter, req *http.Request) {
|
||||
http.Redirect(w, req, "/docs/", http.StatusMovedPermanently)
|
||||
|
||||
File diff suppressed because one or more lines are too long
+64
-24
@@ -69,7 +69,7 @@ code{font-family:var(--mono);font-size:.92em}
|
||||
<span class="chip" id="sessionPill" title="Security session expires in 10 minutes" style="font-family:var(--mono);font-size:12.5px;"><i style="background:var(--ok);"></i><span id="timerCountdown">10:00</span></span>
|
||||
<span class="sp"></span>
|
||||
<button class="btn" id="undo" disabled title="Undo (outside text fields)">Undo</button><button class="btn" id="redo" disabled>Redo</button>
|
||||
<button class="btn" id="pvb">Hide preview</button><button class="btn" id="dl" disabled>Download copy</button><button class="btn pri" id="save" disabled>Save Changes</button>
|
||||
<button class="btn" id="pvb">Hide preview</button><button class="btn" id="dl" disabled>Download data.json</button><button class="btn pri" id="save" disabled>Save Changes</button>
|
||||
<a href="/docs/" target="_blank" class="btn" style="text-decoration:none;">View Docs ↗</a>
|
||||
<a href="/docs/editor/logout" class="btn bad" style="text-decoration:none;">Log out</a>
|
||||
</header>
|
||||
@@ -94,7 +94,20 @@ const post=o=>frame.contentWindow&&frame.contentWindow.postMessage({apidocs:1,..
|
||||
function waitFor(type,ms){return new Promise(res=>{const t=setTimeout(()=>res(null),ms),h=m=>{if(m.type===type){clearTimeout(t);res(m)}else waiters.push(h)};waiters.push(h)})}
|
||||
async function probe(set,ms=2600){set();const end=Date.now()+ms;
|
||||
while(Date.now()<end){const p=waitFor('data',350);post({type:'hello'});const m=await p;if(m)return m}return null}
|
||||
async function discover(){setStatus('wait','Looking for your docs…');
|
||||
async function discover(){
|
||||
setStatus('wait','Looking for your docs…');
|
||||
try {
|
||||
const res = await fetch('/docs/data.json?_t=' + Date.now(), { cache: 'no-store' });
|
||||
if (res.ok) {
|
||||
const live = await res.json();
|
||||
if (live && live.sections && live.sections.length) {
|
||||
frame.removeAttribute('srcdoc');
|
||||
frame.src = '/docs/?embed=1';
|
||||
connect(live, 'data.json', '/docs/data.json');
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch(e){}
|
||||
for(const c of CANDIDATES){const m=await probe(()=>{frame.removeAttribute('srcdoc');frame.src=c+(c.includes('?')?'&':'?')+'embed=1'});if(m)return connect(m.data,m.file||c,c)}
|
||||
failed()}
|
||||
function connect(data,name,s){src=s;file=name;D=data;saved=JSON.stringify(D);last=saved;past=[];future=[];sel={k:'meta'};dirty=false;
|
||||
@@ -304,29 +317,56 @@ document.addEventListener('click',e=>{const x=e.target.closest('[data-a]');if(!x
|
||||
now();draw();if(sel.k==='ep')post({type:'focus',id:ce().id,subIdx:sel.sub});});
|
||||
|
||||
/* ---------- saving ---------- */
|
||||
const idb=()=>new Promise((res,rej)=>{const r=indexedDB.open('apidocs-editor',1);r.onupgradeneeded=()=>r.result.createObjectStore('h');r.onsuccess=()=>res(r.result);r.onerror=()=>rej(r.error)});
|
||||
const hget=async k=>{try{const d=await idb();return await new Promise(r=>{const q=d.transaction('h').objectStore('h').get(k);q.onsuccess=()=>r(q.result);q.onerror=()=>r(null)})}catch{return null}};
|
||||
const hset=async(k,v)=>{try{const d=await idb();d.transaction('h','readwrite').objectStore('h').put(v,k)}catch{}};
|
||||
async function buildHtml(){now();const p=waitFor('html',5000);post({type:'export',data:D});const m=await p;if(!m)throw new Error('The docs page did not answer. Reload both files and try again.');return m.html}
|
||||
async function save(){if(!D)return;try{const html=await buildHtml();
|
||||
try {
|
||||
const resp = await fetch('/docs/editor/save', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ html, data: D })
|
||||
});
|
||||
if (resp.ok) {
|
||||
saved=JSON.stringify(D);dirty=false;saveDraft();updateUI();toast('Saved directly to server! Changes are now live in /docs/');
|
||||
return;
|
||||
async function save(){
|
||||
if (!D) return;
|
||||
const saveBtn = $('#save');
|
||||
saveBtn.disabled = true;
|
||||
saveBtn.textContent = 'Saving…';
|
||||
now();
|
||||
try {
|
||||
const resp = await fetch('/docs/editor/save', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ data: D })
|
||||
});
|
||||
const result = await resp.json().catch(() => ({}));
|
||||
if (resp.ok && result.status === 'ok') {
|
||||
saved = JSON.stringify(D);
|
||||
dirty = false;
|
||||
saveDraft();
|
||||
updateUI();
|
||||
toast('Saved successfully to server! Changes are now live in /docs/');
|
||||
return;
|
||||
}
|
||||
if (resp.status === 401) {
|
||||
toast('Admin session required or expired. Logging in...');
|
||||
setTimeout(() => { window.location.href = '/docs/editor'; }, 1500);
|
||||
return;
|
||||
}
|
||||
toast('Save failed: ' + (result.error || 'Server error ' + resp.status));
|
||||
} catch(err) {
|
||||
console.error('Server save error:', err);
|
||||
toast('Save error: ' + err.message);
|
||||
} finally {
|
||||
saveBtn.disabled = false;
|
||||
saveBtn.textContent = 'Save Changes';
|
||||
}
|
||||
} catch(err){console.warn('Server save error:', err);}
|
||||
if(!window.showSaveFilePicker)return download(html);
|
||||
let h=fh||await hget(dkey());if(h&&(await h.queryPermission({mode:'readwrite'}))!=='granted'&&(await h.requestPermission({mode:'readwrite'}))!=='granted')h=null;
|
||||
if(!h){toast('Choose the docs file in this folder to overwrite it');h=await showSaveFilePicker({suggestedName:file,types:[{description:'HTML',accept:{'text/html':['.html']}}]});await hset(dkey(),h)}
|
||||
fh=h;const w=await h.createWritable();await w.write(html);await w.close();saved=JSON.stringify(D);dirty=false;saveDraft();updateUI();toast('Saved to '+h.name+'. Reload the docs page to see it.')}
|
||||
catch(e){if(e.name!=='AbortError')alert('Could not save in place: '+e.message+'\nUse Download copy and replace the docs file yourself.')}}
|
||||
function download(html){const a=document.createElement('a');a.href=URL.createObjectURL(new Blob([html],{type:'text/html'}));a.download=file||'FASTAPI_BACKEND_API_DOCS.html';a.click();toast('Downloaded. Replace the old docs file with it.')}
|
||||
$('#save').onclick=save;$('#dl').onclick=async()=>{try{download(await buildHtml())}catch(e){alert(e.message)}};
|
||||
}
|
||||
|
||||
function download(data){
|
||||
const jsonStr = JSON.stringify(data || D, null, 2);
|
||||
const blob = new Blob([jsonStr], { type: 'application/json' });
|
||||
const a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = 'data.json';
|
||||
a.click();
|
||||
toast('Downloaded data.json');
|
||||
}
|
||||
|
||||
$('#save').onclick = save;
|
||||
$('#dl').onclick = () => download(D);
|
||||
$('#dl').textContent = 'Download data.json';
|
||||
$('#pvb').onclick=()=>{pvOn=!pvOn;$('#pv').hidden=!pvOn;$('#app').classList.toggle('nopv',!pvOn);$('#pvb').textContent=pvOn?'Hide preview':'Show preview'};
|
||||
addEventListener('beforeunload',e=>{if(dirty){e.preventDefault();e.returnValue=''}});
|
||||
addEventListener('keydown',e=>{if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==='s'){e.preventDefault();save()}
|
||||
|
||||
+18207
-2
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user