feat: simplify data and add cache to LocalFsAdapter

This commit is contained in:
Max Richter
2025-10-05 22:20:43 +02:00
parent fa283d5dd7
commit 6d92c92797
8 changed files with 196 additions and 160 deletions

View File

@@ -3,8 +3,6 @@ package handler
import (
"encoding/json"
"net/http"
"git.max-richter.dev/max/marka/server-new/internal/adapters"
)
type ErrorResponse struct {
@@ -20,8 +18,3 @@ func writeJSON(w http.ResponseWriter, code int, v any) {
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(v)
}
func writeFile(w http.ResponseWriter, file *adapters.FsFile) {
w.Header().Set("Content-Type", file.Type)
w.Write(file.Content)
}

View File

@@ -4,68 +4,44 @@ package handler
import (
"errors"
"net/http"
"time"
"git.max-richter.dev/max/marka/parser"
"git.max-richter.dev/max/marka/server-new/internal/adapters"
"git.max-richter.dev/max/marka/server/internal/adapters"
)
type ResponseItem struct {
Name string `json:"name"`
Content any `json:"content,omitempty"`
Type string `json:"type,omitempty"`
IsDir bool `json:"isDir"`
Size int64 `json:"size,omitempty"`
ModTime time.Time `json:"modTime"`
}
type Handler struct {
adapter adapters.FileAdapter
}
func (h *Handler) get(w http.ResponseWriter, target string) {
fsEntry, err := h.adapter.Read(target)
entry, err := h.adapter.Read(target)
if err != nil {
writeError(w, 500, err)
return
}
if fsEntry.File != nil {
if fsEntry.File.Content != nil && fsEntry.File.Type == "application/markdown" {
data, err := parser.ParseFile(string(fsEntry.File.Content))
if err != nil {
writeError(w, 500, err)
return
}
res := ResponseItem{
Name: fsEntry.File.Name,
Type: fsEntry.File.Type,
Content: data,
IsDir: false,
Size: int64(len(fsEntry.File.Content)),
ModTime: fsEntry.File.ModTime,
}
writeJSON(w, 200, res)
if errors.Is(err, adapters.ErrNotFound) {
writeError(w, http.StatusNotFound, err)
return
}
writeFile(w, fsEntry.File)
writeError(w, http.StatusInternalServerError, err)
return
}
if fsEntry.Dir != nil {
res := ResponseItem{
Name: fsEntry.Dir.Name,
Content: fsEntry.Dir.Files,
IsDir: true,
ModTime: fsEntry.Dir.ModTime,
if entry.Type == "file" {
// For non-markdown files, content is []byte, write it directly
if contentBytes, ok := entry.Content.([]byte); ok {
w.Header().Set("Content-Type", entry.MIME)
w.Write(contentBytes)
return
}
writeJSON(w, 200, res)
// For markdown files, content is parsed, return the whole Entry as JSON
writeJSON(w, http.StatusOK, entry)
return
}
// For directories, return the whole Entry as JSON
if entry.Type == "dir" {
writeJSON(w, http.StatusOK, entry)
return
}
writeError(w, http.StatusInternalServerError, errors.New("unknown entry type"))
}
func (h *Handler) post(w http.ResponseWriter, target string) {