mirror of
https://github.com/Dvorinka/excalidraw-full.git
synced 2026-06-03 22:02:57 +00:00
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:
@@ -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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user