Exemplo n.º 1
0
// LogHeader logs a header
func LogHeader(log termlog.Logger, h http.Header) {
	max := 0
	for k := range h {
		if len(k) > max {
			max = len(k)
		}
	}
	for k, vals := range h {
		for _, v := range vals {
			pad := fmt.Sprintf(fmt.Sprintf("%%%ds", max-len(k)+1), " ")
			log.Say(
				"\t%s%s%s",
				color.BlueString(k)+":",
				pad,
				v,
			)
		}
	}
}
Exemplo n.º 2
0
func devdHandler(log termlog.Logger, route Route, templates *template.Template, logHeaders bool, ignoreHeaders []*regexp.Regexp, livereload bool, latency int) http.Handler {
	ci := inject.CopyInject{}
	if livereload {
		ci = injectLivereload
	}
	next := route.Endpoint.Handler(templates, ci)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var sublog termlog.Logger
		if matchStringAny(ignoreHeaders, fmt.Sprintf("%s%s", route.Host, r.RequestURI)) {
			sublog = termlog.DummyLogger{}
		} else {
			sublog = log.Group()
		}
		timr := timer.Timer{}
		defer func() {
			timing := termlog.DefaultPalette.Timestamp.SprintFunc()("timing: ")
			sublog.SayAs(
				"timer",
				timing+timr.String(),
			)
			sublog.Done()
		}()
		timr.RequestHeaders()
		time.Sleep(time.Millisecond * time.Duration(latency))
		sublog.Say("%s %s", r.Method, r.URL)
		if logHeaders {
			LogHeader(sublog, r.Header)
		}
		ctx := timr.NewContext(context.Background())
		ctx = termlog.NewContext(ctx, sublog)
		next.ServeHTTPContext(
			ctx,
			&ResponseLogWriter{
				log:        sublog,
				rw:         w,
				timr:       &timr,
				logHeaders: logHeaders,
			},
			r,
		)
	})
}
Exemplo n.º 3
0
Arquivo: server.go Projeto: jonaz/devd
// 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.Logger, 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 fmt.Errorf("Could not bind to port: %s", 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)
	err = server.Serve(hl)
	logger.Shout("Server stopped: %v", err)
	return nil
}
Exemplo n.º 4
0
func testpatt(l termlog.Logger) {
	l.Say("Log")
	l.Notice("Notice!")
	l.Warn("Warn!")
	l.Shout("Error!")
}
Exemplo n.º 5
0
Arquivo: cli.go Projeto: supiyun/devd
// 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
}