github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/cli/csv_server.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package cli
    12  
    13  import (
    14  	"fmt"
    15  	"net/http"
    16  	"net/http/pprof"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/workload"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  var csvServerCmd = SetCmdDefaults(&cobra.Command{
    23  	Use:   `csv-server`,
    24  	Short: `serves csv table data through an HTTP interface`,
    25  	Args:  cobra.NoArgs,
    26  	RunE:  runCSVServer,
    27  })
    28  
    29  var port *int
    30  
    31  func init() {
    32  	port = csvServerCmd.Flags().Int(`port`, 8081, `The port to bind to`)
    33  	AddSubCmd(func(_ bool) *cobra.Command { return csvServerCmd })
    34  }
    35  
    36  func runCSVServer(_ *cobra.Command, _ []string) error {
    37  	mux := workload.CSVMux(workload.Registered())
    38  
    39  	// Cribbed straight from pprof's `init()` method. See:
    40  	// https://golang.org/src/net/http/pprof/pprof.go
    41  	mux.HandleFunc("/debug/pprof/", pprof.Index)
    42  	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
    43  	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
    44  	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
    45  	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
    46  
    47  	s := &http.Server{
    48  		Addr:    fmt.Sprintf(`:%d`, *port),
    49  		Handler: mux,
    50  	}
    51  	fmt.Printf("Listening on %s\n", s.Addr)
    52  	return s.ListenAndServe()
    53  }