github.com/cayleygraph/cayley@v0.7.7/cmd/cayley/command/http.go (about)

     1  package command
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/spf13/cobra"
     9  	"github.com/spf13/viper"
    10  
    11  	"github.com/cayleygraph/cayley/clog"
    12  	chttp "github.com/cayleygraph/cayley/internal/http"
    13  )
    14  
    15  func NewHttpCmd() *cobra.Command {
    16  	cmd := &cobra.Command{
    17  		Use:   "http",
    18  		Short: "Serve an HTTP endpoint on the given host and port.",
    19  		RunE: func(cmd *cobra.Command, args []string) error {
    20  			printBackendInfo()
    21  			p := mustSetupProfile(cmd)
    22  			defer mustFinishProfile(p)
    23  
    24  			h, err := openForQueries(cmd)
    25  			if err != nil {
    26  				return err
    27  			}
    28  			defer h.Close()
    29  
    30  			err = chttp.SetupRoutes(h, &chttp.Config{
    31  				Timeout:  viper.GetDuration(keyQueryTimeout),
    32  				ReadOnly: viper.GetBool(KeyReadOnly),
    33  			})
    34  			if err != nil {
    35  				return err
    36  			}
    37  			host, _ := cmd.Flags().GetString("host")
    38  			phost := host
    39  			if host, port, err := net.SplitHostPort(host); err == nil && host == "" {
    40  				phost = net.JoinHostPort("localhost", port)
    41  			}
    42  			clog.Infof("listening on %s, web interface at http://%s", host, phost)
    43  			return http.ListenAndServe(host, nil)
    44  		},
    45  	}
    46  	cmd.Flags().String("host", "127.0.0.1:64210", "host:port to listen on")
    47  	cmd.Flags().Bool("init", false, "initialize the database before using it")
    48  	cmd.Flags().DurationP("timeout", "t", 30*time.Second, "elapsed time until an individual query times out")
    49  	cmd.Flags().String("assets", "", "[depreacted]")
    50  	if cmd.Flags().Lookup("assets") != nil || cmd.Flags().Lookup("assets").Value.String() != "" {
    51  		clog.Errorf("The assets flag is deprecated as assets are provided as part of the binary.")
    52  	}
    53  	registerLoadFlags(cmd)
    54  	viper.BindPFlag(keyQueryTimeout, cmd.Flags().Lookup("timeout"))
    55  	return cmd
    56  }