github.com/orteth01/up@v0.2.0/internal/cli/start/start.go (about)

     1  package start
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/apex/log"
     7  	"github.com/pkg/errors"
     8  	"github.com/tj/kingpin"
     9  
    10  	"github.com/apex/up/handler"
    11  	"github.com/apex/up/internal/cli/root"
    12  	"github.com/apex/up/internal/stats"
    13  )
    14  
    15  func init() {
    16  	cmd := root.Command("start", "Start development server.")
    17  	cmd.Example(`up start`, "Start development server on port 3000.")
    18  	cmd.Example(`up start --address :5000`, "Start development server on port 5000.")
    19  
    20  	addr := cmd.Flag("address", "Address for server.").Default(":3000").String()
    21  
    22  	cmd.Action(func(_ *kingpin.ParseContext) error {
    23  		_, _, err := root.Init()
    24  		if err != nil {
    25  			return errors.Wrap(err, "initializing")
    26  		}
    27  
    28  		stats.Track("Start", map[string]interface{}{
    29  			"address": *addr,
    30  		})
    31  
    32  		h, err := handler.New()
    33  		if err != nil {
    34  			return errors.Wrap(err, "initializing handler")
    35  		}
    36  
    37  		log.WithField("address", *addr).Infof("listening")
    38  		if err := http.ListenAndServe(*addr, h); err != nil {
    39  			return errors.Wrap(err, "binding")
    40  		}
    41  
    42  		return nil
    43  	})
    44  }