golang.org/x/build@v0.0.0-20240506185731-218518f32b70/perfdata/app/app.go (about) 1 // Copyright 2016 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 implements the performance data storage server. Combine 6 // an App with a database and filesystem to get an HTTP server. 7 package app 8 9 import ( 10 "errors" 11 "net/http" 12 "path/filepath" 13 14 "golang.org/x/build/perfdata/db" 15 "golang.org/x/build/perfdata/fs" 16 ) 17 18 // App manages the storage server logic. Construct an App instance 19 // using a literal with DB and FS objects and call RegisterOnMux to 20 // connect it with an HTTP server. 21 type App struct { 22 DB *db.DB 23 FS fs.FS 24 25 // Auth obtains the username for the request. 26 // If necessary, it can write its own response (e.g. a 27 // redirect) and return ErrResponseWritten. 28 Auth func(http.ResponseWriter, *http.Request) (string, error) 29 30 // ViewURLBase will be used to construct a URL to return as 31 // "viewurl" in the response from /upload. If it is non-empty, 32 // the upload ID will be appended to ViewURLBase. 33 ViewURLBase string 34 35 // BaseDir is the directory containing the "template" directory. 36 // If empty, the current directory will be used. 37 BaseDir string 38 } 39 40 // ErrResponseWritten can be returned by App.Auth to abort the normal /upload handling. 41 var ErrResponseWritten = errors.New("response written") 42 43 // RegisterOnMux registers the app's URLs on mux. 44 func (a *App) RegisterOnMux(mux *http.ServeMux) { 45 // TODO(quentin): Should we just make the App itself be an http.Handler? 46 mux.HandleFunc("/", a.index) 47 mux.HandleFunc("/upload", a.upload) 48 mux.HandleFunc("/search", a.search) 49 mux.HandleFunc("/uploads", a.uploads) 50 } 51 52 // index serves the readme on / 53 func (a *App) index(w http.ResponseWriter, r *http.Request) { 54 http.ServeFile(w, r, filepath.Join(a.BaseDir, "static/index.html")) 55 }