Commit d124ba84 authored by Langhammer, Jens's avatar Langhammer, Jens

minor cleanup

parent 3a3bfb28
Pipeline #4190 passed with stages
in 2 minutes and 46 seconds
package internal
import (
"fmt"
"net/http"
"net/url"
"sort"
"git.beryju.org/BeryJu.org/pixie/pkg/utils"
)
func dirList(w http.ResponseWriter, r *http.Request, f http.File) {
dirs, err := f.Readdir(-1)
if err != nil {
http.Error(w, "Error reading directory", http.StatusInternalServerError)
return
}
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), utils.HtmlReplacer.Replace(name))
}
fmt.Fprintf(w, "</pre>\n")
}
......@@ -43,7 +43,7 @@ func (fm *FileMeta) ForFile(f http.File) error {
return err
}
} else {
log.Debugf("Skipping content-type check for %s because file is less than 512 bytes", stat.Name())
log.Debugf("Skipping content-type check for %s because file is more than 512 bytes", stat.Name())
}
fm.Name = stat.Name()
fm.Size = stat.Size()
......
......@@ -22,8 +22,8 @@ type File struct {
// Serve Write cached contents to http response
func (cf File) Serve(w http.ResponseWriter, r *http.Request) {
p, err := cf.FS.GetCacheFallback("", func() ([]byte, error) {
cf.FS.Logger.Debug("File not in Cache, reading from disk")
p, err := cf.FS.GetCacheFallback(cf.Key, func() ([]byte, error) {
cf.FS.Logger.WithField("name", cf.Key).Debug("File not in Cache, reading from disk")
buffer, err := ioutil.ReadAll(cf.openFileIfNeeded())
if err != nil {
return nil, errors.Wrap(err, "ReadAll Error")
......@@ -37,6 +37,7 @@ func (cf File) Serve(w http.ResponseWriter, r *http.Request) {
d, err := cf.Stat()
if err != nil {
// http.Error
cf.FS.Logger.Warning(err)
} else {
http.ServeContent(w, r, d.Name(), d.ModTime(), re)
......@@ -79,11 +80,11 @@ func (cf File) Stat() (os.FileInfo, error) {
return marshalled, nil
})
if err != nil {
cf.FS.Logger.Warning(err)
return nil, errors.Wrap(err, "Failed to populate Cache for Stat")
}
err = json.Unmarshal(statByte, &cf.stat)
if err != nil {
cf.FS.Logger.Warning(err)
return nil, errors.Wrap(err, "Failed to unmarshal JSON")
}
return cf.stat, nil
}
......@@ -92,7 +93,7 @@ func (cf File) openFileIfNeeded() *standard.File {
if cf.File == nil {
f, err := os.Open(cf.Key)
if err != nil {
cf.FS.Logger.Warning("test")
cf.FS.Logger.Warning(err)
}
cf.File = &standard.File{
File: f,
......
......@@ -34,6 +34,7 @@ func NewFileSystem() FileSystem {
cacheConfig := bigcache.DefaultConfig(time.Duration(config.Current.CacheEviction) * time.Minute)
cacheConfig.MaxEntrySize = config.Current.CacheMaxItemSize
cacheConfig.HardMaxCacheSize = config.Current.CacheMaxSize
cacheConfig.Logger = cfs.Logger
cache, err := bigcache.NewBigCache(cacheConfig)
if err != nil {
cfs.Logger.Warning(err)
......@@ -68,7 +69,7 @@ func (cfs FileSystem) Open(name string) (base.ServingFile, error) {
func (cfs FileSystem) GetCacheFallback(key string, populate func() ([]byte, error)) ([]byte, error) {
ret, err := cfs.Cache.Get(key)
if err == bigcache.ErrEntryNotFound {
cfs.Logger.Debug("Entry not found, calling populate()")
cfs.Logger.WithField("key", key).Debug("Entry not found, calling populate()")
realValue, err := populate()
if err != nil {
return nil, errors.Wrap(err, "Error executing CacheGet populate")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment