...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package server
16
17 import (
18 "encoding/json"
19 "net/http"
20
21 "github.com/go-chi/chi/v5"
22 "gitlab.com/flimzy/httpe"
23 )
24
25 func (s *Server) postDoc() httpe.HandlerWithError {
26 return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, r *http.Request) error {
27 db := chi.URLParam(r, "db")
28 var doc interface{}
29 if err := json.NewDecoder(r.Body).Decode(&doc); err != nil {
30 return err
31 }
32 id, rev, err := s.client.DB(db).CreateDoc(r.Context(), doc, options(r))
33 if err != nil {
34 return err
35 }
36 return serveJSON(w, http.StatusCreated, map[string]interface{}{
37 "id": id,
38 "rev": rev,
39 "ok": true,
40 })
41 })
42 }
43
44 func (s *Server) doc() httpe.HandlerWithError {
45 return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, r *http.Request) error {
46 db := chi.URLParam(r, "db")
47 id := chi.URLParam(r, "docid")
48 var doc interface{}
49 err := s.client.DB(db).Get(r.Context(), id, options(r)).ScanDoc(&doc)
50 if err != nil {
51 return err
52 }
53 return serveJSON(w, http.StatusOK, doc)
54 })
55 }
56
View as plain text