redirect.go
1 package corehttp 2 3 import ( 4 "net" 5 "net/http" 6 7 core "github.com/ipfs/kubo/core" 8 ) 9 10 func RedirectOption(path string, redirect string) ServeOption { 11 return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { 12 cfg, err := n.Repo.Config() 13 if err != nil { 14 return nil, err 15 } 16 17 handler := &redirectHandler{redirect, cfg.API.HTTPHeaders} 18 19 if len(path) > 0 { 20 mux.Handle("/"+path+"/", handler) 21 } else { 22 mux.Handle("/", handler) 23 } 24 return mux, nil 25 } 26 } 27 28 type redirectHandler struct { 29 path string 30 headers map[string][]string 31 } 32 33 func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 34 for k, v := range i.headers { 35 w.Header()[http.CanonicalHeaderKey(k)] = v 36 } 37 38 http.Redirect(w, r, i.path, http.StatusFound) 39 }