示例#1
0
func initializeDatabase(cfg config.Config, logger lager.Logger) (db.DB, error) {
	logger.Info("database", lager.Data{"etcd-addresses": flag.Args()})
	maxWorkers := cfg.MaxConcurrentETCDRequests
	if maxWorkers <= 0 {
		maxWorkers = DEFAULT_ETCD_WORKERS
	}

	return db.NewETCD(flag.Args(), maxWorkers)
}
示例#2
0
	"github.com/cloudfoundry-incubator/routing-api/db"
	"github.com/cloudfoundry/storeadapter"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("DB", func() {
	Describe("etcd", func() {
		var (
			etcd  db.DB
			route db.Route
		)

		BeforeEach(func() {
			etcd = db.NewETCD(etcdRunner.NodeURLS())
			route = db.Route{
				Route:   "post_here",
				Port:    7000,
				IP:      "1.2.3.4",
				TTL:     50,
				LogGuid: "my-guid",
			}
			etcd.Connect()
		})

		AfterEach(func() {
			etcd.Disconnect()
		})

		Describe(".ReadRoutes", func() {
示例#3
0
	"github.com/cloudfoundry-incubator/routing-api/db"
	"github.com/cloudfoundry/storeadapter"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("DB", func() {
	Describe("etcd error", func() {
		var (
			etcd db.DB
			err  error
		)

		BeforeEach(func() {
			etcd, err = db.NewETCD(etcdRunner.NodeURLS(), 0)
			Expect(err).To(HaveOccurred())
		})

		It("should not return an etcd instance", func() {
			Expect(etcd).To(BeNil())
		})
	})

	Describe("etcd", func() {

		var (
			etcd db.DB
			err  error
		)
示例#4
0
func main() {
	logger := cf_lager.New("routing-api")

	flag.Parse()
	if *configPath == "" {
		logger.Error("failed to start", errors.New("No configuration file provided"))
		os.Exit(1)
	}

	cfg, err := config.NewConfigFromFile(*configPath)
	if err != nil {
		logger.Error("failed to start", err)
		os.Exit(1)
	}

	err = dropsonde.Initialize(cfg.MetronConfig.Address+":"+cfg.MetronConfig.Port, cfg.LogGuid)
	if err != nil {
		logger.Error("failed to initialize Dropsonde", err)
		os.Exit(1)
	}

	logger.Info("database", lager.Data{"etcd-addresses": flag.Args()})
	database := db.NewETCD(flag.Args())
	err = database.Connect()
	if err != nil {
		logger.Error("failed to connect to etcd", err)
		os.Exit(1)
	}
	defer database.Disconnect()

	var token authentication.Token

	if *devMode {
		token = authentication.NullToken{}
	} else {
		token = authentication.NewAccessToken(cfg.UAAPublicKey)
		err = token.CheckPublicToken()
		if err != nil {
			logger.Error("failed to check public token", err)
			os.Exit(1)
		}
	}

	validator := handlers.NewValidator()

	routesHandler := handlers.NewRoutesHandler(token, *maxTTL, validator, database, logger)
	eventStreamHandler := handlers.NewEventStreamHandler(token, database, logger)

	actions := rata.Handlers{
		"Upsert":      route(routesHandler.Upsert),
		"Delete":      route(routesHandler.Delete),
		"List":        route(routesHandler.List),
		"EventStream": route(eventStreamHandler.EventStream),
	}

	handler, err := rata.NewRouter(routing_api.Routes, actions)
	if err != nil {
		logger.Error("failed to create router", err)
		os.Exit(1)
	}

	handler = handlers.LogWrap(handler, logger)

	logger.Info("starting", lager.Data{"port": *port})
	err = http.ListenAndServe(":"+strconv.Itoa(*port), handler)
	if err != nil {
		panic(err)
	}
}
示例#5
0
	"github.com/coreos/etcd/client"
	"github.com/nu7hatch/gouuid"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("DB", func() {
	Context("when no URLs are passed in", func() {
		var (
			etcd db.DB
			err  error
		)

		BeforeEach(func() {
			etcd, err = db.NewETCD([]string{})
		})

		It("should not return an etcd instance", func() {
			Expect(err).To(HaveOccurred())
			Expect(etcd).To(BeNil())
		})
	})

	Context("when connect fails", func() {
		var (
			etcd db.DB
			err  error
		)

		BeforeEach(func() {
示例#6
0
文件: main.go 项目: yingkitw/gorouter
func main() {
	cf_lager.AddFlags(flag.CommandLine)
	flag.Parse()

	logger, reconfigurableSink := cf_lager.New("routing-api")

	err := checkFlags()
	if err != nil {
		logger.Error("failed to start", err)
		os.Exit(1)
	}

	cfg, err := config.NewConfigFromFile(*configPath, *devMode)
	if err != nil {
		logger.Error("failed to start", err)
		os.Exit(1)
	}

	err = dropsonde.Initialize(cfg.MetronConfig.Address+":"+cfg.MetronConfig.Port, cfg.LogGuid)
	if err != nil {
		logger.Error("failed to initialize Dropsonde", err)
		os.Exit(1)
	}

	if cfg.DebugAddress != "" {
		cf_debug_server.Run(cfg.DebugAddress, reconfigurableSink)
	}

	logger.Info("database", lager.Data{"etcd-addresses": flag.Args()})
	database, err := db.NewETCD(flag.Args())
	if err != nil {
		logger.Error("failed to initialize database", err)
		os.Exit(1)
	}

	err = database.Connect()
	if err != nil {
		logger.Error("failed to connect to database", err)
		os.Exit(1)
	}
	defer database.CancelWatches()

	// seed router groups (one time only)
	seedRouterGroups(cfg, logger, database)

	prefix := "routing_api"
	statsdClient, err := statsd.NewBufferedClient(cfg.StatsdEndpoint, prefix, cfg.StatsdClientFlushInterval, 512)
	if err != nil {
		logger.Error("failed to create a statsd client", err)
		os.Exit(1)
	}
	defer statsdClient.Close()

	apiServer := constructApiServer(cfg, database, statsdClient, logger.Session("api-server"))
	stopper := constructStopper(database)

	routerRegister := constructRouteRegister(cfg.LogGuid, database, logger.Session("route-register"))

	metricsTicker := time.NewTicker(cfg.MetricsReportingInterval)
	metricsReporter := metrics.NewMetricsReporter(database, statsdClient, metricsTicker)

	members := grouper.Members{
		{"api-server", apiServer},
		{"conn-stopper", stopper},
		{"metrics", metricsReporter},
		{"route-register", routerRegister},
	}

	group := grouper.NewOrdered(os.Interrupt, members)
	process := ifrit.Invoke(sigmon.New(group))

	// This is used by testrunner to signal ready for tests.
	logger.Info("started", lager.Data{"port": *port})

	errChan := process.Wait()
	err = <-errChan
	if err != nil {
		logger.Error("shutdown-error", err)
		os.Exit(1)
	}
	logger.Info("exited")
}