feat: restore configurable root page and api docs

This commit is contained in:
OpenClaw Agent
2026-09-21 23:44:57 +08:00
parent 1913375251
commit fc918013ee
3 changed files with 65 additions and 2 deletions
+29 -1
View File
@@ -44,6 +44,8 @@ type Server struct {
RemoveMode int
ImagePreview bool
FolderDownload string
HTMLTitle string
HTMLNotice string
Log *opslog.Logger
}
type fileInfo struct {
@@ -76,6 +78,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("/chfs/shared", s.shared)
mux.HandleFunc("/chfs/shared/", s.shared)
mux.HandleFunc("/chfs/preview", s.preview)
mux.HandleFunc("/asset/api.html", s.apiDoc)
mux.HandleFunc("/", s.fileHandler)
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Go's ServeMux canonicalizes a subtree route before dispatching.
@@ -767,7 +770,32 @@ func (s *Server) preview(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) fileHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
title := s.HTMLTitle
if title == "" {
title = "CuteHttpFileServer"
}
notice := s.HTMLNotice
if strings.HasPrefix(notice, "`") && strings.HasSuffix(notice, "`") && len(notice) >= 2 {
notice = notice[1 : len(notice)-1]
} else {
notice = html.EscapeString(notice)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = fmt.Fprintf(w, `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>%s</title></head><body><main><h1>%s</h1>%s<p><a href="/chfs/files?filepath=">Open file API</a> · <a href="/asset/api.html">API documentation</a></p></main></body></html>`, html.EscapeString(title), html.EscapeString(title), notice)
}
func (s *Server) apiDoc(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/asset/api.html" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
const doc = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>CHFS API</title></head><body><h1>CHFS API</h1><p>Compatibility API implemented by chfs-ng.</p><ul><li>/chfs/files</li><li>/chfs/session</li><li>/chfs/upload</li><li>/chfs/newdir</li><li>/chfs/rename</li><li>/chfs/rmfiles</li><li>/chfs/search</li><li>/chfs/downloaddir/&lt;path&gt;</li><li>/chfs/shared/&lt;path&gt;</li><li>/chfs/preview</li><li>/webdav</li></ul></body></html>`
_, _ = w.Write([]byte(doc))
}
func (s *Server) serveFile(w http.ResponseWriter, r *http.Request, p string, attachment bool) {