github.com/go-kivik/kivik/v4@v4.3.2/x/server/docs.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build !js 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 }