예제 #1
0
파일: app.go 프로젝트: JonathanLogan/mute
func (ce *CtrlEngine) appStart(
	c *cli.Context,
	statusfp io.Writer,
	docroot string,
) error {
	// create listener for a free port
	// TODO: do we want to use a fixed port here?
	l, err := net.Listen("tcp", "localhost:")
	if err != nil {
		return err
	}
	// register handlers
	http.Handle("/", &staticHandler{
		handler: http.FileServer(http.Dir(docroot)),
	})
	http.Handle("/login", &loginHandler{
		ce:       ce,
		c:        c,
		statusfp: statusfp,
	})
	// start HTTP server
	ch := make(chan error)
	go func() {
		ch <- http.Serve(l, nil)
	}()
	// try to open browser
	addr := "http://" + l.Addr().String() + "/login"
	fmt.Fprintf(statusfp, "open browser for address: %s\n", addr)
	if !browser.Start(addr) {
		fmt.Fprintf(statusfp, "could not open browser for address: %s\n", addr)
	}
	return <-ch
}
예제 #2
0
파일: app.go 프로젝트: frankbraun/mute
func (ce *CtrlEngine) appStart(
	c *cli.Context,
	statusfp io.Writer,
	docroot string,
	httpAddress string,
) error {
	// create listener for a free port
	l, err := net.Listen("tcp", httpAddress)
	if err != nil {
		return err
	}
	// create muxer
	muxer := http.NewServeMux()
	// register handlers
	muxer.Handle("/", &staticHandler{
		handler: http.FileServer(http.Dir(docroot)),
	})
	muxer.Handle("/login", &loginHandler{
		ce:       ce,
		c:        c,
		statusfp: statusfp,
	})
	// create HTTP server
	srv := &http.Server{
		Handler:        muxer,
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20, // 1 MB
	}
	// start HTTP server
	ch := make(chan error)
	go func() {
		ch <- srv.Serve(l)
	}()
	// try to open browser
	addr := "http://" + l.Addr().String() + "/login"
	fmt.Fprintf(statusfp, "open browser for address: %s\n", addr)
	if !browser.Start(addr) {
		fmt.Fprintf(statusfp, "could not open browser for address: %s\n", addr)
	}
	return <-ch
}