github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/couchserver/db.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 couchserver 16 17 import ( 18 "encoding/json" 19 "net/http" 20 ) 21 22 // PutDB handles PUT /{db} 23 func (h *Handler) PutDB() http.HandlerFunc { 24 return func(w http.ResponseWriter, r *http.Request) { 25 if err := h.client.CreateDB(r.Context(), DB(r)); err != nil { 26 h.HandleError(w, err) 27 return 28 } 29 h.HandleError(w, json.NewEncoder(w).Encode(map[string]interface{}{ 30 "ok": true, 31 })) 32 } 33 } 34 35 // HeadDB handles HEAD /{db} 36 func (h *Handler) HeadDB() http.HandlerFunc { 37 return func(w http.ResponseWriter, r *http.Request) { 38 exists, err := h.client.DBExists(r.Context(), DB(r)) 39 if err != nil { 40 h.HandleError(w, err) 41 return 42 } 43 if exists { 44 w.WriteHeader(http.StatusOK) 45 } else { 46 w.WriteHeader(http.StatusNotFound) 47 } 48 } 49 } 50 51 // GetDB handles GET /{db} 52 func (h *Handler) GetDB() http.HandlerFunc { 53 return func(w http.ResponseWriter, r *http.Request) { 54 db, err := h.client.DB(r.Context(), DB(r)) 55 if err != nil { 56 h.HandleError(w, err) 57 return 58 } 59 stats, err := db.Stats(r.Context()) 60 if err != nil { 61 h.HandleError(w, err) 62 return 63 } 64 w.Header().Set("Cache-Control", "must-revalidate") 65 w.Header().Set("Content-Type", "application/json") 66 w.WriteHeader(http.StatusOK) 67 err = json.NewEncoder(w).Encode(stats) 68 if err != nil { 69 h.HandleError(w, err) 70 return 71 } 72 } 73 } 74 75 // Flush handles POST /{db}/_ensure_full_commit 76 func (h *Handler) Flush() http.HandlerFunc { 77 return func(w http.ResponseWriter, r *http.Request) { 78 db, err := h.client.DB(r.Context(), DB(r)) 79 if err != nil { 80 h.HandleError(w, err) 81 return 82 } 83 if err := db.Flush(r.Context()); err != nil { 84 h.HandleError(w, err) 85 return 86 } 87 w.Header().Set("Content-Type", typeJSON) 88 h.HandleError(w, json.NewEncoder(w).Encode(map[string]interface{}{ 89 "instance_start_time": 0, 90 "ok": true, 91 })) 92 } 93 }