Exemplo n.º 1
0
func main() {
	var (
		memStore = appdash.NewMemoryStore()
		Store    = appdash.Store(memStore)
		Queryer  = memStore
	)

	app := traceapp.New(nil)
	app.Store = Store
	app.Queryer = Queryer

	var h http.Handler = app
	var l net.Listener
	var proto string
	var err error
	l, err = net.Listen("tcp", c.CollectorAddr)
	if err != nil {
		log.Fatal(err)
	}
	proto = "plaintext TCP (no security)"
	log.Printf("appdash collector listening on %s (%s)",
		c.CollectorAddr, proto)
	cs := appdash.NewServer(l, appdash.NewLocalCollector(Store))
	go cs.Start()

	log.Printf("appdash HTTP server listening on %s", c.HTTPAddr)
	err = http.ListenAndServe(c.HTTPAddr, h)
	if err != nil {
		fmt.Println("listenandserver listen err:", err)
	}
}
Exemplo n.º 2
0
// Execute execudes the commands with the given arguments and returns an error,
// if any.
func (c *ServeCmd) Execute(args []string) error {
	var (
		memStore = appdash.NewMemoryStore()
		Store    = appdash.Store(memStore)
		Queryer  = memStore
	)

	if c.StoreFile != "" {
		f, err := os.Open(c.StoreFile)
		if err != nil && !os.IsNotExist(err) {
			return err
		}
		if f != nil {
			if n, err := memStore.ReadFrom(f); err == nil {
				log.Printf("Read %d traces from file %s", n, c.StoreFile)
			} else if err != nil {
				f.Close()
				return err
			}
			if err := f.Close(); err != nil {
				return err
			}
		}
		if c.PersistInterval != 0 {
			go func() {
				if err := appdash.PersistEvery(memStore, c.PersistInterval, c.StoreFile); err != nil {
					log.Fatal(err)
				}
			}()
		}
	}

	if c.DeleteAfter > 0 {
		Store = &appdash.RecentStore{
			MinEvictAge: c.DeleteAfter,
			DeleteStore: memStore,
			Debug:       true,
		}
	}

	url, err := c.urlOrDefault()
	if err != nil {
		log.Fatal(err)
	}
	app, err := traceapp.New(nil, url)
	if err != nil {
		log.Fatal(err)
	}
	app.Store = Store
	app.Queryer = Queryer

	var h http.Handler
	if c.BasicAuth != "" {
		parts := strings.SplitN(c.BasicAuth, ":", 2)
		if len(parts) != 2 {
			log.Fatalf("Basic auth must be specified as 'user:passwd'.")
		}
		user, passwd := parts[0], parts[1]
		if user == "" || passwd == "" {
			log.Fatalf("Basic auth user and passwd must both be nonempty.")
		}
		log.Printf("Requiring HTTP Basic auth")
		h = newBasicAuthHandler(user, passwd, app)
	} else {
		h = app
	}

	if c.SampleData {
		sampleData(Store)
	}

	var l net.Listener
	var proto string
	if c.TLSCert != "" || c.TLSKey != "" {
		certBytes, err := ioutil.ReadFile(c.TLSCert)
		if err != nil {
			log.Fatal(err)
		}
		keyBytes, err := ioutil.ReadFile(c.TLSKey)
		if err != nil {
			log.Fatal(err)
		}

		var tc tls.Config
		cert, err := tls.X509KeyPair(certBytes, keyBytes)
		if err != nil {
			log.Fatal(err)
		}
		tc.Certificates = []tls.Certificate{cert}
		l, err = tls.Listen("tcp", c.CollectorAddr, &tc)
		if err != nil {
			log.Fatal(err)
		}
		proto = fmt.Sprintf("TLS cert %s, key %s", c.TLSCert, c.TLSKey)
	} else {
		var err error
		l, err = net.Listen("tcp", c.CollectorAddr)
		if err != nil {
			log.Fatal(err)
		}
		proto = "plaintext TCP (no security)"
	}
	log.Printf("appdash collector listening on %s (%s)", c.CollectorAddr, proto)
	cs := appdash.NewServer(l, appdash.NewLocalCollector(Store))
	cs.Debug = c.Debug
	cs.Trace = c.Trace
	go cs.Start()

	if c.TLSCert != "" || c.TLSKey != "" {
		log.Printf("appdash HTTPS server listening on %s (TLS cert %s, key %s)", c.HTTPAddr, c.TLSCert, c.TLSKey)
		return http.ListenAndServeTLS(c.HTTPAddr, c.TLSCert, c.TLSKey, h)
	}

	log.Printf("appdash HTTP server listening on %s", c.HTTPAddr)
	return http.ListenAndServe(c.HTTPAddr, h)
}