github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/couchserver/favicon.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 "embed" 19 "io" 20 "net/http" 21 "os" 22 23 internal "github.com/go-kivik/kivik/v4/int/errors" 24 ) 25 26 //go:embed files 27 var files embed.FS 28 29 // GetFavicon serves GET /favicon.ico 30 func (h *Handler) GetFavicon() http.HandlerFunc { 31 return func(w http.ResponseWriter, _ *http.Request) { 32 var ico io.Reader 33 if h.Favicon == "" { 34 asset, err := files.Open("files/favicon.ico") 35 if err != nil { 36 panic(err) 37 } 38 defer asset.Close() 39 ico = asset 40 } else { 41 file, err := os.Open(h.Favicon) 42 if err != nil { 43 if os.IsNotExist(err) { 44 err = &internal.Error{Status: http.StatusNotFound, Message: "not found"} 45 } 46 h.HandleError(w, err) 47 return 48 } 49 ico = file 50 defer file.Close() 51 } 52 w.Header().Set("Content-Type", "image/x-icon") 53 _, err := io.Copy(w, ico) 54 h.HandleError(w, err) 55 } 56 }