golang.org/x/build@v0.0.0-20240506185731-218518f32b70/perfdata/localperfdata/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  //go:build cgo
     6  
     7  // Localperfdata runs an HTTP server for benchmark perfdata.
     8  //
     9  // Usage:
    10  //
    11  //	localperfdata [-addr address] [-view_url_base url] [-base_dir ../appengine] [-dsn file.db]
    12  package main
    13  
    14  import (
    15  	"flag"
    16  	"log"
    17  	"net/http"
    18  
    19  	"golang.org/x/build/internal/basedir"
    20  	"golang.org/x/build/perfdata/app"
    21  	"golang.org/x/build/perfdata/db"
    22  	_ "golang.org/x/build/perfdata/db/sqlite3"
    23  	"golang.org/x/build/perfdata/fs"
    24  	"golang.org/x/build/perfdata/fs/local"
    25  )
    26  
    27  var (
    28  	addr        = flag.String("addr", ":8080", "serve HTTP on `address`")
    29  	viewURLBase = flag.String("view_url_base", "", "/upload response with `URL` for viewing")
    30  	dsn         = flag.String("dsn", ":memory:", "sqlite `dsn`")
    31  	data        = flag.String("data", "", "data `directory` (in-memory if empty)")
    32  	baseDir     = flag.String("base_dir", basedir.Find("golang.org/x/build/perfdata/appengine"), "base `directory` for static files")
    33  )
    34  
    35  func main() {
    36  	flag.Parse()
    37  
    38  	if *baseDir == "" {
    39  		log.Print("base_dir is required and could not be automatically found")
    40  		flag.Usage()
    41  	}
    42  
    43  	db, err := db.OpenSQL("sqlite3", *dsn)
    44  	if err != nil {
    45  		log.Fatalf("open database: %v", err)
    46  	}
    47  	var fs fs.FS = fs.NewMemFS()
    48  
    49  	if *data != "" {
    50  		fs = local.NewFS(*data)
    51  	}
    52  
    53  	app := &app.App{
    54  		DB:          db,
    55  		FS:          fs,
    56  		ViewURLBase: *viewURLBase,
    57  		Auth:        func(http.ResponseWriter, *http.Request) (string, error) { return "", nil },
    58  		BaseDir:     *baseDir,
    59  	}
    60  	app.RegisterOnMux(http.DefaultServeMux)
    61  
    62  	log.Printf("Listening on %s", *addr)
    63  
    64  	log.Fatal(http.ListenAndServe(*addr, nil))
    65  }