1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-28 17:03:58 +08:00

CR updates

replaced H
moved internal errors into function.

Signed-off-by: Simon Kirkby <tigger@interthingy.com>
This commit is contained in:
Simon Kirkby
2014-11-24 21:10:42 +08:00
committed by Matt Bell
parent 32823d2085
commit 64cb1b6bf8

View File

@ -25,7 +25,7 @@ type ipfs interface {
}
// shortcut for templating
type H map[string]interface{}
type webHandler map[string]interface{}
// struct for directory listing
type directoryItem struct {
@ -91,7 +91,7 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Debug("is directory %s", path)
if path[len(path)-1:] != "/" {
log.Debug("missing trailing slash redirect")
log.Debug("missing trailing slash, redirect")
http.Redirect(w, r, "/ipfs/"+path+"/", 307)
return
}
@ -105,16 +105,12 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// return index page
nd, err := i.ResolvePath(path + "/index.html")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
log.Error("%s", err)
internalWebError(w, err)
return
}
dr, err := i.NewDagReader(nd)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
log.Error("%s", err)
internalWebError(w, err)
return
}
// write to request
@ -124,24 +120,18 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
dirListing = append(dirListing, directoryItem{link.Size, link.Name})
}
// template and return directory listing
//for i, j := range dirListing {
// log.Debug(i, ":", j.Size, " ", j.Name)
//}
err := i.dirList.Execute(w, H{"listing": dirListing, "path": path})
err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
log.Error("%s", err)
internalWebError(w, err)
return
}
return
}
w.WriteHeader(http.StatusInternalServerError)
log.Error(err)
w.Write([]byte(err.Error()))
// not a directory and still an error
internalWebError(w, err)
return
}
// data file
// return data file
io.Copy(w, dr)
}
@ -167,6 +157,13 @@ func (i *ipfsHandler) postHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(mh.Multihash(k).B58String()))
}
// return a 500 error and log
func internalWebError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
log.Error("%s", err)
}
// Directory listing template
var listingTemplate = `
<!DOCTYPE html>