github.com/jgbaldwinbrown/perf@v0.1.1/analysis/app/index.go (about)

     1  // Copyright 2017 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package app
     6  
     7  import (
     8  	"html/template"
     9  	"net/http"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"golang.org/x/perf/storage"
    14  )
    15  
    16  // index redirects / to /search.
    17  func (a *App) index(w http.ResponseWriter, r *http.Request) {
    18  	ctx := requestContext(r)
    19  
    20  	tmpl, err := os.ReadFile(filepath.Join(a.BaseDir, "template/index.html"))
    21  	if err != nil {
    22  		http.Error(w, err.Error(), 500)
    23  		return
    24  	}
    25  
    26  	t, err := template.New("main").Parse(string(tmpl))
    27  	if err != nil {
    28  		http.Error(w, err.Error(), 500)
    29  		return
    30  	}
    31  
    32  	var uploads []storage.UploadInfo
    33  	ul := a.StorageClient.ListUploads(ctx, "", []string{"by", "upload-time"}, 16)
    34  	defer ul.Close()
    35  	for ul.Next() {
    36  		uploads = append(uploads, ul.Info())
    37  	}
    38  	if err := ul.Err(); err != nil {
    39  		errorf(ctx, "failed to fetch recent uploads: %v", err)
    40  	}
    41  
    42  	w.Header().Set("Content-Type", "text/html; charset=utf-8")
    43  	if err := t.Execute(w, struct{ RecentUploads []storage.UploadInfo }{uploads}); err != nil {
    44  		http.Error(w, err.Error(), 500)
    45  		return
    46  	}
    47  }