mirror of
https://github.com/Dvorinka/excalidraw-full.git
synced 2026-06-03 22:02:57 +00:00
eea09906f4
Instead of calling any third-party or home, we provide the stub for firebase API calls.
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
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())
|
|
}
|
|
}
|