func TestRouteHandler(t *testing.T) { var routeHandlerTests = []struct { spec string }{ {"/one=two"}, } for i, tt := range routeHandlerTests { r, err := newRoute(tt.spec) if err != nil { t.Errorf( "Test %d, unexpected error:\n%s\n", i, err, ) } rb, err := rice.FindBox("templates") if err != nil { t.Error(err) } templates, err := ricetemp.MakeTemplates(rb) if err != nil { panic(err) } r.Endpoint.Handler(templates, inject.CopyInject{}) } }
// Serve starts the devd server. The callback is called with the serving URL // just before service starts. func (dd *Devd) Serve(address string, port int, certFile string, logger termlog.TermLog, callback func(string)) error { templates, err := ricetemp.MakeTemplates(rice.MustFindBox("templates")) if err != nil { return fmt.Errorf("Error loading templates: %s", err) } mux, err := dd.Router(logger, templates) if err != nil { return err } var tlsConfig *tls.Config var tlsEnabled bool if certFile != "" { tlsConfig, err = getTLSConfig(certFile) if err != nil { return fmt.Errorf("Could not load certs: %s", err) } tlsEnabled = true } var hl net.Listener if port > 0 { hl, err = net.Listen("tcp", fmt.Sprintf("%v:%d", address, port)) } else { hl, err = pickPort(address, portLow, portHigh, tlsEnabled) } if err != nil { return err } if tlsConfig != nil { hl = tls.NewListener(hl, tlsConfig) } hl = slowdown.NewSlowListener(hl, dd.UpKbps*1024, dd.DownKbps*1024) url := formatURL(tlsEnabled, address, hl.Addr().(*net.TCPAddr).Port) logger.Say("Listening on %s (%s)", url, hl.Addr().String()) server := &http.Server{Addr: hl.Addr().String(), Handler: mux} callback(url) if dd.HasLivereload() { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGHUP) go func() { for { <-c logger.Say("Received signal - reloading") dd.lrserver.Reload([]string{"*"}) } }() } err = server.Serve(hl) logger.Shout("Server stopped: %v", err) return nil }
func TestForwardEndpoint(t *testing.T) { f, err := newForwardEndpoint("http://foo") if err != nil { t.Errorf("Unexpected error: %s", err) } rb, err := rice.FindBox("templates") if err != nil { t.Error(err) } templates, err := ricetemp.MakeTemplates(rb) if err != nil { panic(err) } f.Handler(templates, inject.CopyInject{}) f, err = newForwardEndpoint("%") if err == nil { t.Errorf("Expected error, got %s", f) } }
// Serve starts the devd server func (dd *Devd) Serve(logger termlog.Logger) error { templates, err := ricetemp.MakeTemplates(rice.MustFindBox("templates")) if err != nil { return fmt.Errorf("Error loading templates: %s", err) } ignores := make([]*regexp.Regexp, 0, 0) for _, expr := range dd.IgnoreLogs { v, err := regexp.Compile(expr) if err != nil { return fmt.Errorf("%s", err) } ignores = append(ignores, v) } routeColl := make(RouteCollection) for _, s := range dd.Routes { err := routeColl.Set(s) if err != nil { return fmt.Errorf("Invalid route specification: %s", err) } } mux := http.NewServeMux() for match, route := range routeColl { handler := dd.RouteHandler(logger, route, templates, ignores) mux.Handle(match, http.StripPrefix(route.Path, handler)) } lr := livereload.NewServer("livereload", logger) if dd.LivereloadEnabled() { mux.Handle("/livereload", lr) mux.Handle("/livereload.js", http.HandlerFunc(lr.ServeScript)) } if dd.LivereloadRoutes { err = WatchRoutes(routeColl, lr, dd.Excludes, logger) if err != nil { return fmt.Errorf("Could not watch routes for livereload: %s", err) } } if len(dd.Watch) > 0 { err = WatchPaths(dd.Watch, dd.Excludes, lr, logger) if err != nil { return fmt.Errorf("Could not watch path for livereload: %s", err) } } var hl net.Listener tlsEnabled := false if dd.CertFile != "" { tlsEnabled = true } if dd.Port > 0 { hl, err = net.Listen("tcp", fmt.Sprintf("%v:%d", dd.Address, dd.Port)) } else { hl, err = pickPort(dd.Address, portLow, portHigh, tlsEnabled) } if err != nil { return fmt.Errorf("Could not bind to port: %s", err) } var tlsConfig *tls.Config if dd.CertFile != "" { tlsConfig, err = getTLSConfig(dd.CertFile) if err != nil { return fmt.Errorf("Could not load certs: %s", err) } hl = tls.NewListener(hl, tlsConfig) } hl = slowdown.NewSlowListener(hl, dd.UpKbps*1024, dd.DownKbps*1024) url := formatURL(tlsEnabled, dd.Address, hl.Addr().(*net.TCPAddr).Port) logger.Say("Listening on %s (%s)", url, hl.Addr().String()) if dd.OpenBrowser { go func() { webbrowser.Open(url) }() } server := &http.Server{ Addr: hl.Addr().String(), Handler: hostPortStrip(mux), } err = server.Serve(hl) logger.Shout("Server stopped: %v", err) return nil }