Add different data providers and improve docs

This adds support for switching between in-memory, local filesystem, s3
or sqlite.
This commit is contained in:
patwie
2024-03-29 10:02:31 +00:00
parent 2dd1421b6e
commit 31c8029402
10 changed files with 426 additions and 89 deletions
+48
View File
@@ -0,0 +1,48 @@
package documents
import (
"bytes"
"excalidraw-complete/core"
"io"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
type (
DocumentCreateResponse struct {
ID string `json:"id"`
}
)
func HandleCreate(documentStore core.DocumentStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := new(bytes.Buffer)
_, err := io.Copy(data, r.Body)
if err != nil {
http.Error(w, "Failed to copy", http.StatusInternalServerError)
return
}
id, err := documentStore.Create(r.Context(), &core.Document{Data: *data})
if err != nil {
http.Error(w, "Failed to save", http.StatusInternalServerError)
return
}
render.JSON(w, r, DocumentCreateResponse{ID: id})
render.Status(r, http.StatusOK)
}
}
func HandleGet(documentStore core.DocumentStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
document, err := documentStore.FindID(r.Context(), id)
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
w.Write(document.Data.Bytes())
}
}